blob: 8c752d308b8c67dbdf971b18bf52223cbe281094 [file] [log] [blame]
Junxiao Shi7d054272016-08-04 17:00:41 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
dmcoomese062a182017-06-12 11:10:31 -05002/*
Davide Pesavento233f67c2018-01-21 17:23:21 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi7d054272016-08-04 17:00:41 +00004 *
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
Davide Pesavento233f67c2018-01-21 17:23:21 -050027#include <cinttypes> // for PRIdLEAST64
28#include <cstdlib> // for std::abs()
29#include <cstring> // for std::strspn()
30#include <stdio.h> // for snprintf()
Junxiao Shi7d054272016-08-04 17:00:41 +000031
32namespace ndn {
33namespace util {
34
35std::ostream&
36operator<<(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
60LogLevel
61parseLogLevel(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
dmcoomese062a182017-06-12 11:10:31 -050083/**
84 * \brief checks if incoming logger name meets criteria
85 * \param name name of logger
86 */
87static bool
88isValidLoggerName(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
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400104Logger::Logger(const char* name)
Junxiao Shi7d054272016-08-04 17:00:41 +0000105 : m_moduleName(name)
106{
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400107 if (!isValidLoggerName(m_moduleName)) {
108 BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + m_moduleName + "' is invalid"));
dmcoomese062a182017-06-12 11:10:31 -0500109 }
Junxiao Shi7d054272016-08-04 17:00:41 +0000110 this->setLevel(LogLevel::NONE);
Davide Pesavento1c9c96c2018-04-25 10:45:08 -0400111 Logging::get().addLoggerImpl(*this);
112}
113
114void
115Logger::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));
Junxiao Shi7d054272016-08-04 17:00:41 +0000122}
123
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400124namespace detail {
125
Junxiao Shi7d054272016-08-04 17:00:41 +0000126std::ostream&
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400127operator<<(std::ostream& os, LoggerTimestamp)
Junxiao Shi7d054272016-08-04 17:00:41 +0000128{
129 using namespace ndn::time;
130
Davide Pesavento233f67c2018-01-21 17:23:21 -0500131 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;
Junxiao Shi7d054272016-08-04 17:00:41 +0000136
137 // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
138 char buffer[10 + 1 + 6 + 1];
Davide Pesavento233f67c2018-01-21 17:23:21 -0500139 BOOST_ASSERT_MSG(usecs / usecsPerSec <= 9999999999, "whole seconds cannot fit in 10 characters");
Junxiao Shi7d054272016-08-04 17:00:41 +0000140
141 static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
142 "PRIdLEAST64 is incompatible with microseconds::rep");
Davide Pesavento233f67c2018-01-21 17:23:21 -0500143 // std::snprintf unavailable on some platforms, see #2299
144 ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
145 usecs / usecsPerSec, usecs % usecsPerSec);
Junxiao Shi7d054272016-08-04 17:00:41 +0000146
147 return os << buffer;
148}
149
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400150} // namespace detail
Junxiao Shi7d054272016-08-04 17:00:41 +0000151} // namespace util
152} // namespace ndn