blob: 8cb7d8060c88906a0202c717a24fabcd4d176093 [file] [log] [blame]
Junxiao Shi7d054272016-08-04 17:00:41 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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>
28#include <stdio.h>
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
81Logger::Logger(const std::string& name)
82 : m_moduleName(name)
83{
84 this->setLevel(LogLevel::NONE);
85 Logging::addLogger(*this);
86}
87
88std::ostream&
89operator<<(std::ostream& os, const LoggerTimestamp&)
90{
91 using namespace ndn::time;
92
93 static const microseconds::rep ONE_SECOND = 1000000;
94 microseconds::rep usecs = duration_cast<microseconds>(
95 system_clock::now().time_since_epoch()).count();
96
97 // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
98 char buffer[10 + 1 + 6 + 1];
99 BOOST_ASSERT_MSG(usecs / ONE_SECOND <= 9999999999L,
100 "whole seconds cannot fit in 10 characters");
101
102 static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
103 "PRIdLEAST64 is incompatible with microseconds::rep");
104 // std::snprintf unavailable in some environments <https://redmine.named-data.net/issues/2299>
105 snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
106 usecs / ONE_SECOND, usecs % ONE_SECOND);
107
108 return os << buffer;
109}
110
111} // namespace util
112} // namespace ndn