blob: aad23943d9a4d3cf492d5cd38ac487c299571a88 [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"
13#include <iostream>
14
15namespace nfd {
16
17enum LogLevel {
18 LOG_NONE = 0, // no messages
19 LOG_ERROR = 1, // serious error messages
20 LOG_WARN = 2, // warning messages
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080021 LOG_INFO = 3, // informational messages
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080022 LOG_DEBUG = 4, // debug messages
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080023 LOG_TRACE = 5, // trace messages (most verbose)
24 // LOG_FATAL is not a level and is logged unconditionally
25 LOG_ALL = 255, // all messages
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080026};
27
28class Logger
29{
30public:
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080031 explicit
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080032 Logger(const std::string& name);
33
34 bool
35 isEnabled(LogLevel level)
36 {
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080037 return (m_enabledLogLevel >= level);
38 }
39
40 void
41 setLogLevel(uint32_t level)
42 {
43 m_enabledLogLevel = static_cast<LogLevel>(level);
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080044 }
45
46 const std::string&
47 getName() const
48 {
49 return m_moduleName;
50 }
51
52private:
53 std::string m_moduleName;
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080054 uint32_t m_enabledLogLevel;
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080055};
56
57std::ostream&
58operator<<(std::ostream& output, const Logger& obj);
59
Alexander Afanasyev66886812014-01-31 14:48:48 -080060#define NFD_LOG_INIT(name) \
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080061 static nfd::Logger \
62 g_logger = nfd::Logger(name);
63
Alexander Afanasyev66886812014-01-31 14:48:48 -080064#define NFD_LOG_INCLASS_DECLARE() \
65 static nfd::Logger g_logger;
66
67#define NFD_LOG_INCLASS_DEFINE(cls, name) \
68 nfd::Logger cls::g_logger = nfd::Logger(name);
69
70#define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \
71 template<class T> \
72 nfd::Logger cls<T>::g_logger = nfd::Logger(name);
73
74#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \
75 template<> \
76 nfd::Logger cls<specialization>::g_logger = nfd::Logger(name);
77
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080078#define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \
79 template<> \
80 nfd::Logger cls<s1, s2>::g_logger = nfd::Logger(name);
81
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080082#define NFD_LOG_TRACE(expression) \
83 if(g_logger.isEnabled(nfd::LOG_TRACE)) \
84 std::cerr<<"TRACE: "<<"["<<g_logger<<"] " << expression << "\n"
85
86#define NFD_LOG_DEBUG(expression)\
87 if(g_logger.isEnabled(nfd::LOG_DEBUG)) \
88 std::cerr<<"DEBUG: "<<"["<<g_logger<<"] " << expression <<"\n"
89
90#define NFD_LOG_WARN(expression) \
91 if(g_logger.isEnabled(nfd::LOG_WARN)) \
92 std::cerr<<"WARNING: "<<"["<<g_logger<<"] " << expression <<"\n"
93
94#define NFD_LOG_INFO(expression)\
95 if(g_logger.isEnabled(nfd::LOG_INFO)) \
96 std::cerr<<"INFO: "<<"["<<g_logger<<"] " << expression <<"\n"
97
98#define NFD_LOG_ERROR(expression)\
99 if(g_logger.isEnabled(nfd::LOG_ERROR)) \
100 std::cerr<<"ERROR: "<<"["<<g_logger<<"] " << expression <<"\n"
101
102#define NFD_LOG_FATAL(expression)\
Alexander Afanasyevb84cc922014-02-24 17:52:58 -0800103 std::cerr<<"FATAL: "<<"["<<g_logger<<"] " << expression <<"\n"
Ilya Moiseenkoa807e652014-01-28 11:51:01 -0800104
105} //namespace nfd
106
107
108#endif