LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
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 
274 std::string Log::MDC(std::string const& key, std::string const& value) {
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 
288 void Log::MDCRemove(std::string const& key) {
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 
331  log4cxx::LevelPtr level = _logger->getEffectiveLevel();
332  int levelno = -1;
333  if (level != NULL) {
334  levelno = level->toInt();
335  }
336  return levelno;
337 }
338 
345 bool Log::isEnabledFor(int level) const {
346  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
347  return true;
348  } else {
349  return false;
350  }
351 }
352 
366 Log Log::getChild(std::string const& suffix) const {
367  // strip leading dots and spaces from suffix
368  auto pos = suffix.find_first_not_of(" .");
369  if (pos == std::string::npos) {
370  // empty, just return myself
371  return *this;
372  }
374  if (name.empty()) {
375  name = suffix.substr(pos);
376  } else {
377  name += '.';
378  name += suffix.substr(pos);
379  }
380  return getLogger(name);
381 }
382 
386 void Log::log(log4cxx::LevelPtr level,
387  log4cxx::spi::LocationInfo const& location,
388  char const* fmt,
389  ...
390  ) {
391  va_list args;
392  va_start(args, fmt);
393  char msg[MAX_LOG_MSG_LEN];
394  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
395  logMsg(level, location, msg);
396 }
397 
400 void Log::logMsg(log4cxx::LevelPtr level,
401  log4cxx::spi::LocationInfo const& location,
402  std::string const& msg
403  ) {
404 
405  // do one-time per-thread initialization, this was implemented
406  // with thread_local initially but clang on OS X did not support it
407  void *ptr = pthread_getspecific(::pthreadKey.key);
408  if (ptr == nullptr) {
409 
410  // use pointer value as a flag, don't care where it points to
411  ptr = static_cast<void*>(&::pthreadKey);
412  pthread_setspecific(::pthreadKey.key, ptr);
413 
414  std::lock_guard<std::mutex> lock(mdcInitMutex);
415  // call all functions in the MDC init list
416  for (auto& fun: mdcInitFunctions) {
417  fun();
418  }
419  }
420 
421  // forward everything to logger
422  _logger->forcedLog(level, msg, location);
423 }
424 
425 unsigned lwpID() {
426  return detail::lwpID();
427 }
428 
429 }} // namespace lsst::log
table::Key< std::string > name
Definition: Amplifier.cc:116
char * data
Definition: BaseRecord.cc:61
LSST DM logging module built on log4cxx.
uint64_t * ptr
Definition: RangeSet.cc:88
T begin(T... args)
This static class includes a variety of methods for interacting with the the logging module.
Definition: Log.h:724
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:766
static Log getLogger(Log const &logger)
Definition: Log.h:772
static void MDCRemove(std::string const &key)
Remove the value associated with KEY within the MDC.
Definition: Log.cc:288
int getLevel() const
Retrieve the logging threshold.
Definition: Log.cc:318
bool isEnabledFor(int level) const
Return whether the logging threshold of the logger is less than or equal to LEVEL.
Definition: Log.cc:345
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:400
Log getChild(std::string const &suffix) const
Return a logger which is a descendant to this logger.
Definition: Log.cc:366
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
void setLevel(int level)
Set the logging threshold to LEVEL.
Definition: Log.cc:311
static void configure()
Explicitly configures log4cxx and initializes logging system.
Definition: Log.cc:177
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
static int MDCRegisterInit(std::function< void()> function)
Definition: Log.cc:292
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:386
std::string getName() const
Get the logger name associated with the Log object.
Definition: Log.cc:240
int getEffectiveLevel() const
Retrieve the effective logging threshold.
Definition: Log.cc:330
T compare(T... args)
T empty(T... args)
T end(T... args)
T find_first_not_of(T... args)
T find_last_of(T... args)
T move(T... args)
unsigned lwpID()
Definition: lwpID.cc:40
def getLogger(loggername)
unsigned lwpID()
Function which returns LWP ID on platforms which support it.
Definition: Log.cc:425
A base class for image defects.
#define MAX_LOG_MSG_LEN
Definition: Log.cc:55
T substr(T... args)