Alexander Afanasyev | a8d404b | 2016-11-05 10:07:08 -0600 | [diff] [blame] | 1 | /* -*- 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 | #ifndef NFD_ANDROID_NDN_CXX_ANDROID_NDN_CXX_CUSTOM_LOGGER_HPP |
| 23 | #define NFD_ANDROID_NDN_CXX_ANDROID_NDN_CXX_CUSTOM_LOGGER_HPP |
| 24 | |
| 25 | #include "common.hpp" |
| 26 | #include <atomic> |
| 27 | #include <android/log.h> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace util { |
| 31 | |
| 32 | /** \brief indicates the severity level of a log message |
| 33 | */ |
| 34 | enum class LogLevel { |
| 35 | FATAL = -1, ///< fatal (will be logged unconditionally) |
| 36 | NONE = 0, ///< no messages |
| 37 | ERROR = 1, ///< serious error messages |
| 38 | WARN = 2, ///< warning messages |
| 39 | INFO = 3, ///< informational messages |
| 40 | DEBUG = 4, ///< debug messages |
| 41 | TRACE = 5, ///< trace messages (most verbose) |
| 42 | ALL = 255 ///< all messages |
| 43 | }; |
| 44 | |
| 45 | /** \brief output LogLevel as string |
| 46 | * \throw std::invalid_argument unknown \p level |
| 47 | */ |
| 48 | std::ostream& |
| 49 | operator<<(std::ostream& os, LogLevel level); |
| 50 | |
| 51 | /** \brief parse LogLevel from string |
| 52 | * \throw std::invalid_argument unknown level name |
| 53 | */ |
| 54 | LogLevel |
| 55 | parseLogLevel(const std::string& s); |
| 56 | |
| 57 | /** \brief represents a logger in logging facility |
| 58 | * \note User should declare a new logger with \p NDN_LOG_INIT macro. |
| 59 | */ |
| 60 | class Logger |
| 61 | { |
| 62 | public: |
| 63 | explicit |
| 64 | Logger(const std::string& name); |
| 65 | |
| 66 | const std::string& |
| 67 | getModuleName() const |
| 68 | { |
| 69 | return m_moduleName; |
| 70 | } |
| 71 | |
| 72 | bool |
| 73 | isLevelEnabled(LogLevel level) const |
| 74 | { |
| 75 | return m_currentLevel.load(std::memory_order_relaxed) >= level; |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | setLevel(LogLevel level) |
| 80 | { |
| 81 | m_currentLevel.store(level, std::memory_order_relaxed); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | const std::string m_moduleName; |
| 86 | std::atomic<LogLevel> m_currentLevel; |
| 87 | }; |
| 88 | |
| 89 | /** \brief declare a log module |
| 90 | */ |
| 91 | #define NDN_LOG_INIT(name) \ |
| 92 | namespace { \ |
| 93 | inline ::ndn::util::Logger& getNdnCxxLogger() \ |
| 94 | { \ |
| 95 | static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \ |
| 96 | return logger; \ |
| 97 | } \ |
| 98 | } \ |
| 99 | struct ndn_cxx__allow_trailing_semicolon |
| 100 | |
| 101 | #define NDN_LOG(level, androidLevel, msg, expression) \ |
| 102 | do { \ |
| 103 | if (getNdnCxxLogger().isLevelEnabled(::ndn::util::LogLevel::level)) { \ |
| 104 | std::ostringstream os; \ |
| 105 | os << expression; \ |
| 106 | __android_log_print(ANDROID_LOG_##androidLevel, \ |
| 107 | getNdnCxxLogger().getModuleName().c_str(), "%s", os.str().c_str()); \ |
| 108 | } \ |
| 109 | } while (false) |
| 110 | |
| 111 | #define NDN_LOG_TRACE(expression) NDN_LOG(TRACE, VERBOSE, TRACE, expression) |
| 112 | #define NDN_LOG_DEBUG(expression) NDN_LOG(DEBUG, DEBUG, DEBUG, expression) |
| 113 | #define NDN_LOG_INFO(expression) NDN_LOG(INFO, INFO, INFO, expression) |
| 114 | #define NDN_LOG_WARN(expression) NDN_LOG(WARN, WARN, WARNING, expression) |
| 115 | #define NDN_LOG_ERROR(expression) NDN_LOG(ERROR, ERROR, ERROR, expression) |
| 116 | #define NDN_LOG_FATAL(expression) NDN_LOG(FATAL, FATAL, FATAL, expression) |
| 117 | |
| 118 | } // namespace util |
| 119 | } // namespace ndn |
| 120 | |
| 121 | #endif // NFD_ANDROID_NDN_CXX_ANDROID_NDN_CXX_CUSTOM_LOGGER_HPP |