blob: 604c3041dabdda0a635315d4adfb7df428c1edfe [file] [log] [blame]
Ilya Moiseenkoa807e652014-01-28 11:51:01 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 *
6 * Author: Ilya Moiseenko <iliamo@ucla.edu>
7 */
8
9#ifndef NFD_CORE_LOGGER_HPP
10#define NFD_CORE_LOGGER_HPP
11
12#include "common.hpp"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080013
14namespace nfd {
15
16enum LogLevel {
17 LOG_NONE = 0, // no messages
18 LOG_ERROR = 1, // serious error messages
19 LOG_WARN = 2, // warning messages
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080020 LOG_INFO = 3, // informational messages
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080021 LOG_DEBUG = 4, // debug messages
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080022 LOG_TRACE = 5, // trace messages (most verbose)
23 // LOG_FATAL is not a level and is logged unconditionally
24 LOG_ALL = 255, // all messages
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080025};
26
27class Logger
28{
29public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060030
31 Logger(const std::string& name, LogLevel level)
32 : m_moduleName(name)
33 , m_enabledLogLevel(level)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080034 {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060035 }
36
37 bool
38 isEnabled(LogLevel level) const
39 {
40 // std::cerr << m_moduleName <<
41 // " enabled = " << m_enabledLogLevel
42 // << " level = " << level << std::endl;
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080043 return (m_enabledLogLevel >= level);
44 }
45
46 void
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060047 setLogLevel(LogLevel level)
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080048 {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060049 m_enabledLogLevel = level;
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080050 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060051
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080052 const std::string&
53 getName() const
54 {
55 return m_moduleName;
56 }
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060057
58 void
59 setName(const std::string& name)
60 {
61 m_moduleName = name;
62 }
63
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080064private:
65 std::string m_moduleName;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060066 LogLevel m_enabledLogLevel;
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080067};
68
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060069inline std::ostream&
70operator<<(std::ostream& output, const Logger& obj)
71{
72 output << obj.getName();
73 return output;
74}
75
76} // namespace nfd
77
78#include "core/logger-factory.hpp"
79
80namespace nfd {
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080081
Alexander Afanasyev66886812014-01-31 14:48:48 -080082#define NFD_LOG_INIT(name) \
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060083static nfd::Logger& g_logger = nfd::LoggerFactory::create(name);
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080084
Alexander Afanasyev66886812014-01-31 14:48:48 -080085#define NFD_LOG_INCLASS_DECLARE() \
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060086static nfd::Logger& g_logger;
Alexander Afanasyev66886812014-01-31 14:48:48 -080087
88#define NFD_LOG_INCLASS_DEFINE(cls, name) \
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060089nfd::Logger& cls::g_logger = nfd::LoggerFactory::create(name);
Alexander Afanasyev66886812014-01-31 14:48:48 -080090
91#define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060092template<class T> \
93nfd::Logger& cls<T>::g_logger = nfd::LoggerFactory::create(name);
Alexander Afanasyev66886812014-01-31 14:48:48 -080094
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060095#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \
96template<> \
97nfd::Logger& cls<specialization>::g_logger = nfd::LoggerFactory::create(name);
Alexander Afanasyev66886812014-01-31 14:48:48 -080098
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080099#define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600100template<> \
101nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name);
102
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800103
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800104#define NFD_LOG_TRACE(expression) \
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600105if (g_logger.isEnabled(nfd::LOG_TRACE)) \
106 std::cerr<<"TRACE: "<<"["<<g_logger<<"] " << expression << "\n"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800107
108#define NFD_LOG_DEBUG(expression)\
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600109if (g_logger.isEnabled(nfd::LOG_DEBUG)) \
110 std::cerr<<"DEBUG: "<<"["<<g_logger<<"] " << expression <<"\n"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800111
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600112#define NFD_LOG_WARN(expression) \
113if (g_logger.isEnabled(nfd::LOG_WARN)) \
114 std::cerr<<"WARNING: "<<"["<<g_logger<<"] " << expression <<"\n"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800115
116#define NFD_LOG_INFO(expression)\
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600117if (g_logger.isEnabled(nfd::LOG_INFO)) \
118 std::cerr<<"INFO: "<<"["<<g_logger<<"] " << expression <<"\n"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800119
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600120#define NFD_LOG_ERROR(expression) \
121if (g_logger.isEnabled(nfd::LOG_ERROR)) \
122 std::cerr<<"ERROR: "<<"["<<g_logger<<"] " << expression <<"\n"
123
124#define NFD_LOG_FATAL(expression)\
125std::cerr<<"FATAL: "<<"["<<g_logger<<"] " << expression <<"\n"
126
127} //namespace nfd
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800128
129#endif