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