LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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 
273 void Log::MDC(std::string const& key, std::string const& value) {
274  // put() does not remove existing mapping, to make it less confusing
275  // for clients which expect that MDC() always overwrites existing mapping
276  // we explicitly remove it first if it exists.
277  log4cxx::MDC::remove(key);
278  log4cxx::MDC::put(key, value);
279 }
280 
286  log4cxx::MDC::remove(key);
287 }
288 
289 int Log::MDCRegisterInit(std::function<void()> function) {
290 
291  std::lock_guard<std::mutex> lock(mdcInitMutex);
292 
293  // logMsg may have been called already in this thread, to make sure that
294  // this function is executed in this thread call it explicitly
295  function();
296 
297  // store function for later use
298  ::mdcInitFunctions.push_back(std::move(function));
299 
300  // return arbitrary number
301  return 1;
302 }
303 
308 void Log::setLevel(int level) {
309  _logger->setLevel(log4cxx::Level::toLevel(level));
310 }
311 
315 int Log::getLevel() const {
316  log4cxx::LevelPtr level = _logger->getLevel();
317  int levelno = -1;
318  if (level != NULL) {
319  levelno = level->toInt();
320  }
321  return levelno;
322 }
323 
330 bool Log::isEnabledFor(int level) const {
331  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
332  return true;
333  } else {
334  return false;
335  }
336 }
337 
351 Log Log::getChild(std::string const& suffix) const {
352  // strip leading dots and spaces from suffix
353  auto pos = suffix.find_first_not_of(" .");
354  if (pos == std::string::npos) {
355  // empty, just return myself
356  return *this;
357  }
358  std::string name = getName();
359  if (name.empty()) {
360  name = suffix.substr(pos);
361  } else {
362  name += '.';
363  name += suffix.substr(pos);
364  }
365  return getLogger(name);
366 }
367 
371 void Log::log(log4cxx::LevelPtr level,
372  log4cxx::spi::LocationInfo const& location,
373  char const* fmt,
374  ...
375  ) {
376  va_list args;
377  va_start(args, fmt);
378  char msg[MAX_LOG_MSG_LEN];
379  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
380  logMsg(level, location, msg);
381 }
382 
385 void Log::logMsg(log4cxx::LevelPtr level,
386  log4cxx::spi::LocationInfo const& location,
387  std::string const& msg
388  ) {
389 
390  // do one-time per-thread initialization, this was implemented
391  // with thread_local initially but clang on OS X did not support it
392  void *ptr = pthread_getspecific(::pthreadKey.key);
393  if (ptr == nullptr) {
394 
395  // use pointer value as a flag, don't care where it points to
396  ptr = static_cast<void*>(&::pthreadKey);
397  pthread_setspecific(::pthreadKey.key, ptr);
398 
399  std::lock_guard<std::mutex> lock(mdcInitMutex);
400  // call all functions in the MDC init list
401  for (auto& fun: mdcInitFunctions) {
402  fun();
403  }
404  }
405 
406  // forward everything to logger
407  _logger->forcedLog(level, msg, location);
408 }
409 
410 unsigned lwpID() {
411  return detail::lwpID();
412 }
413 
414 }} // namespace lsst::log
uint64_t * ptr
Definition: RangeSet.cc:88
T empty(T... args)
char * data
Definition: BaseTable.cc:205
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:698
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.
def getLevel(loggername)
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:410
T begin(T... args)
Key< U > key
Definition: Schema.cc:281
#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)