[89] | 1 | /*
|
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
| 4 | * this work for additional information regarding copyright ownership.
|
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
| 6 | * (the "License"); you may not use this file except in compliance with
|
---|
| 7 | * the License. You may obtain a copy of the License at
|
---|
| 8 | *
|
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 10 | *
|
---|
| 11 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 14 | * See the License for the specific language governing permissions and
|
---|
| 15 | * limitations under the License.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | #ifndef _LOG4CXX_HELPERS_CACHED_DATE_FORMAT_H
|
---|
| 19 | #define _LOG4CXX_HELPERS_CACHED_DATE_FORMAT_H
|
---|
| 20 |
|
---|
| 21 | #include <log4cxx/helpers/dateformat.h>
|
---|
| 22 |
|
---|
| 23 | namespace log4cxx
|
---|
| 24 | {
|
---|
| 25 | namespace pattern
|
---|
| 26 | {
|
---|
| 27 | class LOG4CXX_EXPORT CachedDateFormat : public log4cxx::helpers::DateFormat {
|
---|
| 28 | public:
|
---|
| 29 | enum {
|
---|
| 30 | /*
|
---|
| 31 | * Constant used to represent that there was no change
|
---|
| 32 | * observed when changing the millisecond count.
|
---|
| 33 | */
|
---|
| 34 | NO_MILLISECONDS = -2,
|
---|
| 35 | /*
|
---|
| 36 | * Constant used to represent that there was an
|
---|
| 37 | * observed change, but was an expected change.
|
---|
| 38 | */
|
---|
| 39 | UNRECOGNIZED_MILLISECONDS = -1
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | private:
|
---|
| 43 | /**
|
---|
| 44 | * Supported digit set. If the wrapped DateFormat uses
|
---|
| 45 | * a different unit set, the millisecond pattern
|
---|
| 46 | * will not be recognized and duplicate requests
|
---|
| 47 | * will use the cache.
|
---|
| 48 | */
|
---|
| 49 | static const logchar digits[];
|
---|
| 50 |
|
---|
| 51 | enum {
|
---|
| 52 | /**
|
---|
| 53 | * First magic number used to detect the millisecond position.
|
---|
| 54 | */
|
---|
| 55 | magic1 = 654000,
|
---|
| 56 | /**
|
---|
| 57 | * Second magic number used to detect the millisecond position.
|
---|
| 58 | */
|
---|
| 59 | magic2 = 987000
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * Expected representation of first magic number.
|
---|
| 64 | */
|
---|
| 65 | static const logchar magicString1[];
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Expected representation of second magic number.
|
---|
| 70 | */
|
---|
| 71 | static const logchar magicString2[];
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * Expected representation of 0 milliseconds.
|
---|
| 76 | */
|
---|
| 77 | static const logchar zeroString[];
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Wrapped formatter.
|
---|
| 81 | */
|
---|
| 82 | log4cxx::helpers::DateFormatPtr formatter;
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Index of initial digit of millisecond pattern or
|
---|
| 86 | * UNRECOGNIZED_MILLISECONDS or NO_MILLISECONDS.
|
---|
| 87 | */
|
---|
| 88 | mutable int millisecondStart;
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * Integral second preceding the previous convered Date.
|
---|
| 92 | */
|
---|
| 93 | mutable log4cxx_time_t slotBegin;
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * Cache of previous conversion.
|
---|
| 98 | */
|
---|
| 99 | mutable LogString cache;
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Maximum validity period for the cache.
|
---|
| 104 | * Typically 1, use cache for duplicate requests only, or
|
---|
| 105 | * 1000000, use cache for requests within the same integral second.
|
---|
| 106 | */
|
---|
| 107 | const int expiration;
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * Date requested in previous conversion.
|
---|
| 111 | */
|
---|
| 112 | mutable log4cxx_time_t previousTime;
|
---|
| 113 |
|
---|
| 114 | public:
|
---|
| 115 | /**
|
---|
| 116 | * Creates a new CachedDateFormat object.
|
---|
| 117 | * @param dateFormat Date format, may not be null.
|
---|
| 118 | * @param expiration maximum cached range in microseconds.
|
---|
| 119 | * If the dateFormat is known to be incompatible with the
|
---|
| 120 | * caching algorithm, use a value of 0 to totally disable
|
---|
| 121 | * caching or 1 to only use cache for duplicate requests.
|
---|
| 122 | */
|
---|
| 123 | CachedDateFormat(const log4cxx::helpers::DateFormatPtr& dateFormat, int expiration);
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Finds start of millisecond field in formatted time.
|
---|
| 127 | * @param time long time, must be integral number of seconds
|
---|
| 128 | * @param formatted String corresponding formatted string
|
---|
| 129 | * @param formatter DateFormat date format
|
---|
| 130 | * @param pool pool.
|
---|
| 131 | * @return int position in string of first digit of milliseconds,
|
---|
| 132 | * -1 indicates no millisecond field, -2 indicates unrecognized
|
---|
| 133 | * field (likely RelativeTimeDateFormat)
|
---|
| 134 | */
|
---|
| 135 | static int findMillisecondStart(
|
---|
| 136 | log4cxx_time_t time, const LogString& formatted,
|
---|
| 137 | const log4cxx::helpers::DateFormatPtr& formatter,
|
---|
| 138 | log4cxx::helpers::Pool& pool);
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | * Formats a Date into a date/time string.
|
---|
| 142 | *
|
---|
| 143 | * @param date the date to format.
|
---|
| 144 | * @param sbuf the string buffer to write to.
|
---|
| 145 | * @param p memory pool.
|
---|
| 146 | */
|
---|
| 147 | virtual void format(LogString &sbuf,
|
---|
| 148 | log4cxx_time_t date,
|
---|
| 149 | log4cxx::helpers::Pool& p) const;
|
---|
| 150 |
|
---|
| 151 | private:
|
---|
| 152 | /**
|
---|
| 153 | * Formats a count of milliseconds (0-999) into a numeric representation.
|
---|
| 154 | * @param millis Millisecond coun between 0 and 999.
|
---|
| 155 | * @buf String buffer, may not be null.
|
---|
| 156 | * @offset Starting position in buffer, the length of the
|
---|
| 157 | * buffer must be at least offset + 3.
|
---|
| 158 | */
|
---|
| 159 | static void millisecondFormat(int millis,
|
---|
| 160 | LogString& buf,
|
---|
| 161 | int offset);
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | public:
|
---|
| 165 | /**
|
---|
| 166 | * Set timezone.
|
---|
| 167 | *
|
---|
| 168 | * @remarks Setting the timezone using getCalendar().setTimeZone()
|
---|
| 169 | * will likely cause caching to misbehave.
|
---|
| 170 | * @param zone TimeZone new timezone
|
---|
| 171 | */
|
---|
| 172 | virtual void setTimeZone(const log4cxx::helpers::TimeZonePtr& zone);
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * Format an integer consistent with the format method.
|
---|
| 176 | * @param s string to which the numeric string is appended.
|
---|
| 177 | * @param n integer value.
|
---|
| 178 | * @param p memory pool used during formatting.
|
---|
| 179 | */
|
---|
| 180 | virtual void numberFormat(LogString& s,
|
---|
| 181 | int n,
|
---|
| 182 | log4cxx::helpers::Pool& p) const;
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * Gets maximum cache validity for the specified SimpleDateTime
|
---|
| 186 | * conversion pattern.
|
---|
| 187 | * @param pattern conversion pattern, may not be null.
|
---|
| 188 | * @returns Duration in microseconds from an integral second
|
---|
| 189 | * that the cache will return consistent results.
|
---|
| 190 | */
|
---|
| 191 | static int getMaximumCacheValidity(const LogString& pattern);
|
---|
| 192 |
|
---|
| 193 | private:
|
---|
| 194 | CachedDateFormat(const CachedDateFormat&);
|
---|
| 195 | CachedDateFormat& operator=(const CachedDateFormat&);
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * Tests if two string regions are equal.
|
---|
| 199 | * @param target target string.
|
---|
| 200 | * @param toffset character position in target to start comparison.
|
---|
| 201 | * @param other other string.
|
---|
| 202 | * @param ooffset character position in other to start comparison.
|
---|
| 203 | * @param len length of region.
|
---|
| 204 | * @return true if regions are equal.
|
---|
| 205 | */
|
---|
| 206 | static bool regionMatches(
|
---|
| 207 | const LogString& target,
|
---|
| 208 | size_t toffset,
|
---|
| 209 | const LogString& other,
|
---|
| 210 | size_t ooffset,
|
---|
| 211 | size_t len);
|
---|
| 212 |
|
---|
| 213 | };
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | } // namespace helpers
|
---|
| 218 | } // namespace log4cxx
|
---|
| 219 |
|
---|
| 220 | #endif // _LOG4CXX_HELPERS_SIMPLE_DATE_FORMAT_H
|
---|