PURIFY
Next-generation radio interferometric imaging
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
purify::logging::Log Class Reference

Logging system for controlled & formatted writing to stdout. More...

#include <logging.h>

Public Types

enum  Level {
  trace = 0 , debug = 10 , info = 20 , warn = 30 ,
  warning = 30 , error = 40 , critical = 50 , always = 50
}
 Log priority levels. More...
 
using LogMap = std::map< std::string, Log >
 Typedef for a collection of named logs. More...
 
using LevelMap = std::map< std::string, int >
 Typedef for a collection of named log levels. More...
 
using ColorCodes = std::map< int, std::string >
 Typedef for a collection of shell color codes, accessed by log level. More...
 

Public Member Functions

int getLevel () const
 Get the priority level of this logger. More...
 
LogsetLevel (int level)
 Set the priority level of this logger. More...
 
std::string getName () const
 Get the name of this logger. More...
 
LogsetName (const std::string &name)
 Set the name of this logger. More...
 
bool isActive (int level) const
 Will this log level produce output on this logger at the moment? More...
 

Static Public Member Functions

static void setLevel (const std::string &name, int level)
 Set the log levels. More...
 
static void setLevels (const LevelMap &logLevels)
 
static LoggetLog (const std::string &name)
 
static Level getLevelFromName (const std::string &level)
 Get a log level enum from a string. More...
 
static std::string getLevelName (int level)
 Get the std::string representation of a log level. More...
 

Static Public Attributes

static const int end_color = -10
 Special "level-like" code to end coloring. More...
 

Friends

std::ostream & operator<< (Log &log, int level)
 The streaming operator can use Log's internals. More...
 

Detailed Description

Logging system for controlled & formatted writing to stdout.

Definition at line 16 of file logging.h.

Member Typedef Documentation

◆ ColorCodes

using purify::logging::Log::ColorCodes = std::map<int, std::string>

Typedef for a collection of shell color codes, accessed by log level.

Definition at line 38 of file logging.h.

◆ LevelMap

using purify::logging::Log::LevelMap = std::map<std::string, int>

Typedef for a collection of named log levels.

Definition at line 35 of file logging.h.

◆ LogMap

using purify::logging::Log::LogMap = std::map<std::string, Log>

Typedef for a collection of named logs.

Definition at line 32 of file logging.h.

Member Enumeration Documentation

◆ Level

Log priority levels.

Enumerator
trace 
debug 
info 
warn 
warning 
error 
critical 
always 

Definition at line 19 of file logging.h.

19  {
20  trace = 0,
21  debug = 10,
22  info = 20,
23  warn = 30,
24  warning = 30,
25  error = 40,
26  critical = 50,
27  always = 50
28  };

Member Function Documentation

◆ getLevel()

int purify::logging::Log::getLevel ( ) const
inline

Get the priority level of this logger.

Definition at line 85 of file logging.h.

85 { return _level; }

Referenced by getLog().

◆ getLevelFromName()

Log::Level purify::logging::Log::getLevelFromName ( const std::string &  level)
static

Get a log level enum from a string.

Definition at line 122 of file logging.cc.

122  {
123  if (level == "trace") return trace;
124  if (level == "debug") return debug;
125  if (level == "info") return info;
126  if (level == "warn") return warn;
127  if (level == "error") return error;
128  if (level == "critical") return critical;
129  throw std::runtime_error("Couldn't create a log level from string '" + level + "'");
130 }

Referenced by purify::logging::set_level().

◆ getLevelName()

string purify::logging::Log::getLevelName ( int  level)
static

Get the std::string representation of a log level.

Definition at line 86 of file logging.cc.

86  {
87  switch (level) {
88  case trace:
89  return "trace";
90  case debug:
91  return "debug";
92  case info:
93  return "info";
94  case warn:
95  return "warn";
96  case error:
97  return "error";
98  case critical:
99  return "critical";
100  default:
101  return "";
102  }
103 }

◆ getLog()

Log & purify::logging::Log::getLog ( const std::string &  name)
static

Get a logger with the given name. The level will be taken from the "requestedLevels" static map or will be INFO by default.

Definition at line 49 of file logging.cc.

49  {
50  auto theLog = existingLogs.find(name);
51  if (theLog == existingLogs.end()) {
52  int level = info;
53  // Try running through all parent classes to find an existing level
54  string tmpname = name;
55  bool triedAllParents = false;
56  while (!triedAllParents) {
57  // Is there a default level?
58  if (defaultLevels.find(tmpname) != defaultLevels.end()) {
59  level = defaultLevels.find(tmpname)->second;
60  break;
61  }
62  // Is there already such a logger? (NB. tmpname != name in later iterations)
63  if (existingLogs.find(tmpname) != existingLogs.end()) {
64  level = existingLogs.find(tmpname)->second.getLevel();
65  break;
66  }
67  // Crop the string back to the next parent level
68  size_t lastDot = tmpname.find_last_of(".");
69  if (lastDot != string::npos) {
70  tmpname = tmpname.substr(0, lastDot);
71  } else {
72  triedAllParents = true;
73  }
74  }
75  // for (LevelMap::const_iterator l = defaultLevels.begin(); l != defaultLevels.end(); ++l) {
76  //
77  // }
78 
79  // emplace returns pair<iterator,bool>
80  auto result = existingLogs.emplace(name, Log(name, level));
81  theLog = result.first;
82  }
83  return theLog->second;
84 }

References getLevel().

Referenced by purify::logging::getLog().

◆ getName()

std::string purify::logging::Log::getName ( ) const
inline

Get the name of this logger.

Definition at line 100 of file logging.h.

100 { return _name; }

◆ isActive()

bool purify::logging::Log::isActive ( int  level) const
inline

Will this log level produce output on this logger at the moment?

Definition at line 109 of file logging.h.

109 { return (level >= _level); }

◆ setLevel() [1/2]

static void purify::logging::Log::setLevel ( const std::string &  name,
int  level 
)
static

Set the log levels.

Referenced by purify::logging::set_level().

◆ setLevel() [2/2]

Log& purify::logging::Log::setLevel ( int  level)
inline

Set the priority level of this logger.

Definition at line 88 of file logging.h.

88  {
89  _level = level;
90  return *this;
91  }

◆ setLevels()

void purify::logging::Log::setLevels ( const LevelMap logLevels)
static

Definition at line 42 of file logging.cc.

42  {
43  for (LevelMap::const_iterator lev = logLevels.begin(); lev != logLevels.end(); ++lev) {
44  defaultLevels[lev->first] = lev->second;
45  }
46  _updateLevels(defaultLevels, existingLogs);
47 }
void _updateLevels(const Log::LevelMap &defaultLevels, Log::LogMap &existingLogs)
Definition: logging.cc:24

References purify::logging::_updateLevels().

◆ setName()

Log& purify::logging::Log::setName ( const std::string &  name)
inline

Set the name of this logger.

Definition at line 103 of file logging.h.

103  {
104  _name = name;
105  return *this;
106  }

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( Log log,
int  level 
)
friend

The streaming operator can use Log's internals.

Definition at line 171 of file logging.cc.

171  {
172  if (log.isActive(level)) {
173  if (level > Log::warning) {
174  cerr << log.formatMessage(level, "");
175  return cerr;
176  } else {
177  cout << log.formatMessage(level, "");
178  return cout;
179  }
180  } else {
181  static ostream devNull(nullptr);
182  return devNull;
183  }
184 }

Member Data Documentation

◆ end_color

const int purify::logging::Log::end_color = -10
static

Special "level-like" code to end coloring.

Definition at line 29 of file logging.h.


The documentation for this class was generated from the following files: