layout.h 21 KB

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