blob: 0ed35b3c09697757ebdd3c70ca4ff470f23d7c2d [file] [log] [blame]
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Davide Pesaventod0b59982015-02-27 19:15:15 +01003 * Copyright (C) 2014-2015 University of Arizona.
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08004 *
Davide Pesaventod0b59982015-02-27 19:15:15 +01005 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080017 *
18 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
19 */
20
21#ifndef NTG_LOGGER_HPP
22#define NTG_LOGGER_HPP
23
Davide Pesaventod0b59982015-02-27 19:15:15 +010024#include <cstdlib>
Alexander Afanasyev976c3972014-05-26 17:03:40 +030025#include <fstream>
Davide Pesaventod0b59982015-02-27 19:15:15 +010026#include <string>
27
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080028#include <boost/filesystem.hpp>
29#include <boost/date_time/posix_time/posix_time.hpp>
30
31namespace ndn {
32
33class Logger
34{
35public:
36 Logger(const std::string& module)
37 : m_module(module)
38 {
39 }
40
41 void
42 shutdownLogger()
43 {
44 if (m_logFile.is_open())
45 {
46 log("Terminating Logging Operations" , true, true);
47 m_logFile.close();
48 }
49 }
50
51 static std::string
52 getTimestamp()
53 {
54 boost::posix_time::ptime now;
55 now = boost::posix_time::second_clock::local_time();
56 return to_simple_string(now);
57 }
58
59 void
60 log(const std::string& logLine, bool printTime, bool printToConsole)
61 {
Davide Pesaventod0b59982015-02-27 19:15:15 +010062 if (m_logLocation.length() > 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080063 {
64 if (printTime)
65 m_logFile << getTimestamp() << " - ";
66 m_logFile << logLine << std::endl;
67 m_logFile.flush();
68 if (printToConsole)
69 {
70 if (printTime)
71 std::cout << getTimestamp() << " - ";
72 std::cout << logLine << std::endl;
73 }
74 }
75 else
76 {
77 if (printTime)
78 std::cout << getTimestamp() << " - ";
79 std::cout << logLine << std::endl;
80 }
81 }
82
83 void
84 initializeLog(const std::string& instanceId)
85 {
Davide Pesaventod0b59982015-02-27 19:15:15 +010086 const char* envVar = std::getenv("NDN_TRAFFIC_LOGFOLDER");
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080087 m_logLocation = "";
Davide Pesaventod0b59982015-02-27 19:15:15 +010088 if (envVar != nullptr)
89 m_logLocation = envVar;
90
91 std::string logFilename;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080092 if (boost::filesystem::exists(boost::filesystem::path(m_logLocation)))
93 {
94 if (boost::filesystem::is_directory(boost::filesystem::path(m_logLocation)))
95 {
96 logFilename = m_logLocation + "/" + m_module + "_" + instanceId + ".log";
97 m_logFile.open(logFilename.c_str(), std::ofstream::out | std::ofstream::trunc);
98 if (m_logFile.is_open())
99 std::cout << "Log File Initialized: " << logFilename << std::endl;
100 else
101 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700102 std::cout << "ERROR - Unable To Initialize A Log File At: "
103 << m_logLocation << std::endl
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800104 << "Using Default Output For Logging." << std::endl;
105 m_logLocation = "";
106 }
107 }
108 else
109 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700110 std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Should Be A Folder."
111 << std::endl
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800112 << "Using Default Output For Logging." << std::endl;
113 m_logLocation = "";
114 }
115 }
116 else
117 {
118 std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Not Set." << std::endl
119 << "Using Default Output For Logging." << std::endl;
120 m_logLocation = "";
121 }
122 }
123
124private:
125 std::string m_module;
126 std::string m_logLocation;
127 std::ofstream m_logFile;
128};
129
130} // namespace ndn
131
132#endif // NTG_LOGGER_HPP