blob: f8c6e8eaeb41922a15e838f28ee95769933f4db5 [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
21 LOG_TRACE = 3, // trace messages
22 LOG_DEBUG = 4, // debug messages
23 LOG_INFO = 5, // informational messages
24 LOG_FATAL = 6, // fatal error messages
25 LOG_ALL = 0x0fffffff, // all messages
26};
27
28class Logger
29{
30public:
31 Logger(const std::string& name);
32
33 bool
34 isEnabled(LogLevel level)
35 {
36 return m_isEnabled;
37 }
38
39 const std::string&
40 getName() const
41 {
42 return m_moduleName;
43 }
44
45private:
46 std::string m_moduleName;
47 bool m_isEnabled;
48};
49
50std::ostream&
51operator<<(std::ostream& output, const Logger& obj);
52
Alexander Afanasyev66886812014-01-31 14:48:48 -080053#define NFD_LOG_INIT(name) \
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080054 static nfd::Logger \
55 g_logger = nfd::Logger(name);
56
Alexander Afanasyev66886812014-01-31 14:48:48 -080057#define NFD_LOG_INCLASS_DECLARE() \
58 static nfd::Logger g_logger;
59
60#define NFD_LOG_INCLASS_DEFINE(cls, name) \
61 nfd::Logger cls::g_logger = nfd::Logger(name);
62
63#define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \
64 template<class T> \
65 nfd::Logger cls<T>::g_logger = nfd::Logger(name);
66
67#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \
68 template<> \
69 nfd::Logger cls<specialization>::g_logger = nfd::Logger(name);
70
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080071#define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \
72 template<> \
73 nfd::Logger cls<s1, s2>::g_logger = nfd::Logger(name);
74
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080075#define NFD_LOG_TRACE(expression) \
76 if(g_logger.isEnabled(nfd::LOG_TRACE)) \
77 std::cerr<<"TRACE: "<<"["<<g_logger<<"] " << expression << "\n"
78
79#define NFD_LOG_DEBUG(expression)\
80 if(g_logger.isEnabled(nfd::LOG_DEBUG)) \
81 std::cerr<<"DEBUG: "<<"["<<g_logger<<"] " << expression <<"\n"
82
83#define NFD_LOG_WARN(expression) \
84 if(g_logger.isEnabled(nfd::LOG_WARN)) \
85 std::cerr<<"WARNING: "<<"["<<g_logger<<"] " << expression <<"\n"
86
87#define NFD_LOG_INFO(expression)\
88 if(g_logger.isEnabled(nfd::LOG_INFO)) \
89 std::cerr<<"INFO: "<<"["<<g_logger<<"] " << expression <<"\n"
90
91#define NFD_LOG_ERROR(expression)\
92 if(g_logger.isEnabled(nfd::LOG_ERROR)) \
93 std::cerr<<"ERROR: "<<"["<<g_logger<<"] " << expression <<"\n"
94
95#define NFD_LOG_FATAL(expression)\
96 if(g_logger.isEnabled(nfd::LOG_FATAL)) \
97 std::cerr<<"FATAL: "<<"["<<g_logger<<"] " << expression <<"\n"
98
99} //namespace nfd
100
101
102#endif