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 "ndn-cxx/util/logger.hpp"
23 #include "ndn-cxx/util/logging.hpp"
24 #include "ndn-cxx/util/time.hpp"
25 
26 #include <cinttypes> // for PRIdLEAST64
27 #include <cstdlib> // for std::abs()
28 #include <cstring> // for std::strspn()
29 #include <stdio.h> // for snprintf()
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 
86 static bool
87 isValidLoggerName(const std::string& name)
88 {
89  // acceptable characters for Logger name
90  const char* okChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-";
91  if (std::strspn(name.c_str(), okChars) != name.size()) {
92  return false;
93  }
94  if (name.empty() || name.front() == '.' || name.back() == '.') {
95  return false;
96  }
97  if (name.find("..") != std::string::npos) {
98  return false;
99  }
100  return true;
101 }
102 
103 Logger::Logger(const char* name)
104  : m_moduleName(name)
105 {
106  if (!isValidLoggerName(m_moduleName)) {
107  BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + m_moduleName + "' is invalid"));
108  }
109  this->setLevel(LogLevel::NONE);
110  Logging::get().addLoggerImpl(*this);
111 }
112 
113 void
114 Logger::registerModuleName(const char* name)
115 {
116  std::string moduleName(name);
117  if (!isValidLoggerName(moduleName)) {
118  BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + moduleName + "' is invalid"));
119  }
120  Logging::get().registerLoggerNameImpl(std::move(moduleName));
121 }
122 
123 namespace detail {
124 
125 std::ostream&
126 operator<<(std::ostream& os, LoggerTimestamp)
127 {
128  using namespace ndn::time;
129 
130  const auto sinceEpoch = system_clock::now().time_since_epoch();
131  BOOST_ASSERT(sinceEpoch.count() >= 0);
132  // use abs() to silence truncation warning in snprintf(), see #4365
133  const auto usecs = std::abs(duration_cast<microseconds>(sinceEpoch).count());
134  const auto usecsPerSec = microseconds::period::den;
135 
136  // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
137  char buffer[10 + 1 + 6 + 1];
138  BOOST_ASSERT_MSG(usecs / usecsPerSec <= 9999999999, "whole seconds cannot fit in 10 characters");
139 
140  static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
141  "PRIdLEAST64 is incompatible with microseconds::rep");
142  // std::snprintf unavailable on some platforms, see #2299
143  ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
144  usecs / usecsPerSec, usecs % usecsPerSec);
145 
146  return os << buffer;
147 }
148 
149 } // namespace detail
150 } // namespace util
151 } // namespace ndn
Definition: data.cpp:26
Logger(const char *name)
Definition: logger.cpp:103
trace messages (most verbose)
serious error messages
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:35
static void registerModuleName(const char *name)
Definition: logger.cpp:114
informational messages
constexpr duration< Rep, Period > abs(duration< Rep, Period > d)
Definition: time.hpp:50
LogLevel
Indicates the severity level of a log message.
Definition: logger.hpp:40
LogLevel parseLogLevel(const std::string &s)
Parse LogLevel from a string.
Definition: logger.cpp:60
A tag type used to output a timestamp to a stream.
Definition: logger.hpp:107
warning messages
static bool isValidLoggerName(const std::string &name)
checks if incoming logger name meets criteria
Definition: logger.cpp:87
fatal (will be logged unconditionally)
std::string to_string(const V &v)
Definition: backports.hpp:67
void setLevel(LogLevel level)
Definition: logger.hpp:90