blob: 86ed64c9bfc8d7df1c22fb8228f3027ff619a0eb [file] [log] [blame]
Alexander Afanasyev216df012015-02-10 17:35:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev79a0fff2017-12-05 12:45:14 -05003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -06004 * 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.
Alexander Afanasyev216df012015-02-10 17:35:46 -080010 *
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
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -060029#include "core/common.hpp"
Alexander Afanasyev79a0fff2017-12-05 12:45:14 -050030#include <sstream>
Alexander Afanasyev216df012015-02-10 17:35:46 -080031#include <android/log.h>
32
33namespace nfd {
34
35/** \brief indicates a log level
36 * \note This type is internal. Logger should be accessed through NFD_LOG_* macros.
37 */
38enum LogLevel {
39 LOG_FATAL = -1, // fatal (will be logged unconditionally)
40 LOG_NONE = 0, // no messages
41 LOG_ERROR = 1, // serious error messages
42 LOG_WARN = 2, // warning messages
43 LOG_INFO = 3, // informational messages
44 LOG_DEBUG = 4, // debug messages
45 LOG_TRACE = 5, // trace messages (most verbose)
46 LOG_ALL = 255 // all messages
47};
48
49/** \brief provides logging for a module
50 * \note This type is internal. Logger should be accessed through NFD_LOG_* macros.
51 * \note This type is copyable because logger can be declared as a field of
52 * (usually template) classes, and shouldn't prevent those classes to be copyable.
53 */
54class Logger
55{
56public:
57 Logger(const std::string& name, LogLevel level);
58
59 bool
60 isEnabled(LogLevel level) const
61 {
62 return m_enabledLogLevel >= level;
63 }
64
65 void
66 setLogLevel(LogLevel level)
67 {
68 m_enabledLogLevel = level;
69 }
70
71 const std::string&
72 getName() const
73 {
74 return m_moduleName;
75 }
76
77 void
78 setName(const std::string& name)
79 {
80 m_moduleName = name;
81 }
82
83private:
84 std::string m_moduleName;
85 LogLevel m_enabledLogLevel;
86};
87
88inline std::ostream&
89operator<<(std::ostream& output, const Logger& logger)
90{
91 output << logger.getName();
92 return output;
93}
94
95} // namespace nfd
96
97#include "core/logger-factory.hpp"
98
99namespace nfd {
100
101#define NFD_LOG_INIT(name) \
102static nfd::Logger& g_logger = nfd::LoggerFactory::create(name)
103
104#define NFD_LOG_INCLASS_DECLARE() \
105static nfd::Logger& g_logger
106
107#define NFD_LOG_INCLASS_DEFINE(cls, name) \
108nfd::Logger& cls::g_logger = nfd::LoggerFactory::create(name)
109
110#define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \
111template<class T> \
112nfd::Logger& cls<T>::g_logger = nfd::LoggerFactory::create(name)
113
114#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \
115template<> \
116nfd::Logger& cls<specialization>::g_logger = nfd::LoggerFactory::create(name)
117
118#define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \
119template<> \
120nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name)
121
122#define NFD_LOG(nfdLevel, androidLevel, msg, expression) \
123do { \
124 if (g_logger.isEnabled(::nfd::LOG_##nfdLevel)) { \
125 std::ostringstream os; \
126 os << expression; \
127 __android_log_print(ANDROID_LOG_##androidLevel, \
128 g_logger.getName().c_str(), "%s", os.str().c_str()); \
129 } \
130} while (false)
131
132#define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, VERBOSE, TRACE, expression)
133#define NFD_LOG_DEBUG(expression) NFD_LOG(DEBUG, DEBUG, DEBUG, expression)
134#define NFD_LOG_INFO(expression) NFD_LOG(INFO, INFO, INFO, expression)
135#define NFD_LOG_WARN(expression) NFD_LOG(WARN, WARN, WARNING, expression)
136#define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, ERROR, ERROR, expression)
137#define NFD_LOG_FATAL(expression) NFD_LOG(FATAL, FATAL, FATAL, expression)
138
139} // namespace nfd
140
141#endif // NFD_ANDROID_CUSTOM_LOGGER_HPP