PatternFormatter.h 5.0 KB

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