blob: 7952b1c9b960d40db5d548e5dd8cf57132083c7d [file] [log] [blame]
Davide Pesavento35185332019-01-14 04:00:15 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoef064892022-04-05 02:26:03 -04003 * Copyright (c) 2014-2022, Arizona Board of Regents.
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
Davide Pesaventoef064892022-04-05 02:26:03 -040021#ifndef NDNTG_LOGGER_HPP
22#define NDNTG_LOGGER_HPP
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080023
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
Davide Pesaventoef064892022-04-05 02:26:03 -040031namespace ndntg {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080032
33class Logger
34{
35public:
Davide Pesavento35185332019-01-14 04:00:15 -050036 explicit
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080037 Logger(const std::string& module)
38 : m_module(module)
39 {
40 }
41
42 void
Davide Pesavento35185332019-01-14 04:00:15 -050043 log(const std::string& logLine, bool printTime, bool printToConsole)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080044 {
Davide Pesavento35185332019-01-14 04:00:15 -050045 if (m_logLocation.length() > 0) {
46 if (printTime)
47 m_logFile << getTimestamp() << " - ";
48 m_logFile << logLine << std::endl;
49 m_logFile.flush();
50 if (printToConsole) {
51 if (printTime)
52 std::cout << getTimestamp() << " - ";
53 std::cout << logLine << std::endl;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080054 }
Davide Pesavento35185332019-01-14 04:00:15 -050055 }
56 else {
57 if (printTime)
58 std::cout << getTimestamp() << " - ";
59 std::cout << logLine << std::endl;
60 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080061 }
62
Davide Pesavento35185332019-01-14 04:00:15 -050063 void
64 initializeLog(const std::string& instanceId)
65 {
66 m_logLocation = "";
67 const char* envVar = std::getenv("NDN_TRAFFIC_LOGFOLDER");
68 if (envVar != nullptr)
69 m_logLocation = envVar;
70
71 if (m_logLocation.empty()) {
72 std::cout << "Environment variable NDN_TRAFFIC_LOGFOLDER not set.\n"
73 << "Using default output for logging." << std::endl;
74 return;
75 }
76
Davide Pesaventoef064892022-04-05 02:26:03 -040077 boost::filesystem::path logdir(m_logLocation);
78 if (boost::filesystem::exists(logdir)) {
79 if (boost::filesystem::is_directory(logdir)) {
80 auto logfile = logdir / (m_module + '_' + instanceId + ".log");
81 m_logFile.open(logfile.string(), std::ofstream::out | std::ofstream::trunc);
Davide Pesavento35185332019-01-14 04:00:15 -050082 if (m_logFile.is_open()) {
Davide Pesaventoef064892022-04-05 02:26:03 -040083 std::cout << "Log file initialized: " << logfile << std::endl;
Davide Pesavento35185332019-01-14 04:00:15 -050084 }
85 else {
86 std::cout << "ERROR: Unable to initialize a log file at: " << m_logLocation << std::endl
87 << "Using default output for logging." << std::endl;
88 m_logLocation = "";
89 }
90 }
91 else {
92 std::cout << "NDN_TRAFFIC_LOGFOLDER should be a directory.\n"
93 << "Using default output for logging." << std::endl;
94 m_logLocation = "";
95 }
96 }
97 else {
98 std::cout << "NDN_TRAFFIC_LOGFOLDER does not exist.\n"
99 << "Using default output for logging." << std::endl;
100 m_logLocation = "";
101 }
102 }
103
104private:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800105 static std::string
106 getTimestamp()
107 {
Davide Pesaventoef064892022-04-05 02:26:03 -0400108 auto now = boost::posix_time::second_clock::local_time();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800109 return to_simple_string(now);
110 }
111
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800112private:
113 std::string m_module;
114 std::string m_logLocation;
115 std::ofstream m_logFile;
116};
117
Davide Pesaventoef064892022-04-05 02:26:03 -0400118} // namespace ndntg
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800119
Davide Pesaventoef064892022-04-05 02:26:03 -0400120#endif // NDNTG_LOGGER_HPP