tracelogger.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: tracelogger.h
  4. // Created: 1/2009
  5. // Author: Vaclav Haisman
  6. //
  7. //
  8. // Copyright 2009-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_TRACELOGGER_H
  23. #define LOG4CPLUS_TRACELOGGER_H
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/logger.h>
  29. namespace log4cplus
  30. {
  31. /**
  32. * This class is used to produce "Trace" logging. When an instance of
  33. * this class is created, it will log a <code>"ENTER: " + msg</code>
  34. * log message if TRACE_LOG_LEVEL is enabled for <code>logger</code>.
  35. * When an instance of this class is destroyed, it will log a
  36. * <code>"ENTER: " + msg</code> log message if TRACE_LOG_LEVEL is enabled
  37. * for <code>logger</code>.
  38. * <p>
  39. * @see LOG4CPLUS_TRACE
  40. */
  41. class TraceLogger
  42. {
  43. public:
  44. TraceLogger(Logger l, log4cplus::tstring _msg,
  45. const char* _file = LOG4CPLUS_CALLER_FILE (),
  46. int _line = LOG4CPLUS_CALLER_LINE (),
  47. char const * _function = LOG4CPLUS_CALLER_FUNCTION ())
  48. : logger(std::move (l)), msg(std::move (_msg)), file(_file),
  49. function(_function), line(_line)
  50. {
  51. if(logger.isEnabledFor(TRACE_LOG_LEVEL))
  52. logger.forcedLog(TRACE_LOG_LEVEL, LOG4CPLUS_TEXT("ENTER: ") + msg,
  53. file, line, function);
  54. }
  55. ~TraceLogger()
  56. {
  57. if(logger.isEnabledFor(TRACE_LOG_LEVEL))
  58. logger.forcedLog(TRACE_LOG_LEVEL, LOG4CPLUS_TEXT("EXIT: ") + msg,
  59. file, line, function);
  60. }
  61. private:
  62. TraceLogger (TraceLogger const &);
  63. TraceLogger & operator = (TraceLogger const &);
  64. Logger logger;
  65. log4cplus::tstring msg;
  66. const char* file;
  67. const char* function;
  68. int line;
  69. };
  70. } // log4cplus
  71. #endif // LOG4CPLUS_TRACELOGGER_H