You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.8 KiB

  1. // Module: Log4CPLUS
  2. // File: layout.cxx
  3. // Created: 6/2001
  4. // Author: Tad E. Smith
  5. //
  6. //
  7. // Copyright 2001-2010 Tad E. Smith
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. #include <log4cplus/layout.h>
  21. #include <log4cplus/helpers/stringhelper.h>
  22. #include <log4cplus/helpers/timehelper.h>
  23. #include <log4cplus/spi/loggingevent.h>
  24. #include <log4cplus/helpers/property.h>
  25. #include <log4cplus/internal/internal.h>
  26. #include <ostream>
  27. #include <iomanip>
  28. namespace log4cplus
  29. {
  30. void
  31. formatRelativeTimestamp (log4cplus::tostream & output,
  32. log4cplus::spi::InternalLoggingEvent const & event)
  33. {
  34. helpers::Time const rel_time
  35. = event.getTimestamp () - getTTCCLayoutTimeBase ();
  36. tchar const old_fill = output.fill ();
  37. helpers::time_t const sec = rel_time.sec ();
  38. if (sec != 0)
  39. output << sec << std::setfill (LOG4CPLUS_TEXT ('0')) << std::setw (3);
  40. output << rel_time.usec () / 1000;
  41. output.fill (old_fill);
  42. }
  43. //
  44. //
  45. //
  46. Layout::Layout ()
  47. : llmCache(getLogLevelManager())
  48. { }
  49. Layout::Layout (const log4cplus::helpers::Properties&)
  50. : llmCache(getLogLevelManager())
  51. { }
  52. Layout::~Layout()
  53. { }
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // log4cplus::SimpleLayout public methods
  56. ///////////////////////////////////////////////////////////////////////////////
  57. SimpleLayout::SimpleLayout ()
  58. { }
  59. SimpleLayout::SimpleLayout (const helpers::Properties& properties)
  60. : Layout (properties)
  61. { }
  62. SimpleLayout::~SimpleLayout()
  63. { }
  64. void
  65. SimpleLayout::formatAndAppend(log4cplus::tostream& output,
  66. const log4cplus::spi::InternalLoggingEvent& event)
  67. {
  68. output << llmCache.toString(event.getLogLevel())
  69. << LOG4CPLUS_TEXT(" - ")
  70. << event.getMessage()
  71. << LOG4CPLUS_TEXT("\n");
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // log4cplus::TTCCLayout ctors and dtor
  75. ///////////////////////////////////////////////////////////////////////////////
  76. TTCCLayout::TTCCLayout(bool use_gmtime_)
  77. : dateFormat()
  78. , use_gmtime(use_gmtime_)
  79. {
  80. }
  81. TTCCLayout::TTCCLayout(const log4cplus::helpers::Properties& properties)
  82. : Layout(properties)
  83. , dateFormat(properties.getProperty (LOG4CPLUS_TEXT("DateFormat"),
  84. internal::empty_str))
  85. , use_gmtime(false)
  86. {
  87. properties.getBool (use_gmtime, LOG4CPLUS_TEXT("Use_gmtime"));
  88. }
  89. TTCCLayout::~TTCCLayout()
  90. { }
  91. ///////////////////////////////////////////////////////////////////////////////
  92. // log4cplus::TTCCLayout public methods
  93. ///////////////////////////////////////////////////////////////////////////////
  94. void
  95. TTCCLayout::formatAndAppend(log4cplus::tostream& output,
  96. const log4cplus::spi::InternalLoggingEvent& event)
  97. {
  98. if (dateFormat.empty ())
  99. formatRelativeTimestamp (output, event);
  100. else
  101. output << event.getTimestamp().getFormattedTime(dateFormat,
  102. use_gmtime);
  103. output << LOG4CPLUS_TEXT(" [")
  104. << event.getThread()
  105. << LOG4CPLUS_TEXT("] ")
  106. << llmCache.toString(event.getLogLevel())
  107. << LOG4CPLUS_TEXT(" ")
  108. << event.getLoggerName()
  109. << LOG4CPLUS_TEXT(" <")
  110. << event.getNDC()
  111. << LOG4CPLUS_TEXT("> - ")
  112. << event.getMessage()
  113. << LOG4CPLUS_TEXT("\n");
  114. }
  115. } // namespace log4cplus