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 | 233f67c | 2018-01-21 17:23:21 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 24 | #include "logging.hpp" |
| 25 | #include "time.hpp" |
| 26 | |
Davide Pesavento | 233f67c | 2018-01-21 17:23:21 -0500 | [diff] [blame] | 27 | #include <cinttypes> // for PRIdLEAST64 |
| 28 | #include <cstdlib> // for std::abs() |
| 29 | #include <cstring> // for std::strspn() |
| 30 | #include <stdio.h> // for snprintf() |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace util { |
| 34 | |
| 35 | std::ostream& |
| 36 | operator<<(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 | |
| 60 | LogLevel |
| 61 | parseLogLevel(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 | |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 83 | /** |
| 84 | * \brief checks if incoming logger name meets criteria |
| 85 | * \param name name of logger |
| 86 | */ |
| 87 | static bool |
| 88 | isValidLoggerName(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 Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 104 | Logger::Logger(const std::string& name) |
| 105 | : m_moduleName(name) |
| 106 | { |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 107 | if (!isValidLoggerName(name)) { |
Davide Pesavento | 233f67c | 2018-01-21 17:23:21 -0500 | [diff] [blame] | 108 | BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + name + "' is invalid")); |
dmcoomes | e062a18 | 2017-06-12 11:10:31 -0500 | [diff] [blame] | 109 | } |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 110 | this->setLevel(LogLevel::NONE); |
| 111 | Logging::addLogger(*this); |
| 112 | } |
| 113 | |
| 114 | std::ostream& |
| 115 | operator<<(std::ostream& os, const LoggerTimestamp&) |
| 116 | { |
| 117 | using namespace ndn::time; |
| 118 | |
Davide Pesavento | 233f67c | 2018-01-21 17:23:21 -0500 | [diff] [blame] | 119 | const auto sinceEpoch = system_clock::now().time_since_epoch(); |
| 120 | BOOST_ASSERT(sinceEpoch.count() >= 0); |
| 121 | // use abs() to silence truncation warning in snprintf(), see #4365 |
| 122 | const auto usecs = std::abs(duration_cast<microseconds>(sinceEpoch).count()); |
| 123 | const auto usecsPerSec = microseconds::period::den; |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 124 | |
| 125 | // 10 (whole seconds) + '.' + 6 (fraction) + '\0' |
| 126 | char buffer[10 + 1 + 6 + 1]; |
Davide Pesavento | 233f67c | 2018-01-21 17:23:21 -0500 | [diff] [blame] | 127 | BOOST_ASSERT_MSG(usecs / usecsPerSec <= 9999999999, "whole seconds cannot fit in 10 characters"); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 128 | |
| 129 | static_assert(std::is_same<microseconds::rep, int_least64_t>::value, |
| 130 | "PRIdLEAST64 is incompatible with microseconds::rep"); |
Davide Pesavento | 233f67c | 2018-01-21 17:23:21 -0500 | [diff] [blame] | 131 | // std::snprintf unavailable on some platforms, see #2299 |
| 132 | ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64, |
| 133 | usecs / usecsPerSec, usecs % usecsPerSec); |
Junxiao Shi | 7d05427 | 2016-08-04 17:00:41 +0000 | [diff] [blame] | 134 | |
| 135 | return os << buffer; |
| 136 | } |
| 137 | |
| 138 | } // namespace util |
| 139 | } // namespace ndn |