blob: 706c6df9058fa2e1c73f6711be87df1d1b05103c [file] [log] [blame]
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2014 University of Arizona.
4 *
5 * GNU 3.0 License, see the LICENSE file for more information
6 *
7 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
8 */
9
10#ifndef NTG_LOGGER_HPP
11#define NTG_LOGGER_HPP
12
13#include <string>
14#include <boost/filesystem.hpp>
15#include <boost/date_time/posix_time/posix_time.hpp>
16
17namespace ndn {
18
19class Logger
20{
21public:
22 Logger(const std::string& module)
23 : m_module(module)
24 {
25 }
26
27 void
28 shutdownLogger()
29 {
30 if (m_logFile.is_open())
31 {
32 log("Terminating Logging Operations" , true, true);
33 m_logFile.close();
34 }
35 }
36
37 static std::string
38 getTimestamp()
39 {
40 boost::posix_time::ptime now;
41 now = boost::posix_time::second_clock::local_time();
42 return to_simple_string(now);
43 }
44
45 void
46 log(const std::string& logLine, bool printTime, bool printToConsole)
47 {
48 if( m_logLocation.length() > 0 )
49 {
50 if (printTime)
51 m_logFile << getTimestamp() << " - ";
52 m_logFile << logLine << std::endl;
53 m_logFile.flush();
54 if (printToConsole)
55 {
56 if (printTime)
57 std::cout << getTimestamp() << " - ";
58 std::cout << logLine << std::endl;
59 }
60 }
61 else
62 {
63 if (printTime)
64 std::cout << getTimestamp() << " - ";
65 std::cout << logLine << std::endl;
66 }
67 }
68
69 void
70 initializeLog(const std::string& instanceId)
71 {
72 char* variableValue = std::getenv("NDN_TRAFFIC_LOGFOLDER");
73 std::string logFilename;
74 m_logLocation = "";
75 if (variableValue != NULL)
76 m_logLocation = variableValue;
77 if (boost::filesystem::exists(boost::filesystem::path(m_logLocation)))
78 {
79 if (boost::filesystem::is_directory(boost::filesystem::path(m_logLocation)))
80 {
81 logFilename = m_logLocation + "/" + m_module + "_" + instanceId + ".log";
82 m_logFile.open(logFilename.c_str(), std::ofstream::out | std::ofstream::trunc);
83 if (m_logFile.is_open())
84 std::cout << "Log File Initialized: " << logFilename << std::endl;
85 else
86 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070087 std::cout << "ERROR - Unable To Initialize A Log File At: "
88 << m_logLocation << std::endl
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080089 << "Using Default Output For Logging." << std::endl;
90 m_logLocation = "";
91 }
92 }
93 else
94 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070095 std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Should Be A Folder."
96 << std::endl
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080097 << "Using Default Output For Logging." << std::endl;
98 m_logLocation = "";
99 }
100 }
101 else
102 {
103 std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Not Set." << std::endl
104 << "Using Default Output For Logging." << std::endl;
105 m_logLocation = "";
106 }
107 }
108
109private:
110 std::string m_module;
111 std::string m_logLocation;
112 std::ofstream m_logFile;
113};
114
115} // namespace ndn
116
117#endif // NTG_LOGGER_HPP