You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
19 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: Layout.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2010 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file */
  22. #ifndef LOG4CPLUS_LAYOUT_HEADER_
  23. #define LOG4CPLUS_LAYOUT_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/loglevel.h>
  29. #include <log4cplus/streams.h>
  30. #include <log4cplus/tstring.h>
  31. #include <vector>
  32. namespace log4cplus {
  33. // Forward Declarations
  34. namespace pattern
  35. {
  36. class PatternConverter;
  37. }
  38. namespace helpers
  39. {
  40. class Properties;
  41. class Time;
  42. }
  43. namespace spi
  44. {
  45. class InternalLoggingEvent;
  46. }
  47. /**
  48. * This class is used to layout strings sent to an {@link
  49. * log4cplus::Appender}.
  50. */
  51. class LOG4CPLUS_EXPORT Layout
  52. {
  53. public:
  54. Layout();
  55. Layout(const helpers::Properties& properties);
  56. virtual ~Layout() = 0;
  57. virtual void formatAndAppend(log4cplus::tostream& output,
  58. const log4cplus::spi::InternalLoggingEvent& event) = 0;
  59. protected:
  60. LogLevelManager& llmCache;
  61. private:
  62. // Disable copy
  63. Layout(const Layout&);
  64. Layout& operator=(Layout const &);
  65. };
  66. /**
  67. * SimpleLayout consists of the LogLevel of the log statement,
  68. * followed by " - " and then the log message itself. For example,
  69. *
  70. * <pre>
  71. * DEBUG - Hello world
  72. * </pre>
  73. *
  74. * {@link PatternLayout} offers a much more powerful alternative.
  75. */
  76. class LOG4CPLUS_EXPORT SimpleLayout
  77. : public Layout
  78. {
  79. public:
  80. SimpleLayout();
  81. SimpleLayout(const log4cplus::helpers::Properties& properties);
  82. virtual ~SimpleLayout();
  83. virtual void formatAndAppend(log4cplus::tostream& output,
  84. const log4cplus::spi::InternalLoggingEvent& event);
  85. private:
  86. // Disallow copying of instances of this class
  87. SimpleLayout(const SimpleLayout&);
  88. SimpleLayout& operator=(const SimpleLayout&);
  89. };
  90. /**
  91. * TTCC layout format consists of time, thread, Logger and nested
  92. * diagnostic context information, hence the name.
  93. *
  94. * The time format depends on the <code>DateFormat</code> used. Use the
  95. * <code>Use_gmtime</code> to specify whether messages should be logged using
  96. * <code>localtime</code> or <code>gmtime</code>.
  97. *
  98. * Here is an example TTCCLayout output:
  99. *
  100. * <pre>
  101. * 176 [main] INFO org.apache.log4j.examples.Sort - Populating an array of 2 elements in reverse order.
  102. * 225 [main] INFO org.apache.log4j.examples.SortAlgo - Entered the sort method.
  103. * 262 [main] DEBUG org.apache.log4j.examples.SortAlgo.OUTER i=1 - Outer loop.
  104. * 276 [main] DEBUG org.apache.log4j.examples.SortAlgo.SWAP i=1 j=0 - Swapping intArray[0] = 1 and intArray[1] = 0
  105. * 290 [main] DEBUG org.apache.log4j.examples.SortAlgo.OUTER i=0 - Outer loop.
  106. * 304 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Dump of interger array:
  107. * 317 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Element [0] = 0
  108. * 331 [main] INFO org.apache.log4j.examples.SortAlgo.DUMP - Element [1] = 1
  109. * 343 [main] INFO org.apache.log4j.examples.Sort - The next log statement should be an error message.
  110. * 346 [main] ERROR org.apache.log4j.examples.SortAlgo.DUMP - Tried to dump an uninitialized array.
  111. * 467 [main] INFO org.apache.log4j.examples.Sort - Exiting main method.
  112. * </pre>
  113. *
  114. * The first field is the number of milliseconds elapsed since the
  115. * start of the program. The second field is the thread outputting the
  116. * log statement. The third field is the LogLevel, the fourth field is
  117. * the logger to which the statement belongs.
  118. *
  119. * The fifth field (just before the '-') is the nested diagnostic
  120. * context. Note the nested diagnostic context may be empty as in the
  121. * first two statements. The text after the '-' is the message of the
  122. * statement.
  123. *
  124. * PatternLayout offers a much more flexible alternative.
  125. */
  126. class LOG4CPLUS_EXPORT TTCCLayout
  127. : public Layout
  128. {
  129. public:
  130. // Ctor and dtor
  131. TTCCLayout(bool use_gmtime = false);
  132. TTCCLayout(const log4cplus::helpers::Properties& properties);
  133. virtual ~TTCCLayout();
  134. virtual void formatAndAppend(log4cplus::tostream& output,
  135. const log4cplus::spi::InternalLoggingEvent& event);
  136. protected:
  137. log4cplus::tstring dateFormat;
  138. bool use_gmtime;
  139. private:
  140. // Disallow copying of instances of this class
  141. TTCCLayout(const TTCCLayout&);
  142. TTCCLayout& operator=(const TTCCLayout&);
  143. };
  144. LOG4CPLUS_EXPORT helpers::Time const & getTTCCLayoutTimeBase ();
  145. /**
  146. * A flexible layout configurable with pattern string.
  147. *
  148. * The goal of this class is to format a InternalLoggingEvent and return
  149. * the results as a string. The results depend on the <em>conversion
  150. * pattern</em>.
  151. *
  152. * The conversion pattern is closely related to the conversion
  153. * pattern of the printf function in C. A conversion pattern is
  154. * composed of literal text and format control expressions called
  155. * <em>conversion specifiers</em>.
  156. *
  157. * <i>You are free to insert any literal text within the conversion
  158. * pattern.</i>
  159. *
  160. * Each conversion specifier starts with a percent sign (%%) and is
  161. * followed by optional <em>format modifiers</em> and a <em>conversion
  162. * character</em>. The conversion character specifies the type of
  163. * data, e.g. Logger, LogLevel, date, thread name. The format
  164. * modifiers control such things as field width, padding, left and
  165. * right justification. The following is a simple example.
  166. *
  167. * Let the conversion pattern be <b>"%-5p [%t]: %m%n"</b> and assume
  168. * that the log4cplus environment was set to use a PatternLayout. Then the
  169. * statements
  170. * <code><pre>
  171. * Logger root = Logger.getRoot();
  172. * LOG4CPLUS_DEBUG(root, "Message 1");
  173. * LOG4CPLUS_WARN(root, "Message 2");
  174. * </pre></code>
  175. * would yield the output
  176. * <tt><pre>
  177. * DEBUG [main]: Message 1
  178. * WARN [main]: Message 2
  179. * </pre></tt>
  180. *
  181. * Note that there is no explicit separator between text and
  182. * conversion specifiers. The pattern parser knows when it has reached
  183. * the end of a conversion specifier when it reads a conversion
  184. * character. In the example above the conversion specifier
  185. * <b>"%-5p"</b> means the LogLevel of the logging event should be left
  186. * justified to a width of five characters.
  187. *
  188. * The recognized conversion characters are
  189. *
  190. *
  191. * <table border="1" CELLPADDING="8">
  192. * <tr>
  193. * <td>Conversion Character</td>
  194. * <td>Effect</td>
  195. * </tr>
  196. *
  197. * <tr>
  198. * <td align=center><b>b</b></td>
  199. *
  200. * <td>Used to output file name component of path name.
  201. * E.g. <tt>main.cxx</tt> from path <tt>../../main.cxx</tt>.</td>
  202. * </tr>
  203. *
  204. * <tr>
  205. * <td align=center><b>c</b></td>
  206. *
  207. * <td>Used to output the logger of the logging event. The
  208. * logger conversion specifier can be optionally followed by
  209. * <em>precision specifier</em>, that is a decimal constant in
  210. * brackets.
  211. *
  212. * If a precision specifier is given, then only the corresponding
  213. * number of right most components of the logger name will be
  214. * printed. By default the logger name is printed in full.
  215. *
  216. * For example, for the logger name "a.b.c" the pattern
  217. * <b>%c{2}</b> will output "b.c".
  218. *
  219. * </td>
  220. * </tr>
  221. *
  222. * <tr>
  223. * <td align=center><b>d</b></td>
  224. *
  225. * <td>Used to output the date of the logging event in <b>UTC</b>.
  226. *
  227. * The date conversion specifier may be followed by a <em>date format
  228. * specifier</em> enclosed between braces. For example, <b>%%d{%%H:%%M:%%s}</b>
  229. * or <b>%%d{%%d&nbsp;%%b&nbsp;%%Y&nbsp;%%H:%%M:%%s}</b>. If no date format
  230. * specifier is given then <b>%%d{%%d&nbsp;%%m&nbsp;%%Y&nbsp;%%H:%%M:%%s}</b>
  231. * is assumed.
  232. *
  233. * The Following format options are possible:
  234. * <ul>
  235. * <li>%%a -- Abbreviated weekday name</li>
  236. * <li>%%A -- Full weekday name</li>
  237. * <li>%%b -- Abbreviated month name</li>
  238. * <li>%%B -- Full month name</li>
  239. * <li>%%c -- Standard date and time string</li>
  240. * <li>%%d -- Day of month as a decimal(1-31)</li>
  241. * <li>%%H -- Hour(0-23)</li>
  242. * <li>%%I -- Hour(1-12)</li>
  243. * <li>%%j -- Day of year as a decimal(1-366)</li>
  244. * <li>%%m -- Month as decimal(1-12)</li>
  245. * <li>%%M -- Minute as decimal(0-59)</li>
  246. * <li>%%p -- Locale's equivalent of AM or PM</li>
  247. * <li>%%q -- milliseconds as decimal(0-999) -- <b>Log4CPLUS specific</b>
  248. * <li>%%Q -- fractional milliseconds as decimal(0-999.999) -- <b>Log4CPLUS specific</b>
  249. * <li>%%S -- Second as decimal(0-59)</li>
  250. * <li>%%U -- Week of year, Sunday being first day(0-53)</li>
  251. * <li>%%w -- Weekday as a decimal(0-6, Sunday being 0)</li>
  252. * <li>%%W -- Week of year, Monday being first day(0-53)</li>
  253. * <li>%%x -- Standard date string</li>
  254. * <li>%%X -- Standard time string</li>
  255. * <li>%%y -- Year in decimal without century(0-99)</li>
  256. * <li>%%Y -- Year including century as decimal</li>
  257. * <li>%%Z -- Time zone name</li>
  258. * <li>%% -- The percent sign</li>
  259. * </ul>
  260. *
  261. * Lookup the documentation for the <code>strftime()</code> function
  262. * found in the <code>&lt;ctime&gt;</code> header for more information.
  263. * </td>
  264. * </tr>
  265. *
  266. * <tr>
  267. * <td align=center><b>D</b></td>
  268. *
  269. * <td>Used to output the date of the logging event in <b>local</b> time.
  270. *
  271. * All of the above information applies.
  272. * </td>
  273. * </tr>
  274. *
  275. * <tr>
  276. * <td align=center><b>F</b></td>
  277. *
  278. * <td>Used to output the file name where the logging request was
  279. * issued.
  280. *
  281. * <b>NOTE</b> Unlike log4j, there is no performance penalty for
  282. * calling this method.</td>
  283. * </tr>
  284. *
  285. * <tr>
  286. * <td align=center><b>h</b></td>
  287. *
  288. * <td>Used to output the hostname of this system (as returned
  289. * by gethostname(2)).
  290. *
  291. * <b>NOTE</b> The hostname is only retrieved once at
  292. * initialization.
  293. *
  294. * </td>
  295. * </tr>
  296. *
  297. * <tr>
  298. * <td align=center><b>H</b></td>
  299. *
  300. * <td>Used to output the fully-qualified domain name of this
  301. * system (as returned by gethostbyname(2) for the hostname
  302. * returned by gethostname(2)).
  303. *
  304. * <b>NOTE</b> The hostname is only retrieved once at
  305. * initialization.
  306. *
  307. * </td>
  308. * </tr>
  309. *
  310. * <tr>
  311. * <td align=center><b>l</b></td>
  312. *
  313. * <td>Equivalent to using "%F:%L"
  314. *
  315. * <b>NOTE:</b> Unlike log4j, there is no performance penalty for
  316. * calling this method.
  317. *
  318. * </td>
  319. * </tr>
  320. *
  321. * <tr>
  322. * <td align=center><b>L</b></td>
  323. *
  324. * <td>Used to output the line number from where the logging request
  325. * was issued.
  326. *
  327. * <b>NOTE:</b> Unlike log4j, there is no performance penalty for
  328. * calling this method.
  329. *
  330. * </tr>
  331. *
  332. * <tr>
  333. * <td align=center><b>m</b></td>
  334. * <td>Used to output the application supplied message associated with
  335. * the logging event.</td>
  336. * </tr>
  337. *
  338. * <tr>
  339. * <td align=center><b>M</b></td>
  340. *
  341. * <td>Used to output function name using
  342. * <code>__FUNCTION__</code> or similar macro.
  343. *
  344. * <b>NOTE</b> The <code>__FUNCTION__</code> macro is not
  345. * standard but it is common extension provided by all compilers
  346. * (as of 2010). In case it is missing or in case this feature
  347. * is disabled using the
  348. * <code>LOG4CPLUS_DISABLE_FUNCTION_MACRO</code> macro, %M
  349. * expands to an empty string.</td>
  350. * </tr>
  351. *
  352. * <tr>
  353. * <td align=center><b>n</b></td>
  354. *
  355. * <td>Outputs the platform dependent line separator character or
  356. * characters.
  357. * </tr>
  358. *
  359. * <tr>
  360. * <td align=center><b>p</b></td>
  361. * <td>Used to output the LogLevel of the logging event.</td>
  362. * </tr>
  363. *
  364. * <tr>
  365. * <td align=center><b>r</b></td>
  366. * <td>Used to output miliseconds since program start
  367. * of the logging event.</td>
  368. * </tr>
  369. *
  370. * <tr>
  371. * <td align=center><b>t</b></td>
  372. *
  373. * <td>Used to output the name of the thread that generated the
  374. * logging event.</td>
  375. * </tr>
  376. *
  377. * <tr>
  378. * <td align=center><b>T</b></td>
  379. *
  380. * <td>Used to output alternative name of the thread that generated the
  381. * logging event.</td>
  382. * </tr>
  383. *
  384. * <tr>
  385. * <td align=center><b>i</b></td>
  386. *
  387. * <td>Used to output the process ID of the process that generated the
  388. * logging event.</td>
  389. * </tr>
  390. *
  391. * <tr>
  392. * <td align=center><b>x</b></td>
  393. *
  394. * <td>Used to output the NDC (nested diagnostic context) associated
  395. * with the thread that generated the logging event.
  396. * </td>
  397. * </tr>
  398. *
  399. * <tr>
  400. * <td align=center><b>"%%"</b></td>
  401. * <td>The sequence "%%" outputs a single percent sign.
  402. * </td>
  403. * </tr>
  404. *
  405. * </table>
  406. *
  407. * By default the relevant information is output as is. However,
  408. * with the aid of format modifiers it is possible to change the
  409. * minimum field width, the maximum field width and justification.
  410. *
  411. * The optional format modifier is placed between the percent sign
  412. * and the conversion character.
  413. *
  414. * The first optional format modifier is the <em>left justification
  415. * flag</em> which is just the minus (-) character. Then comes the
  416. * optional <em>minimum field width</em> modifier. This is a decimal
  417. * constant that represents the minimum number of characters to
  418. * output. If the data item requires fewer characters, it is padded on
  419. * either the left or the right until the minimum width is
  420. * reached. The default is to pad on the left (right justify) but you
  421. * can specify right padding with the left justification flag. The
  422. * padding character is space. If the data item is larger than the
  423. * minimum field width, the field is expanded to accommodate the
  424. * data. The value is never truncated.
  425. *
  426. * This behavior can be changed using the <em>maximum field
  427. * width</em> modifier which is designated by a period followed by a
  428. * decimal constant. If the data item is longer than the maximum
  429. * field, then the extra characters are removed from the
  430. * <em>beginning</em> of the data item and not from the end. For
  431. * example, it the maximum field width is eight and the data item is
  432. * ten characters long, then the first two characters of the data item
  433. * are dropped. This behavior deviates from the printf function in C
  434. * where truncation is done from the end.
  435. *
  436. * Below are various format modifier examples for the logger
  437. * conversion specifier.
  438. *
  439. *
  440. * <TABLE BORDER=1 CELLPADDING=8>
  441. * <tr>
  442. * <td>Format modifier</td>
  443. * <td>left justify</td>
  444. * <td>minimum width</td>
  445. * <td>maximum width</td>
  446. * <td>comment</td>
  447. * </tr>
  448. *
  449. * <tr>
  450. * <td align=center>%20c</td>
  451. * <td align=center>false</td>
  452. * <td align=center>20</td>
  453. * <td align=center>none</td>
  454. *
  455. * <td>Left pad with spaces if the logger name is less than 20
  456. * characters long.
  457. * </tr>
  458. *
  459. * <tr> <td align=center>%-20c</td> <td align=center>true</td> <td
  460. * align=center>20</td> <td align=center>none</td> <td>Right pad with
  461. * spaces if the logger name is less than 20 characters long.
  462. * </tr>
  463. *
  464. * <tr>
  465. * <td align=center>%.30c</td>
  466. * <td align=center>NA</td>
  467. * <td align=center>none</td>
  468. * <td align=center>30</td>
  469. *
  470. * <td>Truncate from the beginning if the logger name is longer than 30
  471. * characters.
  472. * </tr>
  473. *
  474. * <tr>
  475. * <td align=center>%20.30c</td>
  476. * <td align=center>false</td>
  477. * <td align=center>20</td>
  478. * <td align=center>30</td>
  479. *
  480. * <td>Left pad with spaces if the logger name is shorter than 20
  481. * characters. However, if logger name is longer than 30 characters,
  482. * then truncate from the beginning.
  483. * </tr>
  484. *
  485. * <tr>
  486. * <td align=center>%-20.30c</td>
  487. * <td align=center>true</td>
  488. * <td align=center>20</td>
  489. * <td align=center>30</td>
  490. *
  491. * <td>Right pad with spaces if the logger name is shorter than 20
  492. * characters. However, if logger name is longer than 30 characters,
  493. * then truncate from the beginning.
  494. * </tr>
  495. *
  496. * </table>
  497. *
  498. * Below are some examples of conversion patterns.
  499. *
  500. * <dl>
  501. *
  502. * <dt><b>"%r [%t] %-5p %c %x - %m%n"</b>
  503. * <dd>This is essentially the TTCC layout.
  504. *
  505. * <dt><b>"%-6r [%15.15t] %-5p %30.30c %x - %m%n"</b>
  506. *
  507. * <dd>Similar to the TTCC layout except that the relative time is
  508. * right padded if less than 6 digits, thread name is right padded if
  509. * less than 15 characters and truncated if longer and the logger
  510. * name is left padded if shorter than 30 characters and truncated if
  511. * longer.
  512. *
  513. * </dl>
  514. *
  515. * The above text is largely inspired from Peter A. Darnell and
  516. * Philip E. Margolis' highly recommended book "C -- a Software
  517. * Engineering Approach", ISBN 0-387-97389-3.
  518. */
  519. class LOG4CPLUS_EXPORT PatternLayout
  520. : public Layout
  521. {
  522. public:
  523. // Ctors and dtor
  524. PatternLayout(const log4cplus::tstring& pattern);
  525. PatternLayout(const log4cplus::helpers::Properties& properties);
  526. virtual ~PatternLayout();
  527. virtual void formatAndAppend(log4cplus::tostream& output,
  528. const log4cplus::spi::InternalLoggingEvent& event);
  529. protected:
  530. void init(const log4cplus::tstring& pattern, unsigned ndcMaxDepth = 0);
  531. // Data
  532. log4cplus::tstring pattern;
  533. std::vector<pattern::PatternConverter*> parsedPattern;
  534. private:
  535. // Disallow copying of instances of this class
  536. PatternLayout(const PatternLayout&);
  537. PatternLayout& operator=(const PatternLayout&);
  538. };
  539. } // end namespace log4cplus
  540. #endif // LOG4CPLUS_LAYOUT_HEADER_