| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2014-2022, Arizona Board of Regents. |
| * |
| * This program is free software: you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License as published by |
| * the Free Software Foundation, either version 3 of the License, or |
| * (at your option) any later version. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| * |
| * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| */ |
| |
| #ifndef NDNTG_LOGGER_HPP |
| #define NDNTG_LOGGER_HPP |
| |
| #include <cstdlib> |
| #include <fstream> |
| #include <string> |
| |
| #include <boost/filesystem.hpp> |
| #include <boost/date_time/posix_time/posix_time.hpp> |
| |
| namespace ndntg { |
| |
| class Logger |
| { |
| public: |
| explicit |
| Logger(const std::string& module) |
| : m_module(module) |
| { |
| } |
| |
| void |
| log(const std::string& logLine, bool printTime, bool printToConsole) |
| { |
| if (m_logLocation.length() > 0) { |
| if (printTime) |
| m_logFile << getTimestamp() << " - "; |
| m_logFile << logLine << std::endl; |
| m_logFile.flush(); |
| if (printToConsole) { |
| if (printTime) |
| std::cout << getTimestamp() << " - "; |
| std::cout << logLine << std::endl; |
| } |
| } |
| else { |
| if (printTime) |
| std::cout << getTimestamp() << " - "; |
| std::cout << logLine << std::endl; |
| } |
| } |
| |
| void |
| initializeLog(const std::string& instanceId) |
| { |
| m_logLocation = ""; |
| const char* envVar = std::getenv("NDN_TRAFFIC_LOGFOLDER"); |
| if (envVar != nullptr) |
| m_logLocation = envVar; |
| |
| if (m_logLocation.empty()) { |
| std::cout << "Environment variable NDN_TRAFFIC_LOGFOLDER not set.\n" |
| << "Using default output for logging." << std::endl; |
| return; |
| } |
| |
| boost::filesystem::path logdir(m_logLocation); |
| if (boost::filesystem::exists(logdir)) { |
| if (boost::filesystem::is_directory(logdir)) { |
| auto logfile = logdir / (m_module + '_' + instanceId + ".log"); |
| m_logFile.open(logfile.string(), std::ofstream::out | std::ofstream::trunc); |
| if (m_logFile.is_open()) { |
| std::cout << "Log file initialized: " << logfile << std::endl; |
| } |
| else { |
| std::cout << "ERROR: Unable to initialize a log file at: " << m_logLocation << std::endl |
| << "Using default output for logging." << std::endl; |
| m_logLocation = ""; |
| } |
| } |
| else { |
| std::cout << "NDN_TRAFFIC_LOGFOLDER should be a directory.\n" |
| << "Using default output for logging." << std::endl; |
| m_logLocation = ""; |
| } |
| } |
| else { |
| std::cout << "NDN_TRAFFIC_LOGFOLDER does not exist.\n" |
| << "Using default output for logging." << std::endl; |
| m_logLocation = ""; |
| } |
| } |
| |
| private: |
| static std::string |
| getTimestamp() |
| { |
| auto now = boost::posix_time::second_clock::local_time(); |
| return to_simple_string(now); |
| } |
| |
| private: |
| std::string m_module; |
| std::string m_logLocation; |
| std::ofstream m_logFile; |
| }; |
| |
| } // namespace ndntg |
| |
| #endif // NDNTG_LOGGER_HPP |