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.

622 lines
20 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: Layout.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2013 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. * <table border="1" CELLPADDING="8">
  191. * <tr>
  192. * <td>Conversion Character</td>
  193. * <td>Effect</td>
  194. * </tr>
  195. *
  196. * <tr>
  197. * <td align=center><b>b</b></td>
  198. *
  199. * <td>Used to output file name component of path name.
  200. * E.g. <tt>main.cxx</tt> from path <tt>../../main.cxx</tt>.</td>
  201. * </tr>
  202. *
  203. * <tr>
  204. * <td align=center><b>c</b></td>
  205. *
  206. * <td>Used to output the logger of the logging event. The
  207. * logger conversion specifier can be optionally followed by
  208. * <em>precision specifier</em>, that is a decimal constant in
  209. * brackets.
  210. *
  211. * If a precision specifier is given, then only the corresponding
  212. * number of right most components of the logger name will be
  213. * printed. By default the logger name is printed in full.
  214. *
  215. * For example, for the logger name "a.b.c" the pattern
  216. * <b>%c{2}</b> will output "b.c".
  217. *
  218. * </td>
  219. * </tr>
  220. *
  221. * <tr>
  222. * <td align=center><b>d</b></td>
  223. *
  224. * <td>Used to output the date of the logging event in <b>UTC</b>.
  225. *
  226. * The date conversion specifier may be followed by a <em>date format
  227. * specifier</em> enclosed between braces. For example, <b>%%d{%%H:%%M:%%s}</b>
  228. * or <b>%%d{%%d&nbsp;%%b&nbsp;%%Y&nbsp;%%H:%%M:%%s}</b>. If no date format
  229. * specifier is given then <b>%%d{%%d&nbsp;%%m&nbsp;%%Y&nbsp;%%H:%%M:%%s}</b>
  230. * is assumed.
  231. *
  232. * The Following format options are possible:
  233. * <ul>
  234. * <li>%%a -- Abbreviated weekday name</li>
  235. * <li>%%A -- Full weekday name</li>
  236. * <li>%%b -- Abbreviated month name</li>
  237. * <li>%%B -- Full month name</li>
  238. * <li>%%c -- Standard date and time string</li>
  239. * <li>%%d -- Day of month as a decimal(1-31)</li>
  240. * <li>%%H -- Hour(0-23)</li>
  241. * <li>%%I -- Hour(1-12)</li>
  242. * <li>%%j -- Day of year as a decimal(1-366)</li>
  243. * <li>%%m -- Month as decimal(1-12)</li>
  244. * <li>%%M -- Minute as decimal(0-59)</li>
  245. * <li>%%p -- Locale's equivalent of AM or PM</li>
  246. * <li>%%q -- milliseconds as decimal(0-999) -- <b>Log4CPLUS specific</b>
  247. * <li>%%Q -- fractional milliseconds as decimal(0-999.999) -- <b>Log4CPLUS specific</b>
  248. * <li>%%S -- Second as decimal(0-59)</li>
  249. * <li>%%U -- Week of year, Sunday being first day(0-53)</li>
  250. * <li>%%w -- Weekday as a decimal(0-6, Sunday being 0)</li>
  251. * <li>%%W -- Week of year, Monday being first day(0-53)</li>
  252. * <li>%%x -- Standard date string</li>
  253. * <li>%%X -- Standard time string</li>
  254. * <li>%%y -- Year in decimal without century(0-99)</li>
  255. * <li>%%Y -- Year including century as decimal</li>
  256. * <li>%%Z -- Time zone name</li>
  257. * <li>%% -- The percent sign</li>
  258. * </ul>
  259. *
  260. * Lookup the documentation for the <code>strftime()</code> function
  261. * found in the <code>&lt;ctime&gt;</code> header for more information.
  262. * </td>
  263. * </tr>
  264. *
  265. * <tr>
  266. * <td align=center><b>D</b></td>
  267. *
  268. * <td>Used to output the date of the logging event in <b>local</b> time.
  269. *
  270. * All of the above information applies.
  271. * </td>
  272. * </tr>
  273. *
  274. * <tr>
  275. * <td align=center><b>E</b></td>
  276. *
  277. * <td>Used to output the value of a given environment variable. The
  278. * name of is supplied as an argument in brackets. If the variable does
  279. * exist then empty string will be used.
  280. *
  281. * For example, the pattern <b>%E{HOME}</b> will output the contents
  282. * of the HOME environment variable.
  283. * </td>
  284. * </tr>
  285. *
  286. * <tr>
  287. * <td align=center><b>F</b></td>
  288. *
  289. * <td>Used to output the file name where the logging request was
  290. * issued.
  291. *
  292. * <b>NOTE</b> Unlike log4j, there is no performance penalty for
  293. * calling this method.</td>
  294. * </tr>
  295. *
  296. * <tr>
  297. * <td align=center><b>h</b></td>
  298. *
  299. * <td>Used to output the hostname of this system (as returned
  300. * by gethostname(2)).
  301. *
  302. * <b>NOTE</b> The hostname is only retrieved once at
  303. * initialization.
  304. *
  305. * </td>
  306. * </tr>
  307. *
  308. * <tr>
  309. * <td align=center><b>H</b></td>
  310. *
  311. * <td>Used to output the fully-qualified domain name of this
  312. * system (as returned by gethostbyname(2) for the hostname
  313. * returned by gethostname(2)).
  314. *
  315. * <b>NOTE</b> The hostname is only retrieved once at
  316. * initialization.
  317. *
  318. * </td>
  319. * </tr>
  320. *
  321. * <tr>
  322. * <td align=center><b>l</b></td>
  323. *
  324. * <td>Equivalent to using "%F:%L"
  325. *
  326. * <b>NOTE:</b> Unlike log4j, there is no performance penalty for
  327. * calling this method.
  328. *
  329. * </td>
  330. * </tr>
  331. *
  332. * <tr>
  333. * <td align=center><b>L</b></td>
  334. *
  335. * <td>Used to output the line number from where the logging request
  336. * was issued.
  337. *
  338. * <b>NOTE:</b> Unlike log4j, there is no performance penalty for
  339. * calling this method.
  340. *
  341. * </tr>
  342. *
  343. * <tr>
  344. * <td align=center><b>m</b></td>
  345. * <td>Used to output the application supplied message associated with
  346. * the logging event.</td>
  347. * </tr>
  348. *
  349. * <tr>
  350. * <td align=center><b>M</b></td>
  351. *
  352. * <td>Used to output function name using
  353. * <code>__FUNCTION__</code> or similar macro.
  354. *
  355. * <b>NOTE</b> The <code>__FUNCTION__</code> macro is not
  356. * standard but it is common extension provided by all compilers
  357. * (as of 2010). In case it is missing or in case this feature
  358. * is disabled using the
  359. * <code>LOG4CPLUS_DISABLE_FUNCTION_MACRO</code> macro, %M
  360. * expands to an empty string.</td>
  361. * </tr>
  362. *
  363. * <tr>
  364. * <td align=center><b>n</b></td>
  365. *
  366. * <td>Outputs the platform dependent line separator character or
  367. * characters.
  368. * </tr>
  369. *
  370. * <tr>
  371. * <td align=center><b>p</b></td>
  372. * <td>Used to output the LogLevel of the logging event.</td>
  373. * </tr>
  374. *
  375. * <tr>
  376. * <td align=center><b>r</b></td>
  377. * <td>Used to output miliseconds since program start
  378. * of the logging event.</td>
  379. * </tr>
  380. *
  381. * <tr>
  382. * <td align=center><b>t</b></td>
  383. *
  384. * <td>Used to output the name of the thread that generated the
  385. * logging event.</td>
  386. * </tr>
  387. *
  388. * <tr>
  389. * <td align=center><b>T</b></td>
  390. *
  391. * <td>Used to output alternative name of the thread that generated the
  392. * logging event.</td>
  393. * </tr>
  394. *
  395. * <tr>
  396. * <td align=center><b>i</b></td>
  397. *
  398. * <td>Used to output the process ID of the process that generated the
  399. * logging event.</td>
  400. * </tr>
  401. *
  402. * <tr>
  403. * <td align=center><b>x</b></td>
  404. *
  405. * <td>Used to output the NDC (nested diagnostic context) associated
  406. * with the thread that generated the logging event.
  407. * </td>
  408. * </tr>
  409. *
  410. * <tr>
  411. * <td align=center><b>X</b></td>
  412. *
  413. * <td>Used to output the MDC (mapped diagnostic context)
  414. * associated with the thread that generated the logging
  415. * event. It takes optional key parameter. Without the key
  416. * paramter (%%X), it outputs the whole MDC map. With the key
  417. * (%%X{key}), it outputs just the key's value.
  418. * </td>
  419. * </tr>
  420. *
  421. * <tr>
  422. * <td align=center><b>"%%"</b></td>
  423. * <td>The sequence "%%" outputs a single percent sign.
  424. * </td>
  425. * </tr>
  426. *
  427. * </table>
  428. *
  429. * By default the relevant information is output as is. However,
  430. * with the aid of format modifiers it is possible to change the
  431. * minimum field width, the maximum field width and justification.
  432. *
  433. * The optional format modifier is placed between the percent sign
  434. * and the conversion character.
  435. *
  436. * The first optional format modifier is the <em>left justification
  437. * flag</em> which is just the minus (-) character. Then comes the
  438. * optional <em>minimum field width</em> modifier. This is a decimal
  439. * constant that represents the minimum number of characters to
  440. * output. If the data item requires fewer characters, it is padded on
  441. * either the left or the right until the minimum width is
  442. * reached. The default is to pad on the left (right justify) but you
  443. * can specify right padding with the left justification flag. The
  444. * padding character is space. If the data item is larger than the
  445. * minimum field width, the field is expanded to accommodate the
  446. * data. The value is never truncated.
  447. *
  448. * This behavior can be changed using the <em>maximum field
  449. * width</em> modifier which is designated by a period followed by a
  450. * decimal constant. If the data item is longer than the maximum
  451. * field, then the extra characters are removed from the
  452. * <em>beginning</em> of the data item and not from the end. For
  453. * example, it the maximum field width is eight and the data item is
  454. * ten characters long, then the first two characters of the data item
  455. * are dropped. This behavior deviates from the printf function in C
  456. * where truncation is done from the end.
  457. *
  458. * Below are various format modifier examples for the logger
  459. * conversion specifier.
  460. *
  461. *
  462. * <TABLE BORDER=1 CELLPADDING=8>
  463. * <tr>
  464. * <td>Format modifier</td>
  465. * <td>left justify</td>
  466. * <td>minimum width</td>
  467. * <td>maximum width</td>
  468. * <td>comment</td>
  469. * </tr>
  470. *
  471. * <tr>
  472. * <td align=center>%20c</td>
  473. * <td align=center>false</td>
  474. * <td align=center>20</td>
  475. * <td align=center>none</td>
  476. *
  477. * <td>Left pad with spaces if the logger name is less than 20
  478. * characters long.
  479. * </tr>
  480. *
  481. * <tr> <td align=center>%-20c</td> <td align=center>true</td> <td
  482. * align=center>20</td> <td align=center>none</td> <td>Right pad with
  483. * spaces if the logger name is less than 20 characters long.
  484. * </tr>
  485. *
  486. * <tr>
  487. * <td align=center>%.30c</td>
  488. * <td align=center>NA</td>
  489. * <td align=center>none</td>
  490. * <td align=center>30</td>
  491. *
  492. * <td>Truncate from the beginning if the logger name is longer than 30
  493. * characters.
  494. * </tr>
  495. *
  496. * <tr>
  497. * <td align=center>%20.30c</td>
  498. * <td align=center>false</td>
  499. * <td align=center>20</td>
  500. * <td align=center>30</td>
  501. *
  502. * <td>Left pad with spaces if the logger name is shorter than 20
  503. * characters. However, if logger name is longer than 30 characters,
  504. * then truncate from the beginning.
  505. * </tr>
  506. *
  507. * <tr>
  508. * <td align=center>%-20.30c</td>
  509. * <td align=center>true</td>
  510. * <td align=center>20</td>
  511. * <td align=center>30</td>
  512. *
  513. * <td>Right pad with spaces if the logger name is shorter than 20
  514. * characters. However, if logger name is longer than 30 characters,
  515. * then truncate from the beginning.
  516. * </tr>
  517. *
  518. * </table>
  519. *
  520. * Below are some examples of conversion patterns.
  521. *
  522. * <dl>
  523. *
  524. * <dt><b>"%r [%t] %-5p %c %x - %m%n"</b>
  525. * <dd>This is essentially the TTCC layout.
  526. *
  527. * <dt><b>"%-6r [%15.15t] %-5p %30.30c %x - %m%n"</b>
  528. *
  529. * <dd>Similar to the TTCC layout except that the relative time is
  530. * right padded if less than 6 digits, thread name is right padded if
  531. * less than 15 characters and truncated if longer and the logger
  532. * name is left padded if shorter than 30 characters and truncated if
  533. * longer.
  534. *
  535. * </dl>
  536. *
  537. * The above text is largely inspired from Peter A. Darnell and
  538. * Philip E. Margolis' highly recommended book "C -- a Software
  539. * Engineering Approach", ISBN 0-387-97389-3.
  540. *
  541. * <h3>Properties</h3>
  542. *
  543. * <dl>
  544. * <dt><tt>NDCMaxDepth</tt></dt>
  545. * <dd>This property limits how many deepest NDC components will
  546. * be printed by <b>%%x</b> specifier.</dd>
  547. *
  548. * <dt><tt>ConversionPattern</tt></dt>
  549. * <dd>This property specifies conversion pattern.</dd>
  550. * </dl>
  551. *
  552. */
  553. class LOG4CPLUS_EXPORT PatternLayout
  554. : public Layout
  555. {
  556. public:
  557. // Ctors and dtor
  558. PatternLayout(const log4cplus::tstring& pattern);
  559. PatternLayout(const log4cplus::helpers::Properties& properties);
  560. virtual ~PatternLayout();
  561. virtual void formatAndAppend(log4cplus::tostream& output,
  562. const log4cplus::spi::InternalLoggingEvent& event);
  563. protected:
  564. void init(const log4cplus::tstring& pattern, unsigned ndcMaxDepth = 0);
  565. // Data
  566. log4cplus::tstring pattern;
  567. std::vector<pattern::PatternConverter*> parsedPattern;
  568. private:
  569. // Disallow copying of instances of this class
  570. PatternLayout(const PatternLayout&);
  571. PatternLayout& operator=(const PatternLayout&);
  572. };
  573. } // end namespace log4cplus
  574. #endif // LOG4CPLUS_LAYOUT_HEADER_