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 char* name)
105  : m_moduleName(name)
106 {
107  if (!isValidLoggerName(m_moduleName)) {
108  BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + m_moduleName + "' is invalid"));
109  }
110  this->setLevel(LogLevel::NONE);
111  Logging::get().addLoggerImpl(*this);
112 }
113 
114 void
115 Logger::registerModuleName(const char* name)
116 {
117  std::string moduleName(name);
118  if (!isValidLoggerName(moduleName)) {
119  BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + moduleName + "' is invalid"));
120  }
121  Logging::get().registerLoggerNameImpl(std::move(moduleName));
122 }
123 
124 namespace detail {
125 
126 std::ostream&
127 operator<<(std::ostream& os, LoggerTimestamp)
128 {
129  using namespace ndn::time;
130 
131  const auto sinceEpoch = system_clock::now().time_since_epoch();
132  BOOST_ASSERT(sinceEpoch.count() >= 0);
133  // use abs() to silence truncation warning in snprintf(), see #4365
134  const auto usecs = std::abs(duration_cast<microseconds>(sinceEpoch).count());
135  const auto usecsPerSec = microseconds::period::den;
136 
137  // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
138  char buffer[10 + 1 + 6 + 1];
139  BOOST_ASSERT_MSG(usecs / usecsPerSec <= 9999999999, "whole seconds cannot fit in 10 characters");
140 
141  static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
142  "PRIdLEAST64 is incompatible with microseconds::rep");
143  // std::snprintf unavailable on some platforms, see #2299
144  ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
145  usecs / usecsPerSec, usecs % usecsPerSec);
146 
147  return os << buffer;
148 }
149 
150 } // namespace detail
151 } // namespace util
152 } // namespace ndn
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
Logger(const char *name)
Definition: logger.cpp:104
trace messages (most verbose)
serious error messages
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:36
static void registerModuleName(const char *name)
Definition: logger.cpp:115
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 a string.
Definition: logger.cpp:61
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:88
fatal (will be logged unconditionally)
std::string to_string(const V &v)
Definition: backports.hpp:107
void setLevel(LogLevel level)
Definition: logger.hpp:90