blob: 04ad9540624e3d6019742965e94cb8882143e67b [file] [log] [blame]
Junxiao Shi7d054272016-08-04 17:00:41 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 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 "logger.hpp"
23
24#include "logging.hpp"
25#include "time.hpp"
26
27#include <cinttypes>
28#include <stdio.h>
29#include <type_traits>
30
31namespace ndn {
32namespace util {
33
34std::ostream&
35operator<<(std::ostream& os, LogLevel level)
36{
37 switch (level) {
38 case LogLevel::FATAL:
39 return os << "FATAL";
40 case LogLevel::NONE:
41 return os << "NONE";
42 case LogLevel::ERROR:
43 return os << "ERROR";
44 case LogLevel::WARN:
45 return os << "WARN";
46 case LogLevel::INFO:
47 return os << "INFO";
48 case LogLevel::DEBUG:
49 return os << "DEBUG";
50 case LogLevel::TRACE:
51 return os << "TRACE";
52 case LogLevel::ALL:
53 return os << "ALL";
54 }
55
56 BOOST_THROW_EXCEPTION(std::invalid_argument("unknown log level " + to_string(static_cast<int>(level))));
57}
58
59LogLevel
60parseLogLevel(const std::string& s)
61{
62 if (s == "FATAL")
63 return LogLevel::FATAL;
64 else if (s == "NONE")
65 return LogLevel::NONE;
66 else if (s == "ERROR")
67 return LogLevel::ERROR;
68 else if (s == "WARN")
69 return LogLevel::WARN;
70 else if (s == "INFO")
71 return LogLevel::INFO;
72 else if (s == "DEBUG")
73 return LogLevel::DEBUG;
74 else if (s == "TRACE")
75 return LogLevel::TRACE;
76 else if (s == "ALL")
77 return LogLevel::ALL;
78
79 BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized log level '" + s + "'"));
80}
81
82Logger::Logger(const std::string& name)
83 : m_moduleName(name)
84{
85 this->setLevel(LogLevel::NONE);
86 Logging::addLogger(*this);
87}
88
89std::ostream&
90operator<<(std::ostream& os, const LoggerTimestamp&)
91{
92 using namespace ndn::time;
93
94 static const microseconds::rep ONE_SECOND = 1000000;
95 microseconds::rep usecs = duration_cast<microseconds>(
96 system_clock::now().time_since_epoch()).count();
97
98 // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
99 char buffer[10 + 1 + 6 + 1];
100 BOOST_ASSERT_MSG(usecs / ONE_SECOND <= 9999999999L,
101 "whole seconds cannot fit in 10 characters");
102
103 static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
104 "PRIdLEAST64 is incompatible with microseconds::rep");
105 // std::snprintf unavailable in some environments <https://redmine.named-data.net/issues/2299>
106 snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
107 usecs / ONE_SECOND, usecs % ONE_SECOND);
108
109 return os << buffer;
110}
111
112} // namespace util
113} // namespace ndn