blob: 64cb5a26318ff85682f5db4881b71ad06e64ff31 [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 Pesaventoe1789892017-02-26 15:50:52 -05003 * Copyright (c) 2013-2017 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
27#include <cinttypes>
dmcoomese062a182017-06-12 11:10:31 -050028#include <cstring>
Junxiao Shi7d054272016-08-04 17:00:41 +000029
30namespace ndn {
31namespace util {
32
33std::ostream&
34operator<<(std::ostream& os, LogLevel level)
35{
36 switch (level) {
37 case LogLevel::FATAL:
38 return os << "FATAL";
39 case LogLevel::NONE:
40 return os << "NONE";
41 case LogLevel::ERROR:
42 return os << "ERROR";
43 case LogLevel::WARN:
44 return os << "WARN";
45 case LogLevel::INFO:
46 return os << "INFO";
47 case LogLevel::DEBUG:
48 return os << "DEBUG";
49 case LogLevel::TRACE:
50 return os << "TRACE";
51 case LogLevel::ALL:
52 return os << "ALL";
53 }
54
55 BOOST_THROW_EXCEPTION(std::invalid_argument("unknown log level " + to_string(static_cast<int>(level))));
56}
57
58LogLevel
59parseLogLevel(const std::string& s)
60{
61 if (s == "FATAL")
62 return LogLevel::FATAL;
63 else if (s == "NONE")
64 return LogLevel::NONE;
65 else if (s == "ERROR")
66 return LogLevel::ERROR;
67 else if (s == "WARN")
68 return LogLevel::WARN;
69 else if (s == "INFO")
70 return LogLevel::INFO;
71 else if (s == "DEBUG")
72 return LogLevel::DEBUG;
73 else if (s == "TRACE")
74 return LogLevel::TRACE;
75 else if (s == "ALL")
76 return LogLevel::ALL;
77
78 BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized log level '" + s + "'"));
79}
80
dmcoomese062a182017-06-12 11:10:31 -050081/**
82 * \brief checks if incoming logger name meets criteria
83 * \param name name of logger
84 */
85static bool
86isValidLoggerName(const std::string& name)
87{
88 // acceptable characters for Logger name
89 const char* okChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-";
90 if (std::strspn(name.c_str(), okChars) != name.size()) {
91 return false;
92 }
93 if (name.empty() || name.front() == '.' || name.back() == '.') {
94 return false;
95 }
96 if (name.find("..") != std::string::npos) {
97 return false;
98 }
99 return true;
100}
101
Junxiao Shi7d054272016-08-04 17:00:41 +0000102Logger::Logger(const std::string& name)
103 : m_moduleName(name)
104{
dmcoomese062a182017-06-12 11:10:31 -0500105 if (!isValidLoggerName(name)) {
106 BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name " + name + " is invalid"));
107 }
Junxiao Shi7d054272016-08-04 17:00:41 +0000108 this->setLevel(LogLevel::NONE);
109 Logging::addLogger(*this);
110}
111
112std::ostream&
113operator<<(std::ostream& os, const LoggerTimestamp&)
114{
115 using namespace ndn::time;
116
117 static const microseconds::rep ONE_SECOND = 1000000;
118 microseconds::rep usecs = duration_cast<microseconds>(
119 system_clock::now().time_since_epoch()).count();
120
121 // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
122 char buffer[10 + 1 + 6 + 1];
123 BOOST_ASSERT_MSG(usecs / ONE_SECOND <= 9999999999L,
124 "whole seconds cannot fit in 10 characters");
125
126 static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
127 "PRIdLEAST64 is incompatible with microseconds::rep");
128 // std::snprintf unavailable in some environments <https://redmine.named-data.net/issues/2299>
129 snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
130 usecs / ONE_SECOND, usecs % ONE_SECOND);
131
132 return os << buffer;
133}
134
135} // namespace util
136} // namespace ndn