log4cplus  2.1.0
customloglevelmanager.h
Go to the documentation of this file.
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 
38 #ifndef LOG4CPLUS_INTERNAL_CUSTOMLOGLEVELMANAGER_HEADER_
39 #define LOG4CPLUS_INTERNAL_CUSTOMLOGLEVELMANAGER_HEADER_
40 
41 #include <log4cplus/config.hxx>
42 
43 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
44 #pragma once
45 #endif
46 
47 #if ! defined (INSIDE_LOG4CPLUS)
48 # error "This header must not be be used outside log4cplus' implementation files."
49 #endif
50 
51 #include <map>
54 
55 
56 namespace log4cplus {
57 
58 namespace internal {
59 
60 
65 protected:
68  std::map<LogLevel,tstring> ll2nm;
69  std::map<tstring,LogLevel> nm2ll;
70 
71 public:
73  : pushed_methods (false)
74  { }
75 
76  bool add(LogLevel ll, tstring const &nm)
77  {
79 
80  if (! pushed_methods)
81  {
82  pushed_methods = true;
85  }
86 
87  auto i = ll2nm.lower_bound(ll);
88  if( ( i != ll2nm.end() ) && ( i->first == ll ) && ( i->second != nm ) )
89  return false;
90 
91  auto j = nm2ll.lower_bound(nm);
92  if( ( j != nm2ll.end() ) && ( j->first == nm ) && ( j->second != ll ) )
93  return false;
94 
95  // there is no else after return
96  ll2nm.insert( i, std::make_pair(ll, nm) );
97  nm2ll.insert( j, std::make_pair(nm, ll) );
98  return true;
99  }
100 
101  bool remove(LogLevel ll, tstring const &nm)
102  {
104 
105  auto i = ll2nm.find(ll);
106  auto j = nm2ll.find(nm);
107  if( ( i != ll2nm.end() ) && ( j != nm2ll.end() ) &&
108  ( i->first == j->second ) && ( i->second == j->first ) ) {
109  ll2nm.erase(i);
110  nm2ll.erase(j);
111 
112  return true;
113  }
114 
115  // there is no else after return
116  return false;
117  }
118 
119 protected:
121  {
123  auto i = ll2nm.find(ll);
124  if( i != ll2nm.end() )
125  return i->second;
126 
127  return internal::empty_str;
128  }
129 
131  {
133  auto i = nm2ll.find(nm);
134  if( i != nm2ll.end() )
135  return i->second;
136 
137  return NOT_SET_LOG_LEVEL;
138  }
139 
142 };
143 
145 
146 } // namespace internal
147 
148 } // namespace log4cplus
149 
150 
151 #endif // LOG4CPLUS_INTERNAL_CUSTOMLOGLEVELMANAGER_HEADER
void pushFromStringMethod(StringToLogLevelMethod newFromString)
When creating a "derived" LogLevel, a StringToLogLevelMethod should be defined and registered with th...
void pushToStringMethod(LogLevelToStringMethod newToString)
When creating a "derived" LogLevel, a LogLevelToStringMethod should be defined and registered with th...
Custom log level manager used by C API.
static LOG4CPLUS_PRIVATE tstring const & customToStringMethod(LogLevel ll)
LogLevel customFromStringMethodWorker(const tstring &nm)
static LOG4CPLUS_PRIVATE LogLevel customFromStringMethod(const tstring &nm)
bool remove(LogLevel ll, tstring const &nm)
bool add(LogLevel ll, tstring const &nm)
tstring const & customToStringMethodWorker(LogLevel ll)
#define LOG4CPLUS_PRIVATE
Definition: config.hxx:53
This header contains declaration internal to log4cplus.
log4cplus::tstring const empty_str
Canonical empty string.
LOG4CPLUS_PRIVATE CustomLogLevelManager & getCustomLogLevelManager()
std::basic_string< tchar > tstring
Definition: tstring.h:39
const LogLevel NOT_SET_LOG_LEVEL
The NOT_SET_LOG_LEVEL LogLevel is used to indicated that no particular LogLevel is desired and that t...
Definition: loglevel.h:95
LOG4CPLUS_EXPORT LogLevelManager & getLogLevelManager()
Returns the singleton LogLevelManager.
int LogLevel
Defines the minimum set of priorities recognized by the system, that is FATAL_LOG_LEVEL,...
Definition: loglevel.h:48