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-2022 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 
25 #include <cstring> // for std::strspn()
26 
27 namespace ndn {
28 namespace util {
29 
30 std::ostream&
31 operator<<(std::ostream& os, LogLevel level)
32 {
33  switch (level) {
34  case LogLevel::FATAL:
35  return os << "FATAL";
36  case LogLevel::NONE:
37  return os << "NONE";
38  case LogLevel::ERROR:
39  return os << "ERROR";
40  case LogLevel::WARN:
41  return os << "WARN";
42  case LogLevel::INFO:
43  return os << "INFO";
44  case LogLevel::DEBUG:
45  return os << "DEBUG";
46  case LogLevel::TRACE:
47  return os << "TRACE";
48  case LogLevel::ALL:
49  return os << "ALL";
50  }
51 
52  NDN_THROW(std::invalid_argument("unknown log level " + to_string(to_underlying(level))));
53 }
54 
56 parseLogLevel(const std::string& s)
57 {
58  if (s == "FATAL")
59  return LogLevel::FATAL;
60  else if (s == "NONE")
61  return LogLevel::NONE;
62  else if (s == "ERROR")
63  return LogLevel::ERROR;
64  else if (s == "WARN")
65  return LogLevel::WARN;
66  else if (s == "INFO")
67  return LogLevel::INFO;
68  else if (s == "DEBUG")
69  return LogLevel::DEBUG;
70  else if (s == "TRACE")
71  return LogLevel::TRACE;
72  else if (s == "ALL")
73  return LogLevel::ALL;
74 
75  NDN_THROW(std::invalid_argument("unrecognized log level '" + s + "'"));
76 }
77 
78 static bool
79 isValidLoggerName(const std::string& name)
80 {
81  // acceptable characters for Logger name
82  const char* okChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-";
83  if (std::strspn(name.c_str(), okChars) != name.size()) {
84  return false;
85  }
86  if (name.empty() || name.front() == '.' || name.back() == '.') {
87  return false;
88  }
89  if (name.find("..") != std::string::npos) {
90  return false;
91  }
92  return true;
93 }
94 
95 Logger::Logger(const char* name)
96  : m_moduleName(name)
97 {
98  if (!isValidLoggerName(m_moduleName)) {
99  NDN_THROW(std::invalid_argument("Logger name '" + m_moduleName + "' is invalid"));
100  }
101  this->setLevel(LogLevel::NONE);
102  this->add_attribute(log::module.get_name(), boost::log::attributes::constant<std::string>(m_moduleName));
103  Logging::get().addLoggerImpl(*this);
104 }
105 
106 void
107 Logger::registerModuleName(const char* name)
108 {
109  std::string moduleName(name);
110  if (!isValidLoggerName(moduleName)) {
111  NDN_THROW(std::invalid_argument("Logger name '" + moduleName + "' is invalid"));
112  }
113  Logging::get().registerLoggerNameImpl(std::move(moduleName));
114 }
115 
116 } // namespace util
117 } // namespace ndn
Logger(const char *name)
Definition: logger.cpp:95
static void registerModuleName(const char *name)
Definition: logger.cpp:107
void setLevel(LogLevel level)
Definition: logger.hpp:99
#define NDN_THROW(e)
Definition: exception.hpp:61
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:31
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:31
LogLevel parseLogLevel(const std::string &s)
Parse LogLevel from a string.
Definition: logger.cpp:56
LogLevel
Indicates the severity level of a log message.
Definition: logger.hpp:42
@ FATAL
fatal (will be logged unconditionally)
@ TRACE
trace messages (most verbose)
@ WARN
warning messages
@ INFO
informational messages
@ ALL
all messages
@ NONE
no messages
@ ERROR
serious error messages
@ DEBUG
debug messages
Definition: data.cpp:25
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:125