Alexander Afanasyev | 216df01 | 2015-02-10 17:35:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_ANDROID_CUSTOM_LOGGER_HPP |
| 27 | #define NFD_ANDROID_CUSTOM_LOGGER_HPP |
| 28 | |
| 29 | #include "common.hpp" |
| 30 | #include <android/log.h> |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | /** \brief indicates a log level |
| 35 | * \note This type is internal. Logger should be accessed through NFD_LOG_* macros. |
| 36 | */ |
| 37 | enum LogLevel { |
| 38 | LOG_FATAL = -1, // fatal (will be logged unconditionally) |
| 39 | LOG_NONE = 0, // no messages |
| 40 | LOG_ERROR = 1, // serious error messages |
| 41 | LOG_WARN = 2, // warning messages |
| 42 | LOG_INFO = 3, // informational messages |
| 43 | LOG_DEBUG = 4, // debug messages |
| 44 | LOG_TRACE = 5, // trace messages (most verbose) |
| 45 | LOG_ALL = 255 // all messages |
| 46 | }; |
| 47 | |
| 48 | /** \brief provides logging for a module |
| 49 | * \note This type is internal. Logger should be accessed through NFD_LOG_* macros. |
| 50 | * \note This type is copyable because logger can be declared as a field of |
| 51 | * (usually template) classes, and shouldn't prevent those classes to be copyable. |
| 52 | */ |
| 53 | class Logger |
| 54 | { |
| 55 | public: |
| 56 | Logger(const std::string& name, LogLevel level); |
| 57 | |
| 58 | bool |
| 59 | isEnabled(LogLevel level) const |
| 60 | { |
| 61 | return m_enabledLogLevel >= level; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | setLogLevel(LogLevel level) |
| 66 | { |
| 67 | m_enabledLogLevel = level; |
| 68 | } |
| 69 | |
| 70 | const std::string& |
| 71 | getName() const |
| 72 | { |
| 73 | return m_moduleName; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | setName(const std::string& name) |
| 78 | { |
| 79 | m_moduleName = name; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | std::string m_moduleName; |
| 84 | LogLevel m_enabledLogLevel; |
| 85 | }; |
| 86 | |
| 87 | inline std::ostream& |
| 88 | operator<<(std::ostream& output, const Logger& logger) |
| 89 | { |
| 90 | output << logger.getName(); |
| 91 | return output; |
| 92 | } |
| 93 | |
| 94 | } // namespace nfd |
| 95 | |
| 96 | #include "core/logger-factory.hpp" |
| 97 | |
| 98 | namespace nfd { |
| 99 | |
| 100 | #define NFD_LOG_INIT(name) \ |
| 101 | static nfd::Logger& g_logger = nfd::LoggerFactory::create(name) |
| 102 | |
| 103 | #define NFD_LOG_INCLASS_DECLARE() \ |
| 104 | static nfd::Logger& g_logger |
| 105 | |
| 106 | #define NFD_LOG_INCLASS_DEFINE(cls, name) \ |
| 107 | nfd::Logger& cls::g_logger = nfd::LoggerFactory::create(name) |
| 108 | |
| 109 | #define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \ |
| 110 | template<class T> \ |
| 111 | nfd::Logger& cls<T>::g_logger = nfd::LoggerFactory::create(name) |
| 112 | |
| 113 | #define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \ |
| 114 | template<> \ |
| 115 | nfd::Logger& cls<specialization>::g_logger = nfd::LoggerFactory::create(name) |
| 116 | |
| 117 | #define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \ |
| 118 | template<> \ |
| 119 | nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name) |
| 120 | |
| 121 | #define NFD_LOG(nfdLevel, androidLevel, msg, expression) \ |
| 122 | do { \ |
| 123 | if (g_logger.isEnabled(::nfd::LOG_##nfdLevel)) { \ |
| 124 | std::ostringstream os; \ |
| 125 | os << expression; \ |
| 126 | __android_log_print(ANDROID_LOG_##androidLevel, \ |
| 127 | g_logger.getName().c_str(), "%s", os.str().c_str()); \ |
| 128 | } \ |
| 129 | } while (false) |
| 130 | |
| 131 | #define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, VERBOSE, TRACE, expression) |
| 132 | #define NFD_LOG_DEBUG(expression) NFD_LOG(DEBUG, DEBUG, DEBUG, expression) |
| 133 | #define NFD_LOG_INFO(expression) NFD_LOG(INFO, INFO, INFO, expression) |
| 134 | #define NFD_LOG_WARN(expression) NFD_LOG(WARN, WARN, WARNING, expression) |
| 135 | #define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, ERROR, ERROR, expression) |
| 136 | #define NFD_LOG_FATAL(expression) NFD_LOG(FATAL, FATAL, FATAL, expression) |
| 137 | |
| 138 | } // namespace nfd |
| 139 | |
| 140 | #endif // NFD_ANDROID_CUSTOM_LOGGER_HPP |