Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, Arizona Board of Regents. |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 4 | * |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 5 | * 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 Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 17 | * |
| 18 | * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 19 | */ |
| 20 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 21 | #ifndef NDNTG_LOGGER_HPP |
| 22 | #define NDNTG_LOGGER_HPP |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 24 | #include <cstdlib> |
Alexander Afanasyev | 976c397 | 2014-05-26 17:03:40 +0300 | [diff] [blame] | 25 | #include <fstream> |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 26 | #include <string> |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 27 | #include <string_view> |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame] | 28 | |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 29 | #include <boost/container/static_vector.hpp> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 30 | #include <boost/date_time/posix_time/posix_time.hpp> |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 31 | #include <boost/filesystem/operations.hpp> |
| 32 | #include <boost/filesystem/path.hpp> |
| 33 | |
| 34 | #include <ndn-cxx/util/time.hpp> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 36 | namespace ndntg { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 37 | |
| 38 | class Logger |
| 39 | { |
| 40 | public: |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 41 | explicit |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 42 | Logger(std::string_view module) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 43 | : m_module(module) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 48 | log(std::string_view logLine, bool printTimestamp, bool printToConsole) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 49 | { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 50 | 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 Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 71 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 72 | } |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 73 | |
| 74 | for (auto dest : destinations) { |
| 75 | dest.get() << logLine << std::endl; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 76 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 79 | void |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 80 | initialize(const std::string& instanceId, const std::string& timestampFormat) |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 81 | { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 82 | 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 Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 87 | |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 88 | m_logLocation = ""; |
| 89 | if (const char* envVar = std::getenv("NDN_TRAFFIC_LOGFOLDER"); envVar != nullptr) { |
| 90 | m_logLocation = envVar; |
| 91 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 92 | 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 Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 98 | 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 Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 103 | if (m_logFile.is_open()) { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 104 | if (!timestampFormat.empty()) { |
| 105 | m_logFile.imbue(std::locale(m_logFile.getloc(), |
| 106 | new boost::posix_time::time_facet(timestampFormat.data()))); |
| 107 | } |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 108 | std::cout << "Log file initialized: " << logfile << std::endl; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 109 | } |
| 110 | else { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 111 | std::cout << "ERROR: Unable to initialize a log file at: " << m_logLocation << "\n" |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 112 | << "Using default output for logging." << std::endl; |
| 113 | m_logLocation = ""; |
| 114 | } |
| 115 | } |
| 116 | else { |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 117 | std::cout << "NDN_TRAFFIC_LOGFOLDER is not a directory.\n" |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 118 | << "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 | |
| 129 | private: |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 130 | const std::string m_module; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 131 | std::string m_logLocation; |
| 132 | std::ofstream m_logFile; |
Alexander Lane | e9fe187 | 2023-07-25 20:00:23 -0400 | [diff] [blame^] | 133 | bool m_wantUnixTime = true; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 136 | } // namespace ndntg |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 137 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 138 | #endif // NDNTG_LOGGER_HPP |