layout.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: Layout.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2015 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
  96. * using <code>localtime</code> or <code>gmtime</code>. There are also
  97. * <code>ThreadPrinting</code>, <code>CategoryPrefixing</code> and
  98. * <code>ContextPrinting</code> properties to turn on and off thread name,
  99. * logger name and NDC context printing respectively.
  100. *
  101. * Here is an example TTCCLayout output:
  102. *
  103. * ~~~~
  104. * 1 [0x60004dca0] WARN test.TestThread <> - Thread-3 TestThread.run()- Starting...
  105. * 1 [0x60004dca0] TRACE SlowObject <Thread-3 loop> - ENTER: SlowObject::doSomething()
  106. * 2 [0x60004b030] INFO SlowObject <Thread-0 loop> - Actually doing something...1, 2, 3, testing...DONE
  107. * 2 [0x60004b130] INFO SlowObject <Thread-1 loop> - Actually doing something...
  108. * 2 [0x60004b030] TRACE SlowObject <Thread-0 loop> - EXIT: SlowObject::doSomething()
  109. * 2 [0x60004b030] TRACE SlowObject <Thread-0 loop> - ENTER: SlowObject::doSomething()
  110. * 3 [0x60004b130] INFO SlowObject <Thread-1 loop> - Actually doing something...1, 2, 3, testing...DONE
  111. * 3 [0x60004cad0] INFO SlowObject <Thread-2 loop> - Actually doing something...
  112. * ~~~~
  113. *
  114. * The first field is the number of milliseconds elapsed since
  115. * the start of the program.
  116. *
  117. * The second field is the thread outputting the log
  118. * statement. (The value is the same as that of the `t` formatter
  119. * for PatternLayout.)
  120. *
  121. * The third field is the LogLevel.
  122. *
  123. * The fourth field is the logger to which the statement belongs.
  124. *
  125. * The fifth field (just before the '-') is the nested
  126. * diagnostic context. Note the nested diagnostic context may be
  127. * empty as in the first two statements. The text after the '-'
  128. * is the message of the statement.
  129. *
  130. * PatternLayout offers a much more flexible alternative.
  131. */
  132. class LOG4CPLUS_EXPORT TTCCLayout
  133. : public Layout
  134. {
  135. public:
  136. TTCCLayout(bool use_gmtime = false, bool thread_printing = true,
  137. bool category_prefixes = true, bool context_printing = true);
  138. TTCCLayout(const log4cplus::helpers::Properties& properties);
  139. virtual ~TTCCLayout();
  140. virtual void formatAndAppend(log4cplus::tostream& output,
  141. const log4cplus::spi::InternalLoggingEvent& event);
  142. bool getThreadPrinting() const;
  143. void setThreadPrinting(bool);
  144. bool getCategoryPrefixing() const;
  145. void setCategoryPrefixing(bool);
  146. bool getContextPrinting() const;
  147. void setContextPrinting(bool);
  148. protected:
  149. log4cplus::tstring dateFormat;
  150. bool use_gmtime;
  151. bool thread_printing;
  152. bool category_prefixing;
  153. bool context_printing;
  154. private:
  155. // Disallow copying of instances of this class
  156. TTCCLayout(const TTCCLayout&);
  157. TTCCLayout& operator=(const TTCCLayout&);
  158. };
  159. LOG4CPLUS_EXPORT helpers::Time const & getTTCCLayoutTimeBase ();
  160. /**
  161. * A flexible layout configurable with pattern string.
  162. *
  163. * The goal of this class is to format a InternalLoggingEvent and return
  164. * the results as a string. The results depend on the <em>conversion
  165. * pattern</em>.
  166. *
  167. * The conversion pattern is closely related to the conversion
  168. * pattern of the printf function in C. A conversion pattern is
  169. * composed of literal text and format control expressions called
  170. * <em>conversion specifiers</em>.
  171. *
  172. * <i>You are free to insert any literal text within the conversion
  173. * pattern.</i>
  174. *
  175. * Each conversion specifier starts with a percent sign (%%) and is
  176. * followed by optional <em>format modifiers</em> and a <em>conversion
  177. * character</em>. The conversion character specifies the type of
  178. * data, e.g. Logger, LogLevel, date, thread name. The format
  179. * modifiers control such things as field width, padding, left and
  180. * right justification. The following is a simple example.
  181. *
  182. * Let the conversion pattern be `"%-5p [%t]: %m%n"` and assume
  183. * that the log4cplus environment was set to use a PatternLayout. Then the
  184. * statements
  185. *
  186. * ~~~~{.c}
  187. * Logger root = Logger.getRoot();
  188. * LOG4CPLUS_DEBUG(root, "Message 1");
  189. * LOG4CPLUS_WARN(root, "Message 2");
  190. * ~~~~
  191. *
  192. * would yield the output
  193. *
  194. * ~~~~
  195. * DEBUG [main]: Message 1
  196. * WARN [main]: Message 2
  197. * ~~~~
  198. *
  199. * Note that there is no explicit separator between text and
  200. * conversion specifiers. The pattern parser knows when it has reached
  201. * the end of a conversion specifier when it reads a conversion
  202. * character. In the example above the conversion specifier
  203. * <b>"%-5p"</b> means the LogLevel of the logging event should be left
  204. * justified to a width of five characters.
  205. *
  206. * The recognized conversion characters are
  207. *
  208. * <table border="1" CELLPADDING="8">
  209. * <tr>
  210. * <td>Conversion Character</td>
  211. * <td>Effect</td>
  212. * </tr>
  213. *
  214. * <tr>
  215. * <td align=center><b>b</b></td>
  216. *
  217. * <td>Used to output file name component of path name.
  218. * E.g. <tt>main.cxx</tt> from path <tt>../../main.cxx</tt>.</td>
  219. * </tr>
  220. *
  221. * <tr>
  222. * <td align=center><b>c</b></td>
  223. *
  224. * <td>Used to output the logger of the logging event. The
  225. * logger conversion specifier can be optionally followed by
  226. * <em>precision specifier</em>, that is a decimal constant in
  227. * brackets.
  228. *
  229. * If a precision specifier is given, then only the corresponding
  230. * number of right most components of the logger name will be
  231. * printed. By default the logger name is printed in full.
  232. *
  233. * For example, for the logger name "a.b.c" the pattern
  234. * <b>%c{2}</b> will output "b.c".
  235. *
  236. * </td>
  237. * </tr>
  238. *
  239. * <tr>
  240. * <td align=center><b>d</b></td>
  241. *
  242. * <td>Used to output the date of the logging event in <b>UTC</b>.
  243. *
  244. * The date conversion specifier may be followed by a <em>date format
  245. * specifier</em> enclosed between braces. For example, <b>%%d{%%H:%%M:%%s}</b>
  246. * or <b>%%d{%%d&nbsp;%%b&nbsp;%%Y&nbsp;%%H:%%M:%%s}</b>. If no date format
  247. * specifier is given then <b>%%d{%%d&nbsp;%%m&nbsp;%%Y&nbsp;%%H:%%M:%%s}</b>
  248. * is assumed.
  249. *
  250. * The Following format options are possible:
  251. * <ul>
  252. * <li>%%a -- Abbreviated weekday name</li>
  253. * <li>%%A -- Full weekday name</li>
  254. * <li>%%b -- Abbreviated month name</li>
  255. * <li>%%B -- Full month name</li>
  256. * <li>%%c -- Standard date and time string</li>
  257. * <li>%%d -- Day of month as a decimal(1-31)</li>
  258. * <li>%%H -- Hour(0-23)</li>
  259. * <li>%%I -- Hour(1-12)</li>
  260. * <li>%%j -- Day of year as a decimal(1-366)</li>
  261. * <li>%%m -- Month as decimal(1-12)</li>
  262. * <li>%%M -- Minute as decimal(0-59)</li>
  263. * <li>%%p -- Locale's equivalent of AM or PM</li>
  264. * <li>%%q -- milliseconds as decimal(0-999) -- <b>Log4CPLUS specific</b>
  265. * <li>%%Q -- fractional milliseconds as decimal(0-999.999) -- <b>Log4CPLUS specific</b>
  266. * <li>%%S -- Second as decimal(0-59)</li>
  267. * <li>%%U -- Week of year, Sunday being first day(0-53)</li>
  268. * <li>%%w -- Weekday as a decimal(0-6, Sunday being 0)</li>
  269. * <li>%%W -- Week of year, Monday being first day(0-53)</li>
  270. * <li>%%x -- Standard date string</li>
  271. * <li>%%X -- Standard time string</li>
  272. * <li>%%y -- Year in decimal without century(0-99)</li>
  273. * <li>%%Y -- Year including century as decimal</li>
  274. * <li>%%Z -- Time zone name</li>
  275. * <li>%% -- The percent sign</li>
  276. * </ul>
  277. *
  278. * Lookup the documentation for the <code>strftime()</code> function
  279. * found in the <code>&lt;ctime&gt;</code> header for more information.
  280. * </td>
  281. * </tr>
  282. *
  283. * <tr>
  284. * <td align=center><b>D</b></td>
  285. *
  286. * <td>Used to output the date of the logging event in <b>local</b> time.
  287. *
  288. * All of the above information applies.
  289. * </td>
  290. * </tr>
  291. *
  292. * <tr>
  293. * <td align=center><b>E</b></td>
  294. *
  295. * <td>Used to output the value of a given environment variable. The
  296. * name of is supplied as an argument in brackets. If the variable does
  297. * exist then empty string will be used.
  298. *
  299. * For example, the pattern <b>%E{HOME}</b> will output the contents
  300. * of the HOME environment variable.
  301. * </td>
  302. * </tr>
  303. *
  304. * <tr>
  305. * <td align=center><b>F</b></td>
  306. *
  307. * <td>Used to output the file name where the logging request was
  308. * issued.
  309. *
  310. * <b>NOTE</b> Unlike log4j, there is no performance penalty for
  311. * calling this method.</td>
  312. * </tr>
  313. *
  314. * <tr>
  315. * <td align=center><b>h</b></td>
  316. *
  317. * <td>Used to output the hostname of this system (as returned
  318. * by gethostname(2)).
  319. *
  320. * <b>NOTE</b> The hostname is only retrieved once at
  321. * initialization.
  322. *
  323. * </td>
  324. * </tr>
  325. *
  326. * <tr>
  327. * <td align=center><b>H</b></td>
  328. *
  329. * <td>Used to output the fully-qualified domain name of this
  330. * system (as returned by gethostbyname(2) for the hostname
  331. * returned by gethostname(2)).
  332. *
  333. * <b>NOTE</b> The hostname is only retrieved once at
  334. * initialization.
  335. *
  336. * </td>
  337. * </tr>
  338. *
  339. * <tr>
  340. * <td align=center><b>l</b></td>
  341. *
  342. * <td>Equivalent to using "%F:%L"
  343. *
  344. * <b>NOTE:</b> Unlike log4j, there is no performance penalty for
  345. * calling this method.
  346. *
  347. * </td>
  348. * </tr>
  349. *
  350. * <tr>
  351. * <td align=center><b>L</b></td>
  352. *
  353. * <td>Used to output the line number from where the logging request
  354. * was issued.
  355. *
  356. * <b>NOTE:</b> Unlike log4j, there is no performance penalty for
  357. * calling this method.
  358. *
  359. * </tr>
  360. *
  361. * <tr>
  362. * <td align=center><b>m</b></td>
  363. * <td>Used to output the application supplied message associated with
  364. * the logging event.</td>
  365. * </tr>
  366. *
  367. * <tr>
  368. * <td align=center><b>M</b></td>
  369. *
  370. * <td>Used to output function name using
  371. * <code>__FUNCTION__</code> or similar macro.
  372. *
  373. * <b>NOTE</b> The <code>__FUNCTION__</code> macro is not
  374. * standard but it is common extension provided by all compilers
  375. * (as of 2010). In case it is missing or in case this feature
  376. * is disabled using the
  377. * <code>LOG4CPLUS_DISABLE_FUNCTION_MACRO</code> macro, %M
  378. * expands to an empty string.</td>
  379. * </tr>
  380. *
  381. * <tr>
  382. * <td align=center><b>n</b></td>
  383. *
  384. * <td>Outputs the platform dependent line separator character or
  385. * characters.
  386. * </tr>
  387. *
  388. * <tr>
  389. * <td align=center><b>p</b></td>
  390. * <td>Used to output the LogLevel of the logging event.</td>
  391. * </tr>
  392. *
  393. * <tr>
  394. * <td align=center><b>r</b></td>
  395. * <td>Used to output miliseconds since program start
  396. * of the logging event.</td>
  397. * </tr>
  398. *
  399. * <tr>
  400. * <td align=center><b>t</b></td>
  401. *
  402. * <td>Used to output the thread ID of the thread that generated
  403. * the logging event. (This is either `pthread_t` value returned
  404. * by `pthread_self()` on POSIX platforms or thread ID returned
  405. * by `GetCurrentThreadId()` on Windows.)</td>
  406. * </tr>
  407. *
  408. * <tr>
  409. * <td align=center><b>T</b></td>
  410. *
  411. * <td>Used to output alternative name of the thread that generated the
  412. * logging event.</td>
  413. * </tr>
  414. *
  415. * <tr>
  416. * <td align=center><b>i</b></td>
  417. *
  418. * <td>Used to output the process ID of the process that generated the
  419. * logging event.</td>
  420. * </tr>
  421. *
  422. * <tr>
  423. * <td align=center><b>x</b></td>
  424. *
  425. * <td>Used to output the NDC (nested diagnostic context) associated
  426. * with the thread that generated the logging event.
  427. * </td>
  428. * </tr>
  429. *
  430. * <tr>
  431. * <td align=center><b>X</b></td>
  432. *
  433. * <td>Used to output the MDC (mapped diagnostic context)
  434. * associated with the thread that generated the logging
  435. * event. It takes optional key parameter. Without the key
  436. * paramter (%%X), it outputs the whole MDC map. With the key
  437. * (%%X{key}), it outputs just the key's value.
  438. * </td>
  439. * </tr>
  440. *
  441. * <tr>
  442. * <td align=center><b>"%%"</b></td>
  443. * <td>The sequence "%%" outputs a single percent sign.
  444. * </td>
  445. * </tr>
  446. *
  447. * </table>
  448. *
  449. * By default the relevant information is output as is. However,
  450. * with the aid of format modifiers it is possible to change the
  451. * minimum field width, the maximum field width and justification.
  452. *
  453. * The optional format modifier is placed between the percent sign
  454. * and the conversion character.
  455. *
  456. * The first optional format modifier is the <em>left justification
  457. * flag</em> which is just the minus (-) character. Then comes the
  458. * optional <em>minimum field width</em> modifier. This is a decimal
  459. * constant that represents the minimum number of characters to
  460. * output. If the data item requires fewer characters, it is padded on
  461. * either the left or the right until the minimum width is
  462. * reached. The default is to pad on the left (right justify) but you
  463. * can specify right padding with the left justification flag. The
  464. * padding character is space. If the data item is larger than the
  465. * minimum field width, the field is expanded to accommodate the
  466. * data. The value is never truncated.
  467. *
  468. * This behavior can be changed using the <em>maximum field
  469. * width</em> modifier which is designated by a period followed by a
  470. * decimal constant. If the data item is longer than the maximum
  471. * field, then the extra characters are removed from the
  472. * <em>beginning</em> of the data item and not from the end. For
  473. * example, it the maximum field width is eight and the data item is
  474. * ten characters long, then the first two characters of the data item
  475. * are dropped. This behavior deviates from the printf function in C
  476. * where truncation is done from the end.
  477. *
  478. * Below are various format modifier examples for the logger
  479. * conversion specifier.
  480. *
  481. *
  482. * <TABLE BORDER=1 CELLPADDING=8>
  483. * <tr>
  484. * <td>Format modifier</td>
  485. * <td>left justify</td>
  486. * <td>minimum width</td>
  487. * <td>maximum width</td>
  488. * <td>comment</td>
  489. * </tr>
  490. *
  491. * <tr>
  492. * <td align=center>%20c</td>
  493. * <td align=center>false</td>
  494. * <td align=center>20</td>
  495. * <td align=center>none</td>
  496. *
  497. * <td>Left pad with spaces if the logger name is less than 20
  498. * characters long.
  499. * </tr>
  500. *
  501. * <tr> <td align=center>%-20c</td> <td align=center>true</td> <td
  502. * align=center>20</td> <td align=center>none</td> <td>Right pad with
  503. * spaces if the logger name is less than 20 characters long.
  504. * </tr>
  505. *
  506. * <tr>
  507. * <td align=center>%.30c</td>
  508. * <td align=center>NA</td>
  509. * <td align=center>none</td>
  510. * <td align=center>30</td>
  511. *
  512. * <td>Truncate from the beginning if the logger name is longer than 30
  513. * characters.
  514. * </tr>
  515. *
  516. * <tr>
  517. * <td align=center>%20.30c</td>
  518. * <td align=center>false</td>
  519. * <td align=center>20</td>
  520. * <td align=center>30</td>
  521. *
  522. * <td>Left pad with spaces if the logger name is shorter than 20
  523. * characters. However, if logger name is longer than 30 characters,
  524. * then truncate from the beginning.
  525. * </tr>
  526. *
  527. * <tr>
  528. * <td align=center>%-20.30c</td>
  529. * <td align=center>true</td>
  530. * <td align=center>20</td>
  531. * <td align=center>30</td>
  532. *
  533. * <td>Right pad with spaces if the logger name is shorter than 20
  534. * characters. However, if logger name is longer than 30 characters,
  535. * then truncate from the beginning.
  536. * </tr>
  537. *
  538. * </table>
  539. *
  540. * Below are some examples of conversion patterns.
  541. *
  542. * <dl>
  543. *
  544. * <dt><b>"%r [%t] %-5p %c %x - %m%n"</b>
  545. * <dd>This is essentially the TTCC layout.
  546. *
  547. * <dt><b>"%-6r [%15.15t] %-5p %30.30c %x - %m%n"</b>
  548. *
  549. * <dd>Similar to the TTCC layout except that the relative time is
  550. * right padded if less than 6 digits, thread name is right padded if
  551. * less than 15 characters and truncated if longer and the logger
  552. * name is left padded if shorter than 30 characters and truncated if
  553. * longer.
  554. *
  555. * </dl>
  556. *
  557. * The above text is largely inspired from Peter A. Darnell and
  558. * Philip E. Margolis' highly recommended book "C -- a Software
  559. * Engineering Approach", ISBN 0-387-97389-3.
  560. *
  561. * <h3>Properties</h3>
  562. *
  563. * <dl>
  564. * <dt><tt>NDCMaxDepth</tt></dt>
  565. * <dd>This property limits how many deepest NDC components will
  566. * be printed by <b>%%x</b> specifier.</dd>
  567. *
  568. * <dt><tt>ConversionPattern</tt></dt>
  569. * <dd>This property specifies conversion pattern.</dd>
  570. * </dl>
  571. *
  572. */
  573. class LOG4CPLUS_EXPORT PatternLayout
  574. : public Layout
  575. {
  576. public:
  577. // Ctors and dtor
  578. PatternLayout(const log4cplus::tstring& pattern);
  579. PatternLayout(const log4cplus::helpers::Properties& properties);
  580. virtual ~PatternLayout();
  581. virtual void formatAndAppend(log4cplus::tostream& output,
  582. const log4cplus::spi::InternalLoggingEvent& event);
  583. protected:
  584. void init(const log4cplus::tstring& pattern, unsigned ndcMaxDepth = 0);
  585. // Data
  586. log4cplus::tstring pattern;
  587. std::vector<pattern::PatternConverter*> parsedPattern;
  588. private:
  589. // Disallow copying of instances of this class
  590. PatternLayout(const PatternLayout&);
  591. PatternLayout& operator=(const PatternLayout&);
  592. };
  593. } // end namespace log4cplus
  594. #endif // LOG4CPLUS_LAYOUT_HEADER_