PatternFormatter.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // PatternFormatter.h
  3. //
  4. // $Id: //poco/svn/Foundation/include/Poco/PatternFormatter.h#3 $
  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. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_PatternFormatter_INCLUDED
  38. #define Foundation_PatternFormatter_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/Formatter.h"
  41. #include "Poco/Message.h"
  42. namespace Poco {
  43. class Foundation_API PatternFormatter: public Formatter
  44. /// This Formatter allows for custom formatting of
  45. /// log messages based on format patterns.
  46. ///
  47. /// The format pattern is used as a template to format the message and
  48. /// is copied character by character except for the following special characters,
  49. /// which are replaced by the corresponding value.
  50. ///
  51. /// * %s - message source
  52. /// * %t - message text
  53. /// * %l - message priority level (1 .. 7)
  54. /// * %p - message priority (Fatal, Critical, Error, Warning, Notice, Information, Debug, Trace)
  55. /// * %q - abbreviated message priority (F, C, E, W,N, I, D, T)
  56. /// * %P - message process identifier
  57. /// * %T - message thread name
  58. /// * %I - message thread identifier (numeric)
  59. /// * %N - node or host name
  60. /// * %w - message date/time abbreviated weekday (Mon, Tue, ...)
  61. /// * %W - message date/time full weekday (Monday, Tuesday, ...)
  62. /// * %b - message date/time abbreviated month (Jan, Feb, ...)
  63. /// * %B - message date/time full month (January, February, ...)
  64. /// * %d - message date/time zero-padded day of month (01 .. 31)
  65. /// * %e - message date/time day of month (1 .. 31)
  66. /// * %f - message date/time space-padded day of month ( 1 .. 31)
  67. /// * %m - message date/time zero-padded month (01 .. 12)
  68. /// * %n - message date/time month (1 .. 12)
  69. /// * %o - message date/time space-padded month ( 1 .. 12)
  70. /// * %y - message date/time year without century (70)
  71. /// * %Y - message date/time year with century (1970)
  72. /// * %H - message date/time hour (00 .. 23)
  73. /// * %h - message date/time hour (00 .. 12)
  74. /// * %a - message date/time am/pm
  75. /// * %A - message date/time AM/PM
  76. /// * %M - message date/time minute (00 .. 59)
  77. /// * %S - message date/time second (00 .. 59)
  78. /// * %i - message date/time millisecond (000 .. 999)
  79. /// * %c - message date/time centisecond (0 .. 9)
  80. /// * %z - time zone differential in ISO 8601 format (Z or +NN.NN).
  81. /// * %Z - time zone differential in RFC format (GMT or +NNNN)
  82. /// * %[name] - the value of the message parameter with the given name
  83. /// * %% - percent sign
  84. {
  85. public:
  86. PatternFormatter();
  87. /// Creates a PatternFormatter.
  88. /// The format pattern must be specified with
  89. /// a call to setProperty.
  90. PatternFormatter(const std::string& format);
  91. /// Creates a PatternFormatter that uses the
  92. /// given format pattern.
  93. ~PatternFormatter();
  94. /// Destroys the PatternFormatter.
  95. void format(const Message& msg, std::string& text);
  96. /// Formats the message according to the specified
  97. /// format pattern and places the result in text.
  98. void setProperty(const std::string& name, const std::string& value);
  99. /// Sets the property with the given name to the given value.
  100. ///
  101. /// The following properties are supported:
  102. ///
  103. /// * pattern: The format pattern. See the PatternFormatter class
  104. /// for details.
  105. /// * times: Specifies whether times are adjusted for local time
  106. /// or taken as they are in UTC. Supported values are "local" and "UTC".
  107. ///
  108. /// If any other property name is given, a PropertyNotSupported
  109. /// exception is thrown.
  110. std::string getProperty(const std::string& name) const;
  111. /// Returns the value of the property with the given name or
  112. /// throws a PropertyNotSupported exception if the given
  113. /// name is not recognized.
  114. static const std::string PROP_PATTERN;
  115. static const std::string PROP_TIMES;
  116. protected:
  117. static const std::string& getPriorityName(int);
  118. /// Returns a string for the given priority value.
  119. static void fmt(std::string& str, int value);
  120. static void fmt(std::string& str, int value, int width);
  121. static void fmt0(std::string& str, int value, int width);
  122. private:
  123. bool _localTime;
  124. std::string _pattern;
  125. };
  126. } // namespace Poco
  127. #endif // Foundation_PatternFormatter_INCLUDED