Kea  1.5.0
logger_impl.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2017 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <algorithm>
12 
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <cstring>
16 #include <sstream>
17 #include <boost/lexical_cast.hpp>
18 #include <boost/static_assert.hpp>
19 #include <boost/algorithm/string.hpp>
20 
21 #include <log4cplus/version.h>
22 #include <log4cplus/configurator.h>
23 #include <log4cplus/loggingmacros.h>
24 
25 #include <log/logger.h>
26 #include <log/logger_impl.h>
27 #include <log/logger_level.h>
28 #include <log/logger_level_impl.h>
29 #include <log/logger_name.h>
30 #include <log/logger_manager.h>
31 #include <log/message_dictionary.h>
32 #include <log/message_types.h>
35 
36 #include <util/strutil.h>
37 
38 // Note: as log4cplus and the Kea logger have many concepts in common, and
39 // thus many similar names, to disambiguate types we don't "use" the log4cplus
40 // namespace: instead, all log4cplus types are explicitly qualified.
41 
42 using namespace std;
43 
44 namespace isc {
45 namespace log {
46 
53  const char* const env = getenv("KEA_LOCKFILE_DIR");
54  if (env && boost::iequals(string(env), string("none"))) {
55  return (false);
56  }
57 
58  return (true);
59 }
60 
61 // Constructor. The setting of logger_ must be done when the variable is
62 // constructed (instead of being left to the body of the function); at least
63 // one compiler requires that all member variables be constructed before the
64 // constructor is run, but log4cplus::Logger (the type of logger_) has no
65 // default constructor.
66 LoggerImpl::LoggerImpl(const string& name) :
67  name_(expandLoggerName(name)),
68  logger_(log4cplus::Logger::getInstance(name_))
69 {
70  if (lockfileEnabled()) {
71  sync_ = new interprocess::InterprocessSyncFile("logger");
72  } else {
73  sync_ = new interprocess::InterprocessSyncNull("logger");
74  }
75 }
76 
77 // Destructor. (Here because of virtual declaration.)
78 
80  delete sync_;
81 }
82 
84 std::string
86  std::ostringstream ver;
87  ver << "log4cplus ";
88  ver << log4cplus::versionStr;
89  return (ver.str());
90 }
91 
92 // Set the severity for logging.
93 void
95  Level level(severity, dbglevel);
96  logger_.setLogLevel(LoggerLevelImpl::convertFromBindLevel(level));
97 }
98 
99 // Return severity level
102  Level level = LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
103  return level.severity;
104 }
105 
106 // Return current debug level (only valid if current severity level is DEBUG).
107 int
109  Level level = LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
110  return level.dbglevel;
111 }
112 
113 // Get effective severity. Either the current severity or, if not set, the
114 // severity of the root level.
117  Level level = LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
118  return level.severity;
119 }
120 
121 // Return effective debug level (only valid if current effective severity level
122 // is DEBUG).
123 int
125  Level level = LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
126  return level.dbglevel;
127 }
128 
129 
130 // Output a general message
131 string*
133  return (new string(string(ident) + " " +
134  MessageDictionary::globalDictionary()->getText(ident)));
135 }
136 
137 // Replace the interprocess synchronization object
138 
139 void
141 {
142  if (sync == NULL) {
144  "NULL was passed to setInterprocessSync()");
145  }
146 
147  delete sync_;
148  sync_ = sync;
149 }
150 
151 void
152 LoggerImpl::outputRaw(const Severity& severity, const string& message) {
153  // Use a mutex locker for mutual exclusion from other threads in
154  // this process.
156 
157  // Use an interprocess sync locker for mutual exclusion from other
158  // processes to avoid log messages getting interspersed.
160 
161  if (!locker.lock()) {
162  LOG4CPLUS_ERROR(logger_, "Unable to lock logger lockfile");
163  }
164 
165  switch (severity) {
166  case DEBUG:
167  LOG4CPLUS_DEBUG(logger_, message);
168  break;
169 
170  case INFO:
171  LOG4CPLUS_INFO(logger_, message);
172  break;
173 
174  case WARN:
175  LOG4CPLUS_WARN(logger_, message);
176  break;
177 
178  case ERROR:
179  LOG4CPLUS_ERROR(logger_, message);
180  break;
181 
182  case FATAL:
183  LOG4CPLUS_FATAL(logger_, message);
184  break;
185 
186  case NONE:
187  break;
188 
189  default:
190  LOG4CPLUS_ERROR(logger_,
191  "Unsupported severity in LoggerImpl::outputRaw(): "
192  << severity);
193  }
194 
195  if (!locker.unlock()) {
196  LOG4CPLUS_ERROR(logger_, "Unable to unlock logger lockfile");
197  }
198 }
199 
200 } // namespace log
201 } // namespace isc
Logger Class.
Definition: log/logger.h:143
static const MessageDictionaryPtr & globalDictionary()
Return Global Dictionary.
static isc::util::thread::Mutex & getMutex()
Return a process-global mutex that's used for mutual exclusion among threads of a single process duri...
virtual Severity getEffectiveSeverity()
Get Effective Severity Level for Logger.
Definition: logger_impl.cc:116
Severity severity
Logging severity.
Definition: logger_level.h:43
Bad Interprocess Sync.
Definition: log/logger.h:93
static log4cplus::LogLevel convertFromBindLevel(const isc::log::Level &level)
Convert Kea level to log4cplus logging level.
This holds a lock on a Mutex.
Definition: sync.h:72
std::string * lookupMessage(const MessageID &id)
Look up message text in dictionary.
Definition: logger_impl.cc:132
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
virtual ~LoggerImpl()
Destructor.
Definition: logger_impl.cc:79
int dbglevel
Debug level.
Definition: logger_level.h:44
Severity
Severity Levels.
Definition: logger_level.h:23
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: logger_impl.cc:140
static isc::log::Level convertToBindLevel(const log4cplus::LogLevel loglevel)
Convert log4cplus logging level to Kea logging level.
std::string expandLoggerName(const std::string &name)
Expand logger name.
Definition: logger_name.cc:42
Defines the logger used by the top-level component of kea-dhcp-ddns.
Log level structure.
Definition: logger_level.h:42
bool lockfileEnabled()
detects whether file locking is enabled or disabled
Definition: logger_impl.cc:52
const Name & name_
Definition: dns/message.cc:693
static std::string getVersion()
Version.
Definition: logger_impl.cc:85
virtual int getDebugLevel()
Return debug level.
Definition: logger_impl.cc:108
virtual void setSeverity(Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition: logger_impl.cc:94
void outputRaw(const Severity &severity, const std::string &message)
Raw output.
Definition: logger_impl.cc:152
const char * MessageID
Definition: message_types.h:15
virtual Severity getSeverity()
Get Severity Level for Logger.
Definition: logger_impl.cc:101
bool lock()
Acquire the lock (blocks if something else has acquired a lock on the same task name)
virtual int getEffectiveDebugLevel()
Return effective debug level.
Definition: logger_impl.cc:124