Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Log Class Reference

Global static Log class. More...

#include <ored/utilities/log.hpp>

+ Inheritance diagram for Log:
+ Collaboration diagram for Log:

Public Member Functions

void registerLogger (const QuantLib::ext::shared_ptr< Logger > &logger)
 Add a new Logger. More...
 
void registerIndependentLogger (const QuantLib::ext::shared_ptr< IndependentLogger > &logger)
 
void clearAllIndependentLoggers ()
 
const bool hasLogger (const std::string &name) const
 Check if logger exists. More...
 
const bool hasIndependentLogger (const std::string &name) const
 
QuantLib::ext::shared_ptr< Logger > & logger (const std::string &name)
 Retrieve a Logger. More...
 
QuantLib::ext::shared_ptr< IndependentLogger > & independentLogger (const std::string &name)
 
void removeLogger (const std::string &name)
 Remove a Logger. More...
 
void removeIndependentLogger (const std::string &name)
 
void removeAllLoggers ()
 Remove all loggers. More...
 
void addExcludeFilter (const std::string &, const std::function< bool(const std::string &)>)
 
void removeExcludeFilter (const std::string &)
 
bool checkExcludeFilters (const std::string &)
 
void header (unsigned m, const char *filename, int lineNo)
 macro utility function - do not use directly, not thread safe More...
 
std::ostream & logStream ()
 macro utility function - do not use directly, not thread safe More...
 
void log (unsigned m)
 macro utility function - do not use directly, not thread safe More...
 
boost::shared_mutex & mutex ()
 mutex to acquire locks More...
 
bool filter (unsigned mask)
 
unsigned mask ()
 
void setMask (unsigned mask)
 
const boost::filesystem::path & rootPath ()
 
void setRootPath (const boost::filesystem::path &pth)
 
int maxLen ()
 
void setMaxLen (const int n)
 
bool enabled ()
 
void switchOn ()
 
void switchOff ()
 
bool writeSuppressedMessagesHint ()
 
void setPid (const int pid)
 if a PID is set for the logger, messages are tagged with [1234] if pid = 1234 More...
 

Private Member Functions

 Log ()
 
std::string source (const char *filename, int lineNo) const
 

Private Attributes

std::map< std::string, QuantLib::ext::shared_ptr< Logger > > loggers_
 
std::map< std::string, QuantLib::ext::shared_ptr< IndependentLogger > > independentLoggers_
 
bool enabled_
 
unsigned mask_
 
boost::filesystem::path rootPath_
 
std::ostringstream ls_
 
int maxLen_ = 45
 
std::size_t sameSourceLocationSince_ = 0
 
bool writeSuppressedMessagesHint_ = true
 
std::size_t sameSourceLocationCutoff_ = 1000
 
std::string lastFileName_
 
int lastLineNo_ = 0
 
int pid_ = 0
 
boost::shared_mutex mutex_
 
std::map< std::string, std::function< bool(const std::string &)> > excludeFilters_
 

Friends

class QuantLib::Singleton< Log, std::integral_constant< bool, true > >
 

Detailed Description

Global static Log class.

The Global Log class gets registered with individual loggers and receives application log messages. Once a message is received, it is immediately dispatched to each of the registered loggers, the order in which the loggers are called is not guaranteed.

Logging is done by the calling thread and the LOG call blocks until all the loggers have returned.

At start up, the Log class has no loggers and so will ignore any LOG() messages until it is configured.

To configure the Log class to log to a file "/tmp/my_log.txt"

    Log::instance().removeAllLoggers();
    Log::instance().registerLogger(QuantLib::ext::shared_ptr<Logger>(new FileLogger("/tmp/my_log.txt")));

To change the Log class to only use a BufferLogger the user must call

    Log::instance().removeAllLoggers();
    Log::instance().registerLogger(QuantLib::ext::shared_ptr<Logger>(new BufferLogger));

and then to retrieve log messages from the buffer and print them to stdout the user must call:

    std::cout << "Begin Log Messages:" << std::endl;

    QuantLib::ext::shared_ptr<BufferLogger> bl = QuantLib::ext::dynamic_pointer_cast<BufferLogger>
      (Log::instance().logger(BufferLogger::name));

    while (bl.hasNext())
        std::cout << bl.next() << std::endl;
    std::cout << "End Log Messages." << std::endl;

Definition at line 389 of file log.hpp.

Constructor & Destructor Documentation

◆ Log()

Log ( )
private

Definition at line 272 of file log.cpp.

272 : loggers_(), enabled_(false), mask_(255), ls_() {
273
274 ls_.setf(ios::fixed, ios::floatfield);
275 ls_.setf(ios::showpoint);
276}
unsigned mask_
Definition: log.hpp:508
std::ostringstream ls_
Definition: log.hpp:510
std::map< std::string, QuantLib::ext::shared_ptr< Logger > > loggers_
Definition: log.hpp:505
bool enabled_
Definition: log.hpp:507

Member Function Documentation

◆ registerLogger()

void registerLogger ( const QuantLib::ext::shared_ptr< Logger > &  logger)

Add a new Logger.

Adds a new logger to the Log class, the logger will be stored by it's Logger::name(). This method will throw if a logger with the same name is already registered.

Parameters
loggerthe logger to add

Definition at line 278 of file log.cpp.

278 {
279 boost::unique_lock<boost::shared_mutex> lock(mutex_);
280 QL_REQUIRE(loggers_.find(logger->name()) == loggers_.end(),
281 "Logger with name " << logger->name() << " already registered");
282 loggers_[logger->name()] = logger;
283}
QuantLib::ext::shared_ptr< Logger > & logger(const std::string &name)
Retrieve a Logger.
Definition: log.cpp:303
boost::shared_mutex mutex_
Definition: log.hpp:521
+ Here is the call graph for this function:

◆ registerIndependentLogger()

void registerIndependentLogger ( const QuantLib::ext::shared_ptr< IndependentLogger > &  logger)

Definition at line 285 of file log.cpp.

285 {
286 boost::unique_lock<boost::shared_mutex> lock(mutex_);
287 QL_REQUIRE(independentLoggers_.find(logger->name()) == independentLoggers_.end(),
288 "Logger with name " << logger->name() << " already registered as independent logger");
290}
std::map< std::string, QuantLib::ext::shared_ptr< IndependentLogger > > independentLoggers_
Definition: log.hpp:506
+ Here is the call graph for this function:

◆ clearAllIndependentLoggers()

void clearAllIndependentLoggers ( )

Definition at line 292 of file log.cpp.

292 {
293 boost::unique_lock<boost::shared_mutex> lock(mutex_);
294 for (auto& [_, l] : independentLoggers_)
295 l->clear();
296}

◆ hasLogger()

const bool hasLogger ( const std::string &  name) const

Check if logger exists.

Definition at line 298 of file log.cpp.

298 {
299 boost::shared_lock<boost::shared_mutex> lock(mutex_);
300 return loggers_.find(name) != loggers_.end();
301}
string name
+ Here is the caller graph for this function:

◆ hasIndependentLogger()

const bool hasIndependentLogger ( const std::string &  name) const

Definition at line 309 of file log.cpp.

309 {
310 boost::shared_lock<boost::shared_mutex> lock(mutex_);
311 return independentLoggers_.find(name) != independentLoggers_.end();
312}
+ Here is the caller graph for this function:

◆ logger()

QuantLib::ext::shared_ptr< Logger > & logger ( const std::string &  name)

Retrieve a Logger.

Retrieve a Logger by it's name, for example to retrieve the StderrLogger (assuming it is registered)

QuantLib::ext::shared_ptr<Logger> slogger = Log::instance().logger(StderrLogger::name);

Definition at line 303 of file log.cpp.

303 {
304 boost::shared_lock<boost::shared_mutex> lock(mutex_);
305 QL_REQUIRE(hasLogger(name), "No logger found with name " << name);
306 return loggers_[name];
307}
const bool hasLogger(const std::string &name) const
Check if logger exists.
Definition: log.cpp:298
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ independentLogger()

QuantLib::ext::shared_ptr< IndependentLogger > & independentLogger ( const std::string &  name)

Definition at line 314 of file log.cpp.

314 {
315 boost::shared_lock<boost::shared_mutex> lock(mutex_);
316 QL_REQUIRE(hasIndependentLogger(name), "No independent logger found with name " << name);
318}
const bool hasIndependentLogger(const std::string &name) const
Definition: log.cpp:309
+ Here is the call graph for this function:

◆ removeLogger()

void removeLogger ( const std::string &  name)

Remove a Logger.

Remove a logger by name

Parameters
namethe logger name

Definition at line 320 of file log.cpp.

320 {
321 boost::unique_lock<boost::shared_mutex> lock(mutex_);
322 map<string, QuantLib::ext::shared_ptr<Logger>>::iterator it = loggers_.find(name);
323 if (it != loggers_.end()) {
324 loggers_.erase(it);
325 } else {
326 QL_FAIL("No logger found with name " << name);
327 }
328}

◆ removeIndependentLogger()

void removeIndependentLogger ( const std::string &  name)

Definition at line 330 of file log.cpp.

330 {
331 boost::unique_lock<boost::shared_mutex> lock(mutex_);
332 map<string, QuantLib::ext::shared_ptr<IndependentLogger>>::iterator it = independentLoggers_.find(name);
333 if (it != independentLoggers_.end()) {
334 it->second->removeSinks();
335 independentLoggers_.erase(it);
336 } else {
337 QL_FAIL("No independdent logger found with name " << name);
338 }
339}

◆ removeAllLoggers()

void removeAllLoggers ( )

Remove all loggers.

Removes all loggers. If called, all subsequent log messages will be ignored.

Definition at line 341 of file log.cpp.

341 {
342 boost::unique_lock<boost::shared_mutex> lock(mutex_);
343 loggers_.clear();
344 logging::core::get()->remove_all_sinks();
345 independentLoggers_.clear();
346}

◆ addExcludeFilter()

void addExcludeFilter ( const std::string &  key,
const std::function< bool(const std::string &)>  func 
)

Definition at line 372 of file log.cpp.

372 {
373 boost::unique_lock<boost::shared_mutex> lock(mutex_);
374 excludeFilters_[key] = func;
375}
std::map< std::string, std::function< bool(const std::string &)> > excludeFilters_
Definition: log.hpp:523

◆ removeExcludeFilter()

void removeExcludeFilter ( const std::string &  key)

Definition at line 377 of file log.cpp.

377 {
378 boost::unique_lock<boost::shared_mutex> lock(mutex_);
379 excludeFilters_.erase(key);
380}

◆ checkExcludeFilters()

bool checkExcludeFilters ( const std::string &  msg)

Definition at line 382 of file log.cpp.

382 {
383 boost::shared_lock<boost::shared_mutex> lock(mutex_);
384 for (const auto& f : excludeFilters_) {
385 if (f.second(msg))
386 return true;
387 }
388 return false;
389}

◆ header()

void header ( unsigned  m,
const char *  filename,
int  lineNo 
)

macro utility function - do not use directly, not thread safe

Definition at line 391 of file log.cpp.

391 {
392 // 1. Reset stringstream
393 ls_.str(string());
394 ls_.clear();
395
396 // Write the header to the stream
397 // TYPE [Time Stamp] (file:line)
398 switch (m) {
399 case ORE_ALERT:
400 ls_ << "ALERT ";
401 break;
402 case ORE_CRITICAL:
403 ls_ << "CRITICAL ";
404 break;
405 case ORE_ERROR:
406 ls_ << "ERROR ";
407 break;
408 case ORE_WARNING:
409 ls_ << "WARNING ";
410 break;
411 case ORE_NOTICE:
412 ls_ << "NOTICE ";
413 break;
414 case ORE_DEBUG:
415 ls_ << "DEBUG ";
416 break;
417 case ORE_DATA:
418 ls_ << "DATA ";
419 break;
420 case ORE_MEMORY:
421 ls_ << "MEMORY ";
422 break;
423 }
424
425 // Timestamp
426 // Use boost::posix_time microsecond clock to get better precision (when available).
427 // format is "2014-Apr-04 11:10:16.179347"
428 ls_ << '[' << to_simple_string(microsec_clock::local_time()) << ']';
429
430 // Filename & line no
431 // format is " (file:line)"
432 ls_ << " " << source(filename, lineNo) << " : ";
433
434 // log pid if given
435 if (pid_ > 0)
436 ls_ << " [" << pid_ << "] ";
437
438 // update statistics
439 if (lastLineNo_ == lineNo && lastFileName_ == filename) {
441 } else {
442 lastFileName_ = filename;
443 lastLineNo_ = lineNo;
446 }
447}
int lastLineNo_
Definition: log.hpp:517
std::size_t sameSourceLocationSince_
Definition: log.hpp:513
std::string source(const char *filename, int lineNo) const
Definition: log.cpp:348
std::string lastFileName_
Definition: log.hpp:516
bool writeSuppressedMessagesHint_
Definition: log.hpp:514
#define ORE_DEBUG
Definition: log.hpp:32
#define ORE_CRITICAL
Definition: log.hpp:28
#define ORE_MEMORY
Definition: log.hpp:34
#define ORE_DATA
Definition: log.hpp:33
#define ORE_NOTICE
Definition: log.hpp:31
#define ORE_ERROR
Definition: log.hpp:29
#define ORE_WARNING
Definition: log.hpp:30
#define ORE_ALERT
Definition: log.hpp:27
+ Here is the call graph for this function:

◆ logStream()

std::ostream & logStream ( )

macro utility function - do not use directly, not thread safe

Definition at line 441 of file log.hpp.

441{ return ls_; }

◆ log()

void log ( unsigned  m)

macro utility function - do not use directly, not thread safe

Definition at line 449 of file log.cpp.

449 {
450 string msg = ls_.str();
451 map<string, QuantLib::ext::shared_ptr<Logger>>::iterator it;
453 for (auto& l : loggers_) {
454 l.second->log(m, msg);
455 }
456 } else if (writeSuppressedMessagesHint_) {
457 std::string suffix;
458 if (msg.find(StructuredMessage::name) == string::npos) {
459 suffix = " ... suppressing more messages from same source code location (cutoff = " +
460 std::to_string(sameSourceLocationCutoff_) + " lines)";
461 }
462 for (auto& l : loggers_) {
463 l.second->log(m, msg + suffix);
464 }
466 }
467}
std::size_t sameSourceLocationCutoff_
Definition: log.hpp:515
static constexpr const char * name
Definition: log.hpp:692

◆ mutex()

boost::shared_mutex & mutex ( )

mutex to acquire locks

Definition at line 446 of file log.hpp.

446{ return mutex_; }
+ Here is the caller graph for this function:

◆ filter()

bool filter ( unsigned  mask)

Definition at line 449 of file log.hpp.

449 {
450 boost::shared_lock<boost::shared_mutex> lock(mutex());
451 return 0 != (mask & mask_);
452 }
boost::shared_mutex & mutex()
mutex to acquire locks
Definition: log.hpp:446
unsigned mask()
Definition: log.hpp:453
+ Here is the call graph for this function:

◆ mask()

unsigned mask ( )

Definition at line 453 of file log.hpp.

453 {
454 boost::shared_lock<boost::shared_mutex> lock(mutex());
455 return mask_;
456 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setMask()

void setMask ( unsigned  mask)

Definition at line 457 of file log.hpp.

457 {
458 boost::unique_lock<boost::shared_mutex> lock(mutex());
459 mask_ = mask;
460 }
+ Here is the call graph for this function:

◆ rootPath()

const boost::filesystem::path & rootPath ( )

Definition at line 461 of file log.hpp.

461 {
462 boost::shared_lock<boost::shared_mutex> lock(mutex());
463 return rootPath_;
464 }
boost::filesystem::path rootPath_
Definition: log.hpp:509
+ Here is the call graph for this function:

◆ setRootPath()

void setRootPath ( const boost::filesystem::path &  pth)

Definition at line 465 of file log.hpp.

465 {
466 boost::unique_lock<boost::shared_mutex> lock(mutex());
467 rootPath_ = pth;
468 }
+ Here is the call graph for this function:

◆ maxLen()

int maxLen ( )

Definition at line 469 of file log.hpp.

469 {
470 boost::shared_lock<boost::shared_mutex> lock(mutex());
471 return maxLen_;
472 }
int maxLen_
Definition: log.hpp:512
+ Here is the call graph for this function:

◆ setMaxLen()

void setMaxLen ( const int  n)

Definition at line 473 of file log.hpp.

473 {
474 boost::unique_lock<boost::shared_mutex> lock(mutex());
475 maxLen_ = n;
476 }
+ Here is the call graph for this function:

◆ enabled()

bool enabled ( )

Definition at line 478 of file log.hpp.

478 {
479 boost::shared_lock<boost::shared_mutex> lock(mutex());
480 return enabled_;
481 }
+ Here is the call graph for this function:

◆ switchOn()

void switchOn ( )

Definition at line 482 of file log.hpp.

482 {
483 boost::unique_lock<boost::shared_mutex> lock(mutex());
484 enabled_ = true;
485 }
+ Here is the call graph for this function:

◆ switchOff()

void switchOff ( )

Definition at line 486 of file log.hpp.

486 {
487 boost::unique_lock<boost::shared_mutex> lock(mutex());
488 enabled_ = false;
489 }
+ Here is the call graph for this function:

◆ writeSuppressedMessagesHint()

bool writeSuppressedMessagesHint ( )

Definition at line 491 of file log.hpp.

491 {
492 boost::shared_lock<boost::shared_mutex> lock(mutex());
494 }
+ Here is the call graph for this function:

◆ setPid()

void setPid ( const int  pid)

if a PID is set for the logger, messages are tagged with [1234] if pid = 1234

Definition at line 497 of file log.hpp.

497{ pid_ = pid; }

◆ source()

string source ( const char *  filename,
int  lineNo 
) const
private

Definition at line 348 of file log.cpp.

348 {
349 string filepath;
350 if (rootPath_.empty()) {
351 filepath = filename;
352 } else {
353 filepath = relative(path(filename), rootPath_).string();
354 }
355 int lineNoLen = (int)log10((double)lineNo) + 1; // Length of the int as a string
356 int len = 2 + filepath.length() + 1 + lineNoLen + 1; // " (" + file + ':' + line + ')'
357
358 if (maxLen_ == 0) {
359 return "(" + filepath + ':' + to_string(lineNo) + ')';
360 } else {
361 if (len <= maxLen_) {
362 // pad out spaces
363 return string(maxLen_ - len, ' ') + "(" + filepath + ':' + to_string(lineNo) + ')';
364 } else {
365 // need to trim the filename to fit into maxLen chars
366 // need to remove (len - maxLen_) chars + 3 for the "..."
367 return "(..." + filepath.substr(3 + len - maxLen_) + ':' + to_string(lineNo) + ')';
368 }
369 }
370}
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ QuantLib::Singleton< Log, std::integral_constant< bool, true > >

friend class QuantLib::Singleton< Log, std::integral_constant< bool, true > >
friend

Definition at line 352 of file log.hpp.

Member Data Documentation

◆ loggers_

std::map<std::string, QuantLib::ext::shared_ptr<Logger> > loggers_
private

Definition at line 505 of file log.hpp.

◆ independentLoggers_

std::map<std::string, QuantLib::ext::shared_ptr<IndependentLogger> > independentLoggers_
private

Definition at line 506 of file log.hpp.

◆ enabled_

bool enabled_
private

Definition at line 507 of file log.hpp.

◆ mask_

unsigned mask_
private

Definition at line 508 of file log.hpp.

◆ rootPath_

boost::filesystem::path rootPath_
private

Definition at line 509 of file log.hpp.

◆ ls_

std::ostringstream ls_
private

Definition at line 510 of file log.hpp.

◆ maxLen_

int maxLen_ = 45
private

Definition at line 512 of file log.hpp.

◆ sameSourceLocationSince_

std::size_t sameSourceLocationSince_ = 0
private

Definition at line 513 of file log.hpp.

◆ writeSuppressedMessagesHint_

bool writeSuppressedMessagesHint_ = true
private

Definition at line 514 of file log.hpp.

◆ sameSourceLocationCutoff_

std::size_t sameSourceLocationCutoff_ = 1000
private

Definition at line 515 of file log.hpp.

◆ lastFileName_

std::string lastFileName_
private

Definition at line 516 of file log.hpp.

◆ lastLineNo_

int lastLineNo_ = 0
private

Definition at line 517 of file log.hpp.

◆ pid_

int pid_ = 0
private

Definition at line 519 of file log.hpp.

◆ mutex_

boost::shared_mutex mutex_
mutableprivate

Definition at line 521 of file log.hpp.

◆ excludeFilters_

std::map<std::string, std::function<bool(const std::string&)> > excludeFilters_
private

Definition at line 523 of file log.hpp.