LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
Log.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2013-2014 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
33 // System headers
34 #include <mutex>
35 #include <pthread.h>
36 #include <stdexcept>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <vector>
40 
41 // Third-party headers
42 #include <log4cxx/basicconfigurator.h>
43 #include <log4cxx/consoleappender.h>
44 #include <log4cxx/helpers/bytearrayinputstream.h>
45 #include <log4cxx/patternlayout.h>
46 #include <log4cxx/propertyconfigurator.h>
47 #include <log4cxx/xml/domconfigurator.h>
48 
49 // Local headers
50 #include "lsst/log/Log.h"
51 #include "lwpID.h"
52 
53 
54 // Max message length for varargs/printf style logging
55 #define MAX_LOG_MSG_LEN 1024
56 
57 namespace {
58 
59 // name of the env. variable pointing to logging config file
60 const char configEnv[] = "LSST_LOG_CONFIG";
61 
62 // dafault message layout pattern
63 const char layoutPattern[] = "%c %p: %m%n";
64 
65 /*
66  * Configure LOG4CXX from file, file must exist. If file extension is .xml
67  * then DOMConfigurator is used, otherwise PropertyConfigurator is called.
68  *
69  * If file parsing fails then error messages are printed to standard error,
70  * but execution continues. LOG4CXX will likely stay un-configured in this
71  * case.
72  */
73 void configFromFile(std::string const& filename) {
74  // find position of extension
75  size_t dotpos = filename.find_last_of(".");
76  if (dotpos != std::string::npos && filename.compare(dotpos, std::string::npos, ".xml") == 0) {
78  } else {
80  }
81 }
82 
83 /*
84  * Default configuration.
85  *
86  * If LSST_LOG_CONFIG envvar is defined and points to existing file then
87  * use that file to configure (can be either XML or Properties file).
88  * Otherwise use pre-defined configuration - add console appender to root
89  * logger using pattern layout with the above pattern, level set to INFO.
90  *
91  * IF LOG4CXX was initialized already and you want to reset it then call
92  * `log4cxx::BasicConfigurator::resetConfiguration()` first.
93  */
94 void defaultConfig() {
95  // if LSST_LOG_CONFIG is set then use that file
96  if (const char* env = getenv(::configEnv)) {
97  if (env[0] and access(env, R_OK) == 0) {
98  configFromFile(env);
99  return;
100  }
101  }
102 
103  // use pre-defined configuration
104  log4cxx::LogString pattern(layoutPattern);
105  log4cxx::LayoutPtr layout(new log4cxx::PatternLayout(pattern));
106  log4cxx::AppenderPtr appender(new log4cxx::ConsoleAppender(layout));
107  auto root = log4cxx::Logger::getRootLogger();
108  root->addAppender(appender);
109  root->setLevel(log4cxx::Level::getInfo());
110 }
111 
112 // Protects concurrent configuration
113 std::mutex configMutex;
114 
115 // global initialization flag (protected by configMutex)
116 bool initialized = false;
117 
118 /*
119  * This method is called exactly once to initialize LOG4CXX configuration.
120  * If `initialized` is set to true then default configuration is skipped.
121  */
122 log4cxx::LoggerPtr log4cxxInit() {
123 
124  std::lock_guard<std::mutex> lock(configMutex);
125  if (!initialized) {
126  initialized = true;
127  // do default configuration if no one done any configuration yet
128  ::defaultConfig();
129  }
130 
131  // returns root logger to be used as default logger
132  return log4cxx::Logger::getRootLogger();
133 }
134 
135 // List of the MDC initialization functions
136 std::vector<std::function<void()>> mdcInitFunctions;
137 std::mutex mdcInitMutex;
138 
139 // more efficient alternative to pthread_once
140 struct PthreadKey {
141  PthreadKey() {
142  // we don't need destructor for a key
143  pthread_key_create(&key, nullptr);
144  }
145  pthread_key_t key;
146 } pthreadKey;
147 
148 } // namespace
149 
150 
151 namespace lsst {
152 namespace log {
153 
154 
155 // Log class
156 
160 log4cxx::LoggerPtr const& Log::_defaultLogger() {
161 
162  // initialize on the first call (skips initialization if someone else did that)
163  static log4cxx::LoggerPtr _default(::log4cxxInit());
164 
165  return _default;
166 }
167 
178  std::lock_guard<std::mutex> lock(::configMutex);
179 
180  // Make sure other threads know that default configuration is not needed
181  ::initialized = true;
182 
183  // This removes all defined appenders, resets level to DEBUG,
184  // existing loggers are not deleted, only reset.
185  log4cxx::BasicConfigurator::resetConfiguration();
186 
187  // Do default configuration (only if not configured already?)
188  ::defaultConfig();
189 }
190 
201 void Log::configure(std::string const& filename) {
202  std::lock_guard<std::mutex> lock(::configMutex);
203 
204  // Make sure other threads know that default configuration is not needed
205  ::initialized = true;
206 
207  // This removes all defined appenders, resets level to DEBUG,
208  // existing loggers are not deleted, only reset.
209  log4cxx::BasicConfigurator::resetConfiguration();
210 
211  ::configFromFile(filename);
212 }
213 
220 void Log::configure_prop(std::string const& properties) {
221  std::lock_guard<std::mutex> lock(::configMutex);
222 
223  // Make sure other threads know that default configuration is not needed
224  ::initialized = true;
225 
226  // This removes all defined appenders, resets level to DEBUG,
227  // existing loggers are not deleted, only reset.
228  log4cxx::BasicConfigurator::resetConfiguration();
229 
230  std::vector<unsigned char> data(properties.begin(), properties.end());
231  log4cxx::helpers::InputStreamPtr inStream(new log4cxx::helpers::ByteArrayInputStream(data));
232  log4cxx::helpers::Properties prop;
233  prop.load(inStream);
235 }
236 
240 std::string Log::getName() const {
241  std::string name = _logger->getName();
242  if (name == "root") {
243  name.clear();
244  }
245  return name;
246 }
247 
256 Log Log::getLogger(std::string const& loggername) {
257  if (loggername.empty()){
258  return getDefaultLogger();
259  } else {
260  return Log(log4cxx::Logger::getLogger(loggername));
261  }
262 }
263 
275  // put() does not remove existing mapping, to make it less confusing
276  // for clients which expect that MDC() always overwrites existing mapping
277  // we explicitly remove it first if it exists.
278  std::string const oldValue = log4cxx::MDC::get(key);
279  log4cxx::MDC::remove(key);
280  log4cxx::MDC::put(key, value);
281  return oldValue;
282 }
283 
289  log4cxx::MDC::remove(key);
290 }
291 
292 int Log::MDCRegisterInit(std::function<void()> function) {
293 
294  std::lock_guard<std::mutex> lock(mdcInitMutex);
295 
296  // logMsg may have been called already in this thread, to make sure that
297  // this function is executed in this thread call it explicitly
298  function();
299 
300  // store function for later use
301  ::mdcInitFunctions.push_back(std::move(function));
302 
303  // return arbitrary number
304  return 1;
305 }
306 
311 void Log::setLevel(int level) {
312  _logger->setLevel(log4cxx::Level::toLevel(level));
313 }
314 
318 int Log::getLevel() const {
319  log4cxx::LevelPtr level = _logger->getLevel();
320  int levelno = -1;
321  if (level != NULL) {
322  levelno = level->toInt();
323  }
324  return levelno;
325 }
326 
333 bool Log::isEnabledFor(int level) const {
334  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
335  return true;
336  } else {
337  return false;
338  }
339 }
340 
354 Log Log::getChild(std::string const& suffix) const {
355  // strip leading dots and spaces from suffix
356  auto pos = suffix.find_first_not_of(" .");
357  if (pos == std::string::npos) {
358  // empty, just return myself
359  return *this;
360  }
361  std::string name = getName();
362  if (name.empty()) {
363  name = suffix.substr(pos);
364  } else {
365  name += '.';
366  name += suffix.substr(pos);
367  }
368  return getLogger(name);
369 }
370 
374 void Log::log(log4cxx::LevelPtr level,
375  log4cxx::spi::LocationInfo const& location,
376  char const* fmt,
377  ...
378  ) {
379  va_list args;
380  va_start(args, fmt);
381  char msg[MAX_LOG_MSG_LEN];
382  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
383  logMsg(level, location, msg);
384 }
385 
388 void Log::logMsg(log4cxx::LevelPtr level,
389  log4cxx::spi::LocationInfo const& location,
390  std::string const& msg
391  ) {
392 
393  // do one-time per-thread initialization, this was implemented
394  // with thread_local initially but clang on OS X did not support it
395  void *ptr = pthread_getspecific(::pthreadKey.key);
396  if (ptr == nullptr) {
397 
398  // use pointer value as a flag, don't care where it points to
399  ptr = static_cast<void*>(&::pthreadKey);
400  pthread_setspecific(::pthreadKey.key, ptr);
401 
402  std::lock_guard<std::mutex> lock(mdcInitMutex);
403  // call all functions in the MDC init list
404  for (auto& fun: mdcInitFunctions) {
405  fun();
406  }
407  }
408 
409  // forward everything to logger
410  _logger->forcedLog(level, msg, location);
411 }
412 
413 unsigned lwpID() {
414  return detail::lwpID();
415 }
416 
417 }} // namespace lsst::log
uint64_t * ptr
Definition: RangeSet.cc:88
T empty(T... args)
T find_first_not_of(T... args)
This static class includes a variety of methods for interacting with the the logging module...
Definition: Log.h:713
T end(T... args)
STL class.
LSST DM logging module built on log4cxx.
T find_last_of(T... args)
A base class for image defects.
char * data
Definition: BaseRecord.cc:62
def getLevel(loggername)
Key< U > key
Definition: Schema.cc:281
T clear(T... args)
def setLevel(loggername, level)
T move(T... args)
STL class.
unsigned lwpID()
Function which returns LWP ID on platforms which support it.
Definition: Log.cc:413
T begin(T... args)
#define MAX_LOG_MSG_LEN
Definition: Log.cc:55
def isEnabledFor(logger, level)
T substr(T... args)
def configure_prop(properties)
T compare(T... args)
def getLogger(loggername)