SOPT
Sparse OPTimisation
Classes | Functions
sopt::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...
 
const std::string & mkFormattedString (const std::string &txt)
 

Function Documentation

◆ _updateLevels()

void sopt::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 30 of file logging.cc.

30  {
32  for (Log::LevelMap::const_iterator lev = defaultLevels.begin(); lev != defaultLevels.end(); ++lev) {
33  for (Log::LogMap::iterator log = existingLogs.begin(); log != existingLogs.end(); ++log) {
34  if (log->first.find(lev->first) == 0) {
35  log->second.setLevel(lev->second);
36  }
37  }
38  }
39  }

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

◆ applyFormat()

template<typename Arg >
void sopt::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 162 of file logging.h.

162  {
163  char* delim = strstr(pos, "{}");
164  if (delim != NULL) {
165  ss << std::string(pos, delim-pos) << std::forward<Arg>(arg);
166  pos = delim + 2;
167  }
168  else {
169  SOPT_THROW("Insufficient placeholders for number of arguments!");
170  }
171 }
#define SOPT_THROW(MSG)
Definition: exception.h:46

References SOPT_THROW.

Referenced by mkFormattedString().

◆ getLog()

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

Access method to default Log object.

Definition at line 151 of file logging.h.

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

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

Referenced by set_level().

◆ mkFormattedString() [1/2]

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

Helper method to construct formatted string.

Definition at line 175 of file logging.h.

175  {
176  std::string mys = txt;
177  std::stringstream rtn;
178  char* pos = (char*)txt;
179  ((void)applyFormat(rtn, pos, std::forward<Args>(args)), ...);
180  rtn << std::string(pos);
181  return rtn.str();
182 }
void applyFormat(std::stringstream &ss, char *&pos, Arg &&arg)
Definition: logging.h:162

References applyFormat().

◆ mkFormattedString() [2/2]

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

Definition at line 184 of file logging.h.

184 { return txt; }

◆ operator<<()

std::ostream & sopt::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 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  }

◆ set_level()

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

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

Definition at line 154 of file logging.h.

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

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

Referenced by main(), and TEST_CASE().