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 | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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> |
| 27 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 28 | #include <boost/filesystem.hpp> |
| 29 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 30 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 31 | namespace ndntg { |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 32 | |
| 33 | class Logger |
| 34 | { |
| 35 | public: |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 36 | explicit |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 37 | Logger(const std::string& module) |
| 38 | : m_module(module) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 43 | log(const std::string& logLine, bool printTime, bool printToConsole) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 44 | { |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 45 | 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 Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 54 | } |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 55 | } |
| 56 | else { |
| 57 | if (printTime) |
| 58 | std::cout << getTimestamp() << " - "; |
| 59 | std::cout << logLine << std::endl; |
| 60 | } |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 63 | 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 Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 77 | 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 Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 82 | if (m_logFile.is_open()) { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 83 | std::cout << "Log file initialized: " << logfile << std::endl; |
Davide Pesavento | 3518533 | 2019-01-14 04:00:15 -0500 | [diff] [blame] | 84 | } |
| 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 | |
| 104 | private: |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 105 | static std::string |
| 106 | getTimestamp() |
| 107 | { |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 108 | auto now = boost::posix_time::second_clock::local_time(); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 109 | return to_simple_string(now); |
| 110 | } |
| 111 | |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 112 | private: |
| 113 | std::string m_module; |
| 114 | std::string m_logLocation; |
| 115 | std::ofstream m_logFile; |
| 116 | }; |
| 117 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 118 | } // namespace ndntg |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 119 | |
Davide Pesavento | ef06489 | 2022-04-05 02:26:03 -0400 | [diff] [blame] | 120 | #endif // NDNTG_LOGGER_HPP |