SOPT
Sparse OPTimisation
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
sopt::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 14 of file logging.h.

Member Typedef Documentation

◆ ColorCodes

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

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

Definition at line 36 of file logging.h.

◆ LevelMap

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

Typedef for a collection of named log levels.

Definition at line 33 of file logging.h.

◆ LogMap

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

Typedef for a collection of named logs.

Definition at line 30 of file logging.h.

Member Enumeration Documentation

◆ Level

Log priority levels.

Enumerator
trace 
debug 
info 
warn 
warning 
error 
critical 
always 

Definition at line 18 of file logging.h.

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

Member Function Documentation

◆ getLevel()

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

Get the priority level of this logger.

Definition at line 90 of file logging.h.

90  {
91  return _level;
92  }

Referenced by getLog().

◆ getLevelFromName()

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

Get a log level enum from a string.

Definition at line 138 of file logging.cc.

138  {
139  if (level == "trace") return trace;
140  if (level == "debug") return debug;
141  if (level == "info") return info;
142  if (level == "warn") return warn;
143  if (level == "error") return error;
144  if (level == "critical") return critical;
145  SOPT_THROW("Couldn't create a log level from string '" + level + "'");
146  }
#define SOPT_THROW(MSG)
Definition: exception.h:46

References SOPT_THROW.

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

◆ getLevelName()

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

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

Definition at line 95 of file logging.cc.

95  {
96  switch(level) {
97  case trace:
98  return "trace";
99  case debug:
100  return "debug";
101  case info:
102  return "info";
103  case warn:
104  return "warn";
105  case error:
106  return "error";
107  case critical:
108  return "critical";
109  default:
110  return "";
111  }
112  }

◆ getLog()

Log & sopt::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 57 of file logging.cc.

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

References getLevel().

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

◆ getName()

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

Get the name of this logger.

Definition at line 107 of file logging.h.

107  {
108  return _name;
109  }

◆ isActive()

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

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

Definition at line 118 of file logging.h.

118  {
119  return (level >= _level);
120  }

◆ setLevel() [1/2]

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

Set the log levels.

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

◆ setLevel() [2/2]

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

Set the priority level of this logger.

Definition at line 95 of file logging.h.

95  {
96  _level = level;
97  return *this;
98  }

◆ setLevels()

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

Definition at line 49 of file logging.cc.

49  {
50  for (LevelMap::const_iterator lev = logLevels.begin(); lev != logLevels.end(); ++lev) {
51  defaultLevels[lev->first] = lev->second;
52  }
53  _updateLevels(defaultLevels, existingLogs);
54  }
void _updateLevels(const Log::LevelMap &defaultLevels, Log::LogMap &existingLogs)
Definition: logging.cc:30

References sopt::logging::_updateLevels().

◆ setName()

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

Set the name of this logger.

Definition at line 112 of file logging.h.

112  {
113  _name = name;
114  return *this;
115  }

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 190 of file logging.cc.

190  {
191  if (log.isActive(level)) {
192  if (level > Log::warning) {
193  cerr << log.formatMessage(level, "");
194  return cerr;
195  } else {
196  cout << log.formatMessage(level, "");
197  return cout;
198  }
199  } else {
200  static ostream devNull(nullptr);
201  return devNull;
202  }
203  }

Member Data Documentation

◆ end_color

const int sopt::logging::Log::end_color {-10}
static

Special "level-like" code to end coloring.

Definition at line 26 of file logging.h.


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