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