LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
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 
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  }
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
std::string
STL class.
lsst::log::Log::MDCRemove
static void MDCRemove(std::string const &key)
Remove the value associated with KEY within the MDC.
Definition: Log.cc:288
lsst::log::Log::getLevel
int getLevel() const
Retrieve the logging threshold.
Definition: Log.cc:318
lsst::meas::algorithms.psfSelectionFromMatchList.args
list args
Definition: psfSelectionFromMatchList.py:27
std::move
T move(T... args)
std::vector
STL class.
lsst::log::Log::isEnabledFor
bool isEnabledFor(int level) const
Return whether the logging threshold of the logger is less than or equal to LEVEL.
Definition: Log.cc:333
std::lock_guard
STL class.
lsst::log::Log::getLogger
static Log getLogger(Log const &logger)
Definition: Log.h:760
std::function
MAX_LOG_MSG_LEN
#define MAX_LOG_MSG_LEN
Definition: Log.cc:55
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
std::string::find_first_not_of
T find_first_not_of(T... args)
lsst::log::Log::logMsg
void logMsg(log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, std::string const &msg)
Method used by LOGS_INFO and similar macros to process a log message.
Definition: Log.cc:388
lsst::log::Log::configure_prop
static void configure_prop(std::string const &properties)
Configures log4cxx using a string containing the list of properties, equivalent to configuring from a...
Definition: Log.cc:220
lsst::log.log.logContinued.configure
def configure(*args)
Definition: logContinued.py:146
data
char * data
Definition: BaseRecord.cc:62
lsst::base.packages.log
log
Definition: packages.py:38
lsst::log::lwpID
unsigned lwpID()
Function which returns LWP ID on platforms which support it.
Definition: Log.cc:413
lsst::log::detail::lwpID
unsigned lwpID()
Definition: lwpID.cc:40
std::string::compare
T compare(T... args)
lsst::log::Log::configure
static void configure()
Explicitly configures log4cxx and initializes logging system.
Definition: Log.cc:177
lsst::log::Log::getName
std::string getName() const
Get the logger name associated with the Log object.
Definition: Log.cc:240
lsst::log::Log::getDefaultLogger
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:754
std::string::find_last_of
T find_last_of(T... args)
lsstDebug.getInfo
getInfo
Definition: lsstDebug.py:87
lsst::log::Log::Log
Log()
Definition: Log.h:719
ptr
uint64_t * ptr
Definition: RangeSet.cc:88
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::log::Log::log
void log(log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, char const *fmt,...)
Method used by LOG_INFO and similar macros to process a log message with variable arguments along wit...
Definition: Log.cc:374
std::string::substr
T substr(T... args)
lsst::log::Log::MDCRegisterInit
static int MDCRegisterInit(std::function< void()> function)
Definition: Log.cc:292
std::string::begin
T begin(T... args)
key
Key< U > key
Definition: Schema.cc:281
std::string::empty
T empty(T... args)
std::mutex
STL class.
lsst::log.log.logContinued.getLogger
def getLogger(loggername)
Definition: logContinued.py:158
std::string::end
T end(T... args)
va_list
lsst::log::Log::MDC
static std::string MDC(std::string const &key, std::string const &value)
Places a KEY/VALUE pair in the Mapped Diagnostic Context (MDC) for the current thread.
Definition: Log.cc:274
lsst::log::Log
This static class includes a variety of methods for interacting with the the logging module.
Definition: Log.h:713
lsst::log::Log::getChild
Log getChild(std::string const &suffix) const
Return a logger which is a descendant to this logger.
Definition: Log.cc:354
lwpID.h
Log.h
LSST DM logging module built on log4cxx.
lsst::log::Log::setLevel
void setLevel(int level)
Set the logging threshold to LEVEL.
Definition: Log.cc:311