PURIFY
Next-generation radio interferometric imaging
Classes | Functions
purify::logging Namespace Reference

Classes

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

Functions

void _updateLevels (const Log::LevelMap &defaultLevels, Log::LogMap &existingLogs)
 
ostream & operator<< (Log &log, int level)
 Streaming output to a logger must have a Log::Level/int as its first argument. More...
 
LoggetLog ()
 Access method to default Log object. More...
 
void set_level (const std::string &level)
 Method to set the logging level of the default Log object. More...
 
template<typename Arg >
void applyFormat (std::stringstream &ss, char *&pos, Arg &&arg)
 
template<typename... Args>
std::string mkFormattedString (const char *txt, Args &&... args)
 Helper method to construct formatted string. More...
 
std::string mkFormattedString (const std::string &txt)
 

Function Documentation

◆ _updateLevels()

void purify::logging::_updateLevels ( const Log::LevelMap defaultLevels,
Log::LogMap existingLogs 
)
Todo:
Add single static setLevel
Todo:
Check ordering - "Foo" should come before "Foo.Bar"

Definition at line 24 of file logging.cc.

24  {
26  for (Log::LevelMap::const_iterator lev = defaultLevels.begin(); lev != defaultLevels.end();
27  ++lev) {
28  for (Log::LogMap::iterator log = existingLogs.begin(); log != existingLogs.end(); ++log) {
29  if (log->first.find(lev->first) == 0) {
30  log->second.setLevel(lev->second);
31  }
32  }
33  }
34 }

Referenced by purify::logging::Log::setLevels().

◆ applyFormat()

template<typename Arg >
void purify::logging::applyFormat ( std::stringstream &  ss,
char *&  pos,
Arg &&  arg 
)

Helper method to ireplace a set of curly braces with the template argument arg in a string stream

Definition at line 142 of file logging.h.

142  {
143  char* delim = strstr(pos, "{}");
144  if (delim != NULL) {
145  ss << std::string(pos, delim - pos) << std::forward<Arg>(arg);
146  pos = delim + 2;
147  } else {
148  throw std::runtime_error("Insufficient placeholders for number of arguments!");
149  }
150 }

Referenced by mkFormattedString().

◆ getLog()

Log& purify::logging::getLog ( )
inline

Access method to default Log object.

Definition at line 134 of file logging.h.

134 { return Log::getLog("purify::"); }
Log & getLog()
Access method to default Log object.
Definition: logging.h:134

References purify::logging::Log::getLog().

Referenced by set_level().

◆ mkFormattedString() [1/2]

template<typename... Args>
std::string purify::logging::mkFormattedString ( const char *  txt,
Args &&...  args 
)
inline

Helper method to construct formatted string.

Definition at line 154 of file logging.h.

154  {
155  std::string mys = txt;
156  std::stringstream rtn;
157  char* pos = (char*)txt;
158  ((void)applyFormat(rtn, pos, std::forward<Args>(args)), ...);
159  rtn << std::string(pos);
160  return rtn.str();
161 }
void applyFormat(std::stringstream &ss, char *&pos, Arg &&arg)
Definition: logging.h:142

References applyFormat().

Referenced by mkFormattedString().

◆ mkFormattedString() [2/2]

std::string purify::logging::mkFormattedString ( const std::string &  txt)
inline

Definition at line 163 of file logging.h.

163  {
164  return mkFormattedString(txt.data());
165 }
std::string mkFormattedString(const std::string &txt)
Definition: logging.h:163

References mkFormattedString().

◆ operator<<()

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

Streaming output to a logger must have a Log::Level/int as its first argument.

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 }

◆ set_level()

void purify::logging::set_level ( const std::string &  level)
inline

Method to set the logging level of the default Log object.

Definition at line 137 of file logging.h.

137 { getLog().setLevel(Log::getLevelFromName(level)); }
static void setLevel(const std::string &name, int level)
Set the log levels.

References purify::logging::Log::getLevelFromName(), getLog(), and purify::logging::Log::setLevel().

Referenced by main(), and TEST_CASE().