Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 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 | |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 24 | #include "time.hpp" |
| 25 | |
| 26 | #include <cinttypes> |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 27 | #include <cstring> |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace util { |
| 31 | |
| 32 | std::ostream& |
| 33 | operator<<(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 | |
| 57 | LogLevel |
| 58 | parseLogLevel(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 | |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 80 | /** |
| 81 | * \brief checks if incoming logger name meets criteria |
| 82 | * \param name name of logger |
| 83 | */ |
| 84 | static bool |
| 85 | isValidLoggerName(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 Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 101 | Logger::Logger(const std::string& name) |
| 102 | : m_moduleName(name) |
| 103 | { |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace util |
| 107 | } // namespace ndn |