blob: d9be745c66ad5bf1342ea7e4bd3edb1e47a6e896 [file] [log] [blame]
Davide Pesavento35185332019-01-14 04:00:15 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Alexander Lanee9fe1872023-07-25 20:00:23 -04003 * Copyright (c) 2014-2023, 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>
Alexander Lanee9fe1872023-07-25 20:00:23 -040027#include <string_view>
Davide Pesaventod0b59982015-02-27 19:15:15 +010028
Alexander Lanee9fe1872023-07-25 20:00:23 -040029#include <boost/container/static_vector.hpp>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080030#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Lanee9fe1872023-07-25 20:00:23 -040031#include <boost/filesystem/operations.hpp>
32#include <boost/filesystem/path.hpp>
33
34#include <ndn-cxx/util/time.hpp>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080035
Davide Pesaventoef064892022-04-05 02:26:03 -040036namespace ndntg {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080037
38class Logger
39{
40public:
Davide Pesavento35185332019-01-14 04:00:15 -050041 explicit
Alexander Lanee9fe1872023-07-25 20:00:23 -040042 Logger(std::string_view module)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080043 : m_module(module)
44 {
45 }
46
47 void
Alexander Lanee9fe1872023-07-25 20:00:23 -040048 log(std::string_view logLine, bool printTimestamp, bool printToConsole)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080049 {
Alexander Lanee9fe1872023-07-25 20:00:23 -040050 boost::container::static_vector<std::reference_wrapper<std::ostream>, 2> destinations;
51 if (!m_logLocation.empty()) {
52 destinations.emplace_back(m_logFile);
53 }
54 if (m_logLocation.empty() || printToConsole) {
55 destinations.emplace_back(std::cout);
56 }
57
58 if (printTimestamp) {
59 if (m_wantUnixTime) {
60 using namespace ndn::time;
61 auto now = std::to_string(toUnixTimestamp<microseconds>(system_clock::now()).count() / 1e6);
62 for (auto dest : destinations) {
63 dest.get() << '[' << now << "] ";
64 }
65 }
66 else {
67 auto now = boost::posix_time::microsec_clock::local_time();
68 for (auto dest : destinations) {
69 dest.get() << '[' << now << "] ";
70 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080071 }
Davide Pesavento35185332019-01-14 04:00:15 -050072 }
Alexander Lanee9fe1872023-07-25 20:00:23 -040073
74 for (auto dest : destinations) {
75 dest.get() << logLine << std::endl;
Davide Pesavento35185332019-01-14 04:00:15 -050076 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080077 }
78
Davide Pesavento35185332019-01-14 04:00:15 -050079 void
Alexander Lanee9fe1872023-07-25 20:00:23 -040080 initialize(const std::string& instanceId, const std::string& timestampFormat)
Davide Pesavento35185332019-01-14 04:00:15 -050081 {
Alexander Lanee9fe1872023-07-25 20:00:23 -040082 m_wantUnixTime = timestampFormat.empty();
83 if (!timestampFormat.empty()) {
84 std::cout.imbue(std::locale(std::cout.getloc(),
85 new boost::posix_time::time_facet(timestampFormat.data())));
86 }
Davide Pesavento35185332019-01-14 04:00:15 -050087
Alexander Lanee9fe1872023-07-25 20:00:23 -040088 m_logLocation = "";
89 if (const char* envVar = std::getenv("NDN_TRAFFIC_LOGFOLDER"); envVar != nullptr) {
90 m_logLocation = envVar;
91 }
Davide Pesavento35185332019-01-14 04:00:15 -050092 if (m_logLocation.empty()) {
93 std::cout << "Environment variable NDN_TRAFFIC_LOGFOLDER not set.\n"
94 << "Using default output for logging." << std::endl;
95 return;
96 }
97
Davide Pesaventoef064892022-04-05 02:26:03 -040098 boost::filesystem::path logdir(m_logLocation);
99 if (boost::filesystem::exists(logdir)) {
100 if (boost::filesystem::is_directory(logdir)) {
101 auto logfile = logdir / (m_module + '_' + instanceId + ".log");
102 m_logFile.open(logfile.string(), std::ofstream::out | std::ofstream::trunc);
Davide Pesavento35185332019-01-14 04:00:15 -0500103 if (m_logFile.is_open()) {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400104 if (!timestampFormat.empty()) {
105 m_logFile.imbue(std::locale(m_logFile.getloc(),
106 new boost::posix_time::time_facet(timestampFormat.data())));
107 }
Davide Pesaventoef064892022-04-05 02:26:03 -0400108 std::cout << "Log file initialized: " << logfile << std::endl;
Davide Pesavento35185332019-01-14 04:00:15 -0500109 }
110 else {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400111 std::cout << "ERROR: Unable to initialize a log file at: " << m_logLocation << "\n"
Davide Pesavento35185332019-01-14 04:00:15 -0500112 << "Using default output for logging." << std::endl;
113 m_logLocation = "";
114 }
115 }
116 else {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400117 std::cout << "NDN_TRAFFIC_LOGFOLDER is not a directory.\n"
Davide Pesavento35185332019-01-14 04:00:15 -0500118 << "Using default output for logging." << std::endl;
119 m_logLocation = "";
120 }
121 }
122 else {
123 std::cout << "NDN_TRAFFIC_LOGFOLDER does not exist.\n"
124 << "Using default output for logging." << std::endl;
125 m_logLocation = "";
126 }
127 }
128
129private:
Alexander Lanee9fe1872023-07-25 20:00:23 -0400130 const std::string m_module;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800131 std::string m_logLocation;
132 std::ofstream m_logFile;
Alexander Lanee9fe1872023-07-25 20:00:23 -0400133 bool m_wantUnixTime = true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800134};
135
Davide Pesaventoef064892022-04-05 02:26:03 -0400136} // namespace ndntg
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800137
Davide Pesaventoef064892022-04-05 02:26:03 -0400138#endif // NDNTG_LOGGER_HPP