blob: 54af7cbb9f8b8a0aa6b1ca026cad33d2697e320e [file] [log] [blame]
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev8269a052015-02-09 16:25:36 -08003 * Copyright (c) 2014-2015, 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.
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * 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/>.
Junxiao Shi70db9922014-11-16 21:12:03 -070024 */
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080025
26#ifndef NFD_CORE_LOGGER_HPP
27#define NFD_CORE_LOGGER_HPP
28
29#include "common.hpp"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080030
Alexander Afanasyev8269a052015-02-09 16:25:36 -080031#ifdef HAVE_CUSTOM_LOGGER
32#include "custom-logger.hpp"
33#else
34
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -080035#include <mutex>
36
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080037namespace nfd {
38
Junxiao Shi70db9922014-11-16 21:12:03 -070039/** \brief indicates a log level
40 * \note This type is internal. Logger should be accessed through NFD_LOG_* macros.
41 */
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080042enum LogLevel {
Alexander Afanasyev8269a052015-02-09 16:25:36 -080043 LOG_FATAL = -1, // fatal (will be logged unconditionally)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080044 LOG_NONE = 0, // no messages
45 LOG_ERROR = 1, // serious error messages
46 LOG_WARN = 2, // warning messages
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080047 LOG_INFO = 3, // informational messages
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080048 LOG_DEBUG = 4, // debug messages
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080049 LOG_TRACE = 5, // trace messages (most verbose)
Davide Pesavento1bdef282014-04-08 20:59:50 +020050 LOG_ALL = 255 // all messages
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080051};
52
Junxiao Shi70db9922014-11-16 21:12:03 -070053/** \brief provides logging for a module
54 * \note This type is internal. Logger should be accessed through NFD_LOG_* macros.
55 * \note This type is copyable because logger can be declared as a field of
56 * (usually template) classes, and shouldn't prevent those classes to be copyable.
57 */
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080058class Logger
59{
60public:
Junxiao Shi70db9922014-11-16 21:12:03 -070061 Logger(const std::string& name, LogLevel level);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060062
63 bool
64 isEnabled(LogLevel level) const
65 {
Alexander Afanasyev8269a052015-02-09 16:25:36 -080066 return m_enabledLogLevel >= level;
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080067 }
68
69 void
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060070 setLogLevel(LogLevel level)
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080071 {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060072 m_enabledLogLevel = level;
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080073 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060074
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080075 const std::string&
76 getName() const
77 {
78 return m_moduleName;
79 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060080
81 void
82 setName(const std::string& name)
83 {
84 m_moduleName = name;
85 }
86
Junxiao Shi70db9922014-11-16 21:12:03 -070087 /** \return string representation of time since epoch: seconds.microseconds
88 * \warning Return value is in a statically allocated buffer,
89 * which subsequent calls will overwrite.
90 */
91 static const char*
92 now();
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -060093
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080094private:
95 std::string m_moduleName;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060096 LogLevel m_enabledLogLevel;
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080097};
98
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060099inline std::ostream&
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600100operator<<(std::ostream& output, const Logger& logger)
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600101{
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600102 output << logger.getName();
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600103 return output;
104}
105
106} // namespace nfd
107
108#include "core/logger-factory.hpp"
109
110namespace nfd {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800111
Davide Pesavento1bdef282014-04-08 20:59:50 +0200112#define NFD_LOG_INIT(name) \
113static nfd::Logger& g_logger = nfd::LoggerFactory::create(name)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800114
Davide Pesavento1bdef282014-04-08 20:59:50 +0200115#define NFD_LOG_INCLASS_DECLARE() \
116static nfd::Logger& g_logger
Alexander Afanasyev66886812014-01-31 14:48:48 -0800117
Davide Pesavento1bdef282014-04-08 20:59:50 +0200118#define NFD_LOG_INCLASS_DEFINE(cls, name) \
119nfd::Logger& cls::g_logger = nfd::LoggerFactory::create(name)
Alexander Afanasyev66886812014-01-31 14:48:48 -0800120
Davide Pesavento1bdef282014-04-08 20:59:50 +0200121#define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \
122template<class T> \
123nfd::Logger& cls<T>::g_logger = nfd::LoggerFactory::create(name)
Alexander Afanasyev66886812014-01-31 14:48:48 -0800124
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600125#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \
Davide Pesavento1bdef282014-04-08 20:59:50 +0200126template<> \
127nfd::Logger& cls<specialization>::g_logger = nfd::LoggerFactory::create(name)
Alexander Afanasyev66886812014-01-31 14:48:48 -0800128
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800129#define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \
Davide Pesavento1bdef282014-04-08 20:59:50 +0200130template<> \
131nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name)
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600132
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -0800133extern std::mutex g_logMutex;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800134
Junxiao Shi70db9922014-11-16 21:12:03 -0700135#define NFD_LOG(level, msg, expression) \
136do { \
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -0800137 if (g_logger.isEnabled(::nfd::LOG_##level)) { \
138 std::lock_guard<std::mutex> lock(::nfd::g_logMutex); \
Junxiao Shi70db9922014-11-16 21:12:03 -0700139 std::clog << ::nfd::Logger::now() << " "#msg": " \
140 << "[" << g_logger << "] " << expression << "\n"; \
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -0800141 } \
Steve DiBenedetto3a61fb42014-04-04 10:32:51 -0600142} while (false)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800143
Junxiao Shi70db9922014-11-16 21:12:03 -0700144#define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, TRACE, expression)
145#define NFD_LOG_DEBUG(expression) NFD_LOG(DEBUG, DEBUG, expression)
146#define NFD_LOG_INFO(expression) NFD_LOG(INFO, INFO, expression)
147#define NFD_LOG_WARN(expression) NFD_LOG(WARN, WARNING, expression)
148#define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, ERROR, expression)
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800149#define NFD_LOG_FATAL(expression) NFD_LOG(FATAL, FATAL, expression)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800150
Junxiao Shi70db9922014-11-16 21:12:03 -0700151} // namespace nfd
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600152
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800153#endif // HAVE_CUSTOM_LOGGER
154
Junxiao Shi70db9922014-11-16 21:12:03 -0700155#endif // NFD_CORE_LOGGER_HPP