logger.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #include "logger.hpp"
23 
24 #include "logging.hpp"
25 #include "time.hpp"
26 
27 #include <cinttypes> // for PRIdLEAST64
28 #include <cstdlib> // for std::abs()
29 #include <cstring> // for std::strspn()
30 #include <stdio.h> // for snprintf()
31 
32 namespace ndn {
33 namespace util {
34 
35 std::ostream&
36 operator<<(std::ostream& os, LogLevel level)
37 {
38  switch (level) {
39  case LogLevel::FATAL:
40  return os << "FATAL";
41  case LogLevel::NONE:
42  return os << "NONE";
43  case LogLevel::ERROR:
44  return os << "ERROR";
45  case LogLevel::WARN:
46  return os << "WARN";
47  case LogLevel::INFO:
48  return os << "INFO";
49  case LogLevel::DEBUG:
50  return os << "DEBUG";
51  case LogLevel::TRACE:
52  return os << "TRACE";
53  case LogLevel::ALL:
54  return os << "ALL";
55  }
56 
57  BOOST_THROW_EXCEPTION(std::invalid_argument("unknown log level " + to_string(static_cast<int>(level))));
58 }
59 
61 parseLogLevel(const std::string& s)
62 {
63  if (s == "FATAL")
64  return LogLevel::FATAL;
65  else if (s == "NONE")
66  return LogLevel::NONE;
67  else if (s == "ERROR")
68  return LogLevel::ERROR;
69  else if (s == "WARN")
70  return LogLevel::WARN;
71  else if (s == "INFO")
72  return LogLevel::INFO;
73  else if (s == "DEBUG")
74  return LogLevel::DEBUG;
75  else if (s == "TRACE")
76  return LogLevel::TRACE;
77  else if (s == "ALL")
78  return LogLevel::ALL;
79 
80  BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized log level '" + s + "'"));
81 }
82 
87 static bool
88 isValidLoggerName(const std::string& name)
89 {
90  // acceptable characters for Logger name
91  const char* okChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-";
92  if (std::strspn(name.c_str(), okChars) != name.size()) {
93  return false;
94  }
95  if (name.empty() || name.front() == '.' || name.back() == '.') {
96  return false;
97  }
98  if (name.find("..") != std::string::npos) {
99  return false;
100  }
101  return true;
102 }
103 
104 Logger::Logger(const std::string& name)
105  : m_moduleName(name)
106 {
107  if (!isValidLoggerName(name)) {
108  BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + name + "' is invalid"));
109  }
110  this->setLevel(LogLevel::NONE);
111  Logging::addLogger(*this);
112 }
113 
114 std::ostream&
115 operator<<(std::ostream& os, const LoggerTimestamp&)
116 {
117  using namespace ndn::time;
118 
119  const auto sinceEpoch = system_clock::now().time_since_epoch();
120  BOOST_ASSERT(sinceEpoch.count() >= 0);
121  // use abs() to silence truncation warning in snprintf(), see #4365
122  const auto usecs = std::abs(duration_cast<microseconds>(sinceEpoch).count());
123  const auto usecsPerSec = microseconds::period::den;
124 
125  // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
126  char buffer[10 + 1 + 6 + 1];
127  BOOST_ASSERT_MSG(usecs / usecsPerSec <= 9999999999, "whole seconds cannot fit in 10 characters");
128 
129  static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
130  "PRIdLEAST64 is incompatible with microseconds::rep");
131  // std::snprintf unavailable on some platforms, see #2299
132  ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
133  usecs / usecsPerSec, usecs % usecsPerSec);
134 
135  return os << buffer;
136 }
137 
138 } // namespace util
139 } // namespace ndn
static void addLogger(Logger &logger)
register a new logger
Definition: logging.hpp:180
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
trace messages (most verbose)
serious error messages
std::ostream & operator<<(std::ostream &os, LogLevel level)
output LogLevel as string
Definition: logger.cpp:36
a tag that writes a timestamp upon stream output
Definition: logger.hpp:116
informational messages
constexpr duration< Rep, Period > abs(duration< Rep, Period > d)
Definition: time.hpp:51
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:61
Logger(const std::string &name)
Definition: logger.cpp:104
warning messages
static bool isValidLoggerName(const std::string &name)
checks if incoming logger name meets criteria
Definition: logger.cpp:88
fatal (will be logged unconditionally)
std::string to_string(const V &v)
Definition: backports.hpp:84
void setLevel(LogLevel level)
Definition: logger.hpp:85