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-2019 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 
82 static bool
83 isValidLoggerName(const std::string& name)
84 {
85  // acceptable characters for Logger name
86  const char* okChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-";
87  if (std::strspn(name.c_str(), okChars) != name.size()) {
88  return false;
89  }
90  if (name.empty() || name.front() == '.' || name.back() == '.') {
91  return false;
92  }
93  if (name.find("..") != std::string::npos) {
94  return false;
95  }
96  return true;
97 }
98 
99 Logger::Logger(const char* name)
100  : m_moduleName(name)
101 {
102  if (!isValidLoggerName(m_moduleName)) {
103  NDN_THROW(std::invalid_argument("Logger name '" + m_moduleName + "' is invalid"));
104  }
105  this->setLevel(LogLevel::NONE);
106  this->add_attribute(log::module.get_name(), boost::log::attributes::constant<std::string>(m_moduleName));
107  Logging::get().addLoggerImpl(*this);
108 }
109 
110 void
111 Logger::registerModuleName(const char* name)
112 {
113  std::string moduleName(name);
114  if (!isValidLoggerName(moduleName)) {
115  NDN_THROW(std::invalid_argument("Logger name '" + moduleName + "' is invalid"));
116  }
117  Logging::get().registerLoggerNameImpl(std::move(moduleName));
118 }
119 
120 } // namespace util
121 } // namespace ndn
Definition: data.cpp:26
Logger(const char *name)
Definition: logger.cpp:99
trace messages (most verbose)
std::string to_string(const T &val)
Definition: backports.hpp:102
serious error messages
std::ostream & operator<<(std::ostream &os, LogLevel level)
Output LogLevel as a string.
Definition: logger.cpp:31
static void registerModuleName(const char *name)
Definition: logger.cpp:111
informational messages
#define NDN_THROW(e)
Definition: exception.hpp:61
LogLevel
Indicates the severity level of a log message.
Definition: logger.hpp:42
LogLevel parseLogLevel(const std::string &s)
Parse LogLevel from a string.
Definition: logger.cpp:56
warning messages
static bool isValidLoggerName(const std::string &name)
checks if incoming logger name meets criteria
Definition: logger.cpp:83
fatal (will be logged unconditionally)
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition: backports.hpp:141
void setLevel(LogLevel level)
Definition: logger.hpp:99