blob: 928b1124d1b216d639fc23db9e80ffc756c9db3f [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#include "logger.hpp"
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080010#include <boost/algorithm/string.hpp>
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080011
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080012namespace nfd {
13
14inline static bool
15isNumber(const std::string& s)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080016{
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080017 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 Moiseenkoa807e652014-01-28 11:51:01 -080028
29Logger::Logger(const std::string& name)
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080030 : m_moduleName(name)
Ilya Moiseenkoa807e652014-01-28 11:51:01 -080031{
Alexander Afanasyevb84cc922014-02-24 17:52:58 -080032 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 Moiseenkoa807e652014-01-28 11:51:01 -080073}
74
75std::ostream&
76operator<<(std::ostream& output, const Logger& obj)
77{
78 output << obj.getName();
79 return output;
80}
81
82} // namespace nfd