Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame^] | 1 | /* -*- 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 | |
| 15 | namespace nfd { |
| 16 | |
| 17 | enum 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 | |
| 28 | class Logger |
| 29 | { |
| 30 | public: |
| 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 | |
| 45 | private: |
| 46 | std::string m_moduleName; |
| 47 | bool m_isEnabled; |
| 48 | }; |
| 49 | |
| 50 | std::ostream& |
| 51 | operator<<(std::ostream& output, const Logger& obj); |
| 52 | |
| 53 | #define NFD_LOG_INIT(name) \ |
| 54 | static nfd::Logger \ |
| 55 | g_logger = nfd::Logger(name); |
| 56 | |
| 57 | #define NFD_LOG_TRACE(expression) \ |
| 58 | if(g_logger.isEnabled(nfd::LOG_TRACE)) \ |
| 59 | std::cerr<<"TRACE: "<<"["<<g_logger<<"] " << expression << "\n" |
| 60 | |
| 61 | #define NFD_LOG_DEBUG(expression)\ |
| 62 | if(g_logger.isEnabled(nfd::LOG_DEBUG)) \ |
| 63 | std::cerr<<"DEBUG: "<<"["<<g_logger<<"] " << expression <<"\n" |
| 64 | |
| 65 | #define NFD_LOG_WARN(expression) \ |
| 66 | if(g_logger.isEnabled(nfd::LOG_WARN)) \ |
| 67 | std::cerr<<"WARNING: "<<"["<<g_logger<<"] " << expression <<"\n" |
| 68 | |
| 69 | #define NFD_LOG_INFO(expression)\ |
| 70 | if(g_logger.isEnabled(nfd::LOG_INFO)) \ |
| 71 | std::cerr<<"INFO: "<<"["<<g_logger<<"] " << expression <<"\n" |
| 72 | |
| 73 | #define NFD_LOG_ERROR(expression)\ |
| 74 | if(g_logger.isEnabled(nfd::LOG_ERROR)) \ |
| 75 | std::cerr<<"ERROR: "<<"["<<g_logger<<"] " << expression <<"\n" |
| 76 | |
| 77 | #define NFD_LOG_FATAL(expression)\ |
| 78 | if(g_logger.isEnabled(nfd::LOG_FATAL)) \ |
| 79 | std::cerr<<"FATAL: "<<"["<<g_logger<<"] " << expression <<"\n" |
| 80 | |
| 81 | } //namespace nfd |
| 82 | |
| 83 | |
| 84 | #endif |