blob: e4b3b41025c1b5fe9d383aa2629745854555933c [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
Junxiao Shi7d054272016-08-04 17:00:41 +000024#include "time.hpp"
25
26#include <cinttypes>
dmcoomese062a182017-06-12 11:10:31 -050027#include <cstring>
Junxiao Shi7d054272016-08-04 17:00:41 +000028
29namespace ndn {
30namespace util {
31
32std::ostream&
33operator<<(std::ostream& os, LogLevel level)
34{
35 switch (level) {
36 case LogLevel::FATAL:
37 return os << "FATAL";
38 case LogLevel::NONE:
39 return os << "NONE";
40 case LogLevel::ERROR:
41 return os << "ERROR";
42 case LogLevel::WARN:
43 return os << "WARN";
44 case LogLevel::INFO:
45 return os << "INFO";
46 case LogLevel::DEBUG:
47 return os << "DEBUG";
48 case LogLevel::TRACE:
49 return os << "TRACE";
50 case LogLevel::ALL:
51 return os << "ALL";
52 }
53
54 BOOST_THROW_EXCEPTION(std::invalid_argument("unknown log level " + to_string(static_cast<int>(level))));
55}
56
57LogLevel
58parseLogLevel(const std::string& s)
59{
60 if (s == "FATAL")
61 return LogLevel::FATAL;
62 else if (s == "NONE")
63 return LogLevel::NONE;
64 else if (s == "ERROR")
65 return LogLevel::ERROR;
66 else if (s == "WARN")
67 return LogLevel::WARN;
68 else if (s == "INFO")
69 return LogLevel::INFO;
70 else if (s == "DEBUG")
71 return LogLevel::DEBUG;
72 else if (s == "TRACE")
73 return LogLevel::TRACE;
74 else if (s == "ALL")
75 return LogLevel::ALL;
76
77 BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized log level '" + s + "'"));
78}
79
dmcoomese062a182017-06-12 11:10:31 -050080/**
81 * \brief checks if incoming logger name meets criteria
82 * \param name name of logger
83 */
84static bool
85isValidLoggerName(const std::string& name)
86{
87 // acceptable characters for Logger name
88 const char* okChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-";
89 if (std::strspn(name.c_str(), okChars) != name.size()) {
90 return false;
91 }
92 if (name.empty() || name.front() == '.' || name.back() == '.') {
93 return false;
94 }
95 if (name.find("..") != std::string::npos) {
96 return false;
97 }
98 return true;
99}
100
Junxiao Shi7d054272016-08-04 17:00:41 +0000101Logger::Logger(const std::string& name)
102 : m_moduleName(name)
103{
Junxiao Shi7d054272016-08-04 17:00:41 +0000104}
105
106} // namespace util
107} // namespace ndn