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