customloglevelmanager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: customloglevelmanager.h
  4. // Created: 12/2018
  5. // Author: Jens Rehsack
  6. // Author: Václav Haisman
  7. //
  8. //
  9. // Copyright (C) 2018, Jens Rehsack. All rights reserved.
  10. //
  11. // Redistribution and use in source and binary forms, with or without modifica-
  12. // tion, are permitted provided that the following conditions are met:
  13. //
  14. // 1. Redistributions of source code must retain the above copyright notice,
  15. // this list of conditions and the following disclaimer.
  16. //
  17. // 2. Redistributions in binary form must reproduce the above copyright notice,
  18. // this list of conditions and the following disclaimer in the documentation
  19. // and/or other materials provided with the distribution.
  20. //
  21. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  22. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  23. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  26. // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  28. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. /** @file
  32. * This header contains declaration internal to log4cplus. They must never be
  33. * visible from user accesible headers or exported in DLL/shared library.
  34. */
  35. #ifndef LOG4CPLUS_INTERNAL_CUSTOMLOGLEVELMANAGER_HEADER_
  36. #define LOG4CPLUS_INTERNAL_CUSTOMLOGLEVELMANAGER_HEADER_
  37. #include <log4cplus/config.hxx>
  38. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  39. #pragma once
  40. #endif
  41. #if ! defined (INSIDE_LOG4CPLUS)
  42. # error "This header must not be be used outside log4cplus' implementation files."
  43. #endif
  44. #include <map>
  45. #include <log4cplus/thread/syncprims.h>
  46. #include <log4cplus/internal/internal.h>
  47. namespace log4cplus {
  48. namespace internal {
  49. /**
  50. * Custom log level manager used by C API.
  51. */
  52. class CustomLogLevelManager {
  53. protected:
  54. log4cplus::thread::Mutex mtx;
  55. bool pushed_methods;
  56. std::map<LogLevel,tstring> ll2nm;
  57. std::map<tstring,LogLevel> nm2ll;
  58. public:
  59. CustomLogLevelManager()
  60. : pushed_methods (false)
  61. { }
  62. bool add(LogLevel ll, tstring const &nm)
  63. {
  64. log4cplus::thread::MutexGuard guard (mtx);
  65. if (! pushed_methods)
  66. {
  67. pushed_methods = true;
  68. getLogLevelManager().pushToStringMethod(customToStringMethod);
  69. getLogLevelManager().pushFromStringMethod(customFromStringMethod);
  70. }
  71. auto i = ll2nm.lower_bound(ll);
  72. if( ( i != ll2nm.end() ) && ( i->first == ll ) && ( i->second != nm ) )
  73. return false;
  74. auto j = nm2ll.lower_bound(nm);
  75. if( ( j != nm2ll.end() ) && ( j->first == nm ) && ( j->second != ll ) )
  76. return false;
  77. // there is no else after return
  78. ll2nm.insert( i, std::make_pair(ll, nm) );
  79. nm2ll.insert( j, std::make_pair(nm, ll) );
  80. return true;
  81. }
  82. bool remove(LogLevel ll, tstring const &nm)
  83. {
  84. log4cplus::thread::MutexGuard guard (mtx);
  85. auto i = ll2nm.find(ll);
  86. auto j = nm2ll.find(nm);
  87. if( ( i != ll2nm.end() ) && ( j != nm2ll.end() ) &&
  88. ( i->first == j->second ) && ( i->second == j->first ) ) {
  89. ll2nm.erase(i);
  90. nm2ll.erase(j);
  91. return true;
  92. }
  93. // there is no else after return
  94. return false;
  95. }
  96. protected:
  97. tstring const & customToStringMethodWorker(LogLevel ll)
  98. {
  99. log4cplus::thread::MutexGuard guard (mtx);
  100. auto i = ll2nm.find(ll);
  101. if( i != ll2nm.end() )
  102. return i->second;
  103. return internal::empty_str;
  104. }
  105. LogLevel customFromStringMethodWorker(const tstring& nm)
  106. {
  107. log4cplus::thread::MutexGuard guard (mtx);
  108. auto i = nm2ll.find(nm);
  109. if( i != nm2ll.end() )
  110. return i->second;
  111. return NOT_SET_LOG_LEVEL;
  112. }
  113. LOG4CPLUS_PRIVATE static tstring const & customToStringMethod(LogLevel ll);
  114. LOG4CPLUS_PRIVATE static LogLevel customFromStringMethod(const tstring& nm);
  115. };
  116. LOG4CPLUS_PRIVATE CustomLogLevelManager & getCustomLogLevelManager ();
  117. } // namespace internal
  118. } // namespace log4cplus
  119. #endif // LOG4CPLUS_INTERNAL_CUSTOMLOGLEVELMANAGER_HEADER