PatternFormatter.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // PatternFormatter.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/PatternFormatter.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: PatternFormatter
  9. //
  10. // Definition of the PatternFormatter class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_PatternFormatter_INCLUDED
  18. #define Foundation_PatternFormatter_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Formatter.h"
  21. #include "Poco/Message.h"
  22. #include <vector>
  23. namespace Poco {
  24. class Foundation_API PatternFormatter: public Formatter
  25. /// This Formatter allows for custom formatting of
  26. /// log messages based on format patterns.
  27. ///
  28. /// The format pattern is used as a template to format the message and
  29. /// is copied character by character except for the following special characters,
  30. /// which are replaced by the corresponding value.
  31. ///
  32. /// * %s - message source
  33. /// * %t - message text
  34. /// * %l - message priority level (1 .. 7)
  35. /// * %p - message priority (Fatal, Critical, Error, Warning, Notice, Information, Debug, Trace)
  36. /// * %q - abbreviated message priority (F, C, E, W, N, I, D, T)
  37. /// * %P - message process identifier
  38. /// * %T - message thread name
  39. /// * %I - message thread identifier (numeric)
  40. /// * %N - node or host name
  41. /// * %U - message source file path (empty string if not set)
  42. /// * %u - message source line number (0 if not set)
  43. /// * %w - message date/time abbreviated weekday (Mon, Tue, ...)
  44. /// * %W - message date/time full weekday (Monday, Tuesday, ...)
  45. /// * %b - message date/time abbreviated month (Jan, Feb, ...)
  46. /// * %B - message date/time full month (January, February, ...)
  47. /// * %d - message date/time zero-padded day of month (01 .. 31)
  48. /// * %e - message date/time day of month (1 .. 31)
  49. /// * %f - message date/time space-padded day of month ( 1 .. 31)
  50. /// * %m - message date/time zero-padded month (01 .. 12)
  51. /// * %n - message date/time month (1 .. 12)
  52. /// * %o - message date/time space-padded month ( 1 .. 12)
  53. /// * %y - message date/time year without century (70)
  54. /// * %Y - message date/time year with century (1970)
  55. /// * %H - message date/time hour (00 .. 23)
  56. /// * %h - message date/time hour (00 .. 12)
  57. /// * %a - message date/time am/pm
  58. /// * %A - message date/time AM/PM
  59. /// * %M - message date/time minute (00 .. 59)
  60. /// * %S - message date/time second (00 .. 59)
  61. /// * %i - message date/time millisecond (000 .. 999)
  62. /// * %c - message date/time centisecond (0 .. 9)
  63. /// * %F - message date/time fractional seconds/microseconds (000000 - 999999)
  64. /// * %z - time zone differential in ISO 8601 format (Z or +NN.NN)
  65. /// * %Z - time zone differential in RFC format (GMT or +NNNN)
  66. /// * %L - convert time to local time (must be specified before any date/time specifier; does not itself output anything)
  67. /// * %E - epoch time (UTC, seconds since midnight, January 1, 1970)
  68. /// * %v[width] - the message source (%s) but text length is padded/cropped to 'width'
  69. /// * %[name] - the value of the message parameter with the given name
  70. /// * %% - percent sign
  71. {
  72. public:
  73. PatternFormatter();
  74. /// Creates a PatternFormatter.
  75. /// The format pattern must be specified with
  76. /// a call to setProperty.
  77. PatternFormatter(const std::string& format);
  78. /// Creates a PatternFormatter that uses the
  79. /// given format pattern.
  80. ~PatternFormatter();
  81. /// Destroys the PatternFormatter.
  82. void format(const Message& msg, std::string& text);
  83. /// Formats the message according to the specified
  84. /// format pattern and places the result in text.
  85. void setProperty(const std::string& name, const std::string& value);
  86. /// Sets the property with the given name to the given value.
  87. ///
  88. /// The following properties are supported:
  89. ///
  90. /// * pattern: The format pattern. See the PatternFormatter class
  91. /// for details.
  92. /// * times: Specifies whether times are adjusted for local time
  93. /// or taken as they are in UTC. Supported values are "local" and "UTC".
  94. ///
  95. /// If any other property name is given, a PropertyNotSupported
  96. /// exception is thrown.
  97. std::string getProperty(const std::string& name) const;
  98. /// Returns the value of the property with the given name or
  99. /// throws a PropertyNotSupported exception if the given
  100. /// name is not recognized.
  101. static const std::string PROP_PATTERN;
  102. static const std::string PROP_TIMES;
  103. protected:
  104. static const std::string& getPriorityName(int);
  105. /// Returns a string for the given priority value.
  106. private:
  107. struct PatternAction
  108. {
  109. PatternAction(): key(0), length(0)
  110. {
  111. }
  112. char key;
  113. int length;
  114. std::string property;
  115. std::string prepend;
  116. };
  117. void parsePattern();
  118. /// Will parse the _pattern string into the vector of PatternActions,
  119. /// which contains the message key, any text that needs to be written first
  120. /// a proprety in case of %[] and required length.
  121. std::vector<PatternAction> _patternActions;
  122. bool _localTime;
  123. std::string _pattern;
  124. };
  125. } // namespace Poco
  126. #endif // Foundation_PatternFormatter_INCLUDED