blob: eeae74804c6744daf24063f57ec1a94b763e0171 [file] [log] [blame]
Davide Pesavento35185332019-01-14 04:00:15 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoc2f25b72024-12-13 19:23:50 -05003 * Copyright (c) 2014-2024, 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>
Davide Pesaventoc2f25b72024-12-13 19:23:50 -050025#include <filesystem>
Alexander Afanasyev976c3972014-05-26 17:03:40 +030026#include <fstream>
Davide Pesaventod0b59982015-02-27 19:15:15 +010027#include <string>
Alexander Lanee9fe1872023-07-25 20:00:23 -040028#include <string_view>
Davide Pesaventod0b59982015-02-27 19:15:15 +010029
Alexander Lanee9fe1872023-07-25 20:00:23 -040030#include <boost/container/static_vector.hpp>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080031#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Lanee9fe1872023-07-25 20:00:23 -040032
33#include <ndn-cxx/util/time.hpp>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080034
Davide Pesaventoef064892022-04-05 02:26:03 -040035namespace ndntg {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080036
37class Logger
38{
39public:
Davide Pesavento35185332019-01-14 04:00:15 -050040 explicit
Alexander Lanee9fe1872023-07-25 20:00:23 -040041 Logger(std::string_view module)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080042 : m_module(module)
43 {
44 }
45
46 void
Alexander Lanee9fe1872023-07-25 20:00:23 -040047 log(std::string_view logLine, bool printTimestamp, bool printToConsole)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080048 {
Alexander Lanee9fe1872023-07-25 20:00:23 -040049 boost::container::static_vector<std::reference_wrapper<std::ostream>, 2> destinations;
50 if (!m_logLocation.empty()) {
51 destinations.emplace_back(m_logFile);
52 }
53 if (m_logLocation.empty() || printToConsole) {
54 destinations.emplace_back(std::cout);
55 }
56
57 if (printTimestamp) {
58 if (m_wantUnixTime) {
59 using namespace ndn::time;
60 auto now = std::to_string(toUnixTimestamp<microseconds>(system_clock::now()).count() / 1e6);
61 for (auto dest : destinations) {
62 dest.get() << '[' << now << "] ";
63 }
64 }
65 else {
66 auto now = boost::posix_time::microsec_clock::local_time();
67 for (auto dest : destinations) {
68 dest.get() << '[' << now << "] ";
69 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080070 }
Davide Pesavento35185332019-01-14 04:00:15 -050071 }
Alexander Lanee9fe1872023-07-25 20:00:23 -040072
73 for (auto dest : destinations) {
74 dest.get() << logLine << std::endl;
Davide Pesavento35185332019-01-14 04:00:15 -050075 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080076 }
77
Davide Pesavento35185332019-01-14 04:00:15 -050078 void
Alexander Lanee9fe1872023-07-25 20:00:23 -040079 initialize(const std::string& instanceId, const std::string& timestampFormat)
Davide Pesavento35185332019-01-14 04:00:15 -050080 {
Alexander Lanee9fe1872023-07-25 20:00:23 -040081 m_wantUnixTime = timestampFormat.empty();
82 if (!timestampFormat.empty()) {
83 std::cout.imbue(std::locale(std::cout.getloc(),
84 new boost::posix_time::time_facet(timestampFormat.data())));
85 }
Davide Pesavento35185332019-01-14 04:00:15 -050086
Alexander Lanee9fe1872023-07-25 20:00:23 -040087 m_logLocation = "";
88 if (const char* envVar = std::getenv("NDN_TRAFFIC_LOGFOLDER"); envVar != nullptr) {
89 m_logLocation = envVar;
90 }
Davide Pesavento35185332019-01-14 04:00:15 -050091 if (m_logLocation.empty()) {
92 std::cout << "Environment variable NDN_TRAFFIC_LOGFOLDER not set.\n"
93 << "Using default output for logging." << std::endl;
94 return;
95 }
96
Davide Pesaventoc2f25b72024-12-13 19:23:50 -050097 std::filesystem::path logdir(m_logLocation);
98 if (std::filesystem::exists(logdir)) {
99 if (std::filesystem::is_directory(logdir)) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400100 auto logfile = logdir / (m_module + '_' + instanceId + ".log");
101 m_logFile.open(logfile.string(), std::ofstream::out | std::ofstream::trunc);
Davide Pesavento35185332019-01-14 04:00:15 -0500102 if (m_logFile.is_open()) {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400103 if (!timestampFormat.empty()) {
104 m_logFile.imbue(std::locale(m_logFile.getloc(),
105 new boost::posix_time::time_facet(timestampFormat.data())));
106 }
Davide Pesaventoef064892022-04-05 02:26:03 -0400107 std::cout << "Log file initialized: " << logfile << std::endl;
Davide Pesavento35185332019-01-14 04:00:15 -0500108 }
109 else {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400110 std::cout << "ERROR: Unable to initialize a log file at: " << m_logLocation << "\n"
Davide Pesavento35185332019-01-14 04:00:15 -0500111 << "Using default output for logging." << std::endl;
112 m_logLocation = "";
113 }
114 }
115 else {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400116 std::cout << "NDN_TRAFFIC_LOGFOLDER is not a directory.\n"
Davide Pesavento35185332019-01-14 04:00:15 -0500117 << "Using default output for logging." << std::endl;
118 m_logLocation = "";
119 }
120 }
121 else {
122 std::cout << "NDN_TRAFFIC_LOGFOLDER does not exist.\n"
123 << "Using default output for logging." << std::endl;
124 m_logLocation = "";
125 }
126 }
127
128private:
Alexander Lanee9fe1872023-07-25 20:00:23 -0400129 const std::string m_module;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800130 std::string m_logLocation;
131 std::ofstream m_logFile;
Alexander Lanee9fe1872023-07-25 20:00:23 -0400132 bool m_wantUnixTime = true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800133};
134
Davide Pesaventoef064892022-04-05 02:26:03 -0400135} // namespace ndntg
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800136
Davide Pesaventoef064892022-04-05 02:26:03 -0400137#endif // NDNTG_LOGGER_HPP