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 | #include "logger.hpp" |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame^] | 10 | #include <boost/algorithm/string.hpp> |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 11 | |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame^] | 12 | namespace nfd { |
| 13 | |
| 14 | inline static bool |
| 15 | isNumber(const std::string& s) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 16 | { |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame^] | 17 | if (s.empty()) |
| 18 | return false; |
| 19 | |
| 20 | for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) |
| 21 | { |
| 22 | if (!std::isdigit(*i)) |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | return true; |
| 27 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 28 | |
| 29 | Logger::Logger(const std::string& name) |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame^] | 30 | : m_moduleName(name) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 31 | { |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame^] | 32 | char* nfdLog = getenv("NFD_LOG"); |
| 33 | if (nfdLog) |
| 34 | { |
| 35 | std::string level = nfdLog; |
| 36 | if (isNumber(nfdLog)) |
| 37 | { |
| 38 | setLogLevel(boost::lexical_cast<uint32_t>(nfdLog)); |
| 39 | } |
| 40 | else if (boost::iequals(level, "none")) |
| 41 | { |
| 42 | m_enabledLogLevel = LOG_NONE; |
| 43 | } |
| 44 | else if (boost::iequals(level, "error")) |
| 45 | { |
| 46 | m_enabledLogLevel = LOG_ERROR; |
| 47 | } |
| 48 | else if (boost::iequals(level, "warn")) |
| 49 | { |
| 50 | m_enabledLogLevel = LOG_WARN; |
| 51 | } |
| 52 | else if (boost::iequals(level, "info")) |
| 53 | { |
| 54 | m_enabledLogLevel = LOG_INFO; |
| 55 | } |
| 56 | else if (boost::iequals(level, "debug")) |
| 57 | { |
| 58 | m_enabledLogLevel = LOG_DEBUG; |
| 59 | } |
| 60 | else if (boost::iequals(level, "trace")) |
| 61 | { |
| 62 | m_enabledLogLevel = LOG_TRACE; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | m_enabledLogLevel = LOG_ALL; |
| 67 | } |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | m_enabledLogLevel = LOG_WARN; |
| 72 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | std::ostream& |
| 76 | operator<<(std::ostream& output, const Logger& obj) |
| 77 | { |
| 78 | output << obj.getName(); |
| 79 | return output; |
| 80 | } |
| 81 | |
| 82 | } // namespace nfd |