logger.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "logger.hpp"
23 
24 #include "logging.hpp"
25 #include "time.hpp"
26 
27 #include <cinttypes>
28 #include <stdio.h>
29 #include <type_traits>
30 
31 namespace ndn {
32 namespace util {
33 
34 std::ostream&
35 operator<<(std::ostream& os, LogLevel level)
36 {
37  switch (level) {
38  case LogLevel::FATAL:
39  return os << "FATAL";
40  case LogLevel::NONE:
41  return os << "NONE";
42  case LogLevel::ERROR:
43  return os << "ERROR";
44  case LogLevel::WARN:
45  return os << "WARN";
46  case LogLevel::INFO:
47  return os << "INFO";
48  case LogLevel::DEBUG:
49  return os << "DEBUG";
50  case LogLevel::TRACE:
51  return os << "TRACE";
52  case LogLevel::ALL:
53  return os << "ALL";
54  }
55 
56  BOOST_THROW_EXCEPTION(std::invalid_argument("unknown log level " + to_string(static_cast<int>(level))));
57 }
58 
60 parseLogLevel(const std::string& s)
61 {
62  if (s == "FATAL")
63  return LogLevel::FATAL;
64  else if (s == "NONE")
65  return LogLevel::NONE;
66  else if (s == "ERROR")
67  return LogLevel::ERROR;
68  else if (s == "WARN")
69  return LogLevel::WARN;
70  else if (s == "INFO")
71  return LogLevel::INFO;
72  else if (s == "DEBUG")
73  return LogLevel::DEBUG;
74  else if (s == "TRACE")
75  return LogLevel::TRACE;
76  else if (s == "ALL")
77  return LogLevel::ALL;
78 
79  BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized log level '" + s + "'"));
80 }
81 
82 Logger::Logger(const std::string& name)
83  : m_moduleName(name)
84 {
85  this->setLevel(LogLevel::NONE);
86  Logging::addLogger(*this);
87 }
88 
89 std::ostream&
90 operator<<(std::ostream& os, const LoggerTimestamp&)
91 {
92  using namespace ndn::time;
93 
94  static const microseconds::rep ONE_SECOND = 1000000;
95  microseconds::rep usecs = duration_cast<microseconds>(
96  system_clock::now().time_since_epoch()).count();
97 
98  // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
99  char buffer[10 + 1 + 6 + 1];
100  BOOST_ASSERT_MSG(usecs / ONE_SECOND <= 9999999999L,
101  "whole seconds cannot fit in 10 characters");
102 
103  static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
104  "PRIdLEAST64 is incompatible with microseconds::rep");
105  // std::snprintf unavailable in some environments <https://redmine.named-data.net/issues/2299>
106  snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
107  usecs / ONE_SECOND, usecs % ONE_SECOND);
108 
109  return os << buffer;
110 }
111 
112 } // namespace util
113 } // namespace ndn
static void addLogger(Logger &logger)
register a new logger
Definition: logging.hpp:158
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
trace messages (most verbose)
serious error messages
a tag that writes a timestamp upon stream output
Definition: logger.hpp:112
informational messages
LogLevel
indicates the severity level of a log message
Definition: logger.hpp:40
LogLevel parseLogLevel(const std::string &s)
parse LogLevel from string
Definition: logger.cpp:60
Logger(const std::string &name)
Definition: logger.cpp:82
warning messages
std::ostream & operator<<(std::ostream &os, Digest< Hash > &digest)
Definition: digest.cpp:170
fatal (will be logged unconditionally)
std::string to_string(const V &v)
Definition: backports.hpp:51
void setLevel(LogLevel level)
Definition: logger.hpp:85