blob: 92a103913268c3baf636e352092cbe6fbcda4e6d [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
Junxiao Shi7d054272016-08-04 17:00:41 +0000104Logger::Logger(const std::string& name)
105 : m_moduleName(name)
106{
dmcoomese062a182017-06-12 11:10:31 -0500107 if (!isValidLoggerName(name)) {
Davide Pesavento233f67c2018-01-21 17:23:21 -0500108 BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + name + "' is invalid"));
dmcoomese062a182017-06-12 11:10:31 -0500109 }
Junxiao Shi7d054272016-08-04 17:00:41 +0000110 this->setLevel(LogLevel::NONE);
111 Logging::addLogger(*this);
112}
113
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400114namespace detail {
115
Junxiao Shi7d054272016-08-04 17:00:41 +0000116std::ostream&
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400117operator<<(std::ostream& os, LoggerTimestamp)
Junxiao Shi7d054272016-08-04 17:00:41 +0000118{
119 using namespace ndn::time;
120
Davide Pesavento233f67c2018-01-21 17:23:21 -0500121 const auto sinceEpoch = system_clock::now().time_since_epoch();
122 BOOST_ASSERT(sinceEpoch.count() >= 0);
123 // use abs() to silence truncation warning in snprintf(), see #4365
124 const auto usecs = std::abs(duration_cast<microseconds>(sinceEpoch).count());
125 const auto usecsPerSec = microseconds::period::den;
Junxiao Shi7d054272016-08-04 17:00:41 +0000126
127 // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
128 char buffer[10 + 1 + 6 + 1];
Davide Pesavento233f67c2018-01-21 17:23:21 -0500129 BOOST_ASSERT_MSG(usecs / usecsPerSec <= 9999999999, "whole seconds cannot fit in 10 characters");
Junxiao Shi7d054272016-08-04 17:00:41 +0000130
131 static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
132 "PRIdLEAST64 is incompatible with microseconds::rep");
Davide Pesavento233f67c2018-01-21 17:23:21 -0500133 // std::snprintf unavailable on some platforms, see #2299
134 ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
135 usecs / usecsPerSec, usecs % usecsPerSec);
Junxiao Shi7d054272016-08-04 17:00:41 +0000136
137 return os << buffer;
138}
139
Davide Pesavento0c145ec2018-04-10 21:33:57 -0400140} // namespace detail
Junxiao Shi7d054272016-08-04 17:00:41 +0000141} // namespace util
142} // namespace ndn