Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame^] | 3 | * Copyright (C) 2014-2015 University of Arizona. |
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 | |
| 21 | #ifndef NTG_LOGGER_HPP |
| 22 | #define NTG_LOGGER_HPP |
| 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 | |
| 31 | namespace ndn { |
| 32 | |
| 33 | class Logger |
| 34 | { |
| 35 | public: |
| 36 | Logger(const std::string& module) |
| 37 | : m_module(module) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | shutdownLogger() |
| 43 | { |
| 44 | if (m_logFile.is_open()) |
| 45 | { |
| 46 | log("Terminating Logging Operations" , true, true); |
| 47 | m_logFile.close(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static std::string |
| 52 | getTimestamp() |
| 53 | { |
| 54 | boost::posix_time::ptime now; |
| 55 | now = boost::posix_time::second_clock::local_time(); |
| 56 | return to_simple_string(now); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | log(const std::string& logLine, bool printTime, bool printToConsole) |
| 61 | { |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame^] | 62 | if (m_logLocation.length() > 0) |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 63 | { |
| 64 | if (printTime) |
| 65 | m_logFile << getTimestamp() << " - "; |
| 66 | m_logFile << logLine << std::endl; |
| 67 | m_logFile.flush(); |
| 68 | if (printToConsole) |
| 69 | { |
| 70 | if (printTime) |
| 71 | std::cout << getTimestamp() << " - "; |
| 72 | std::cout << logLine << std::endl; |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | if (printTime) |
| 78 | std::cout << getTimestamp() << " - "; |
| 79 | std::cout << logLine << std::endl; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | initializeLog(const std::string& instanceId) |
| 85 | { |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame^] | 86 | const char* envVar = std::getenv("NDN_TRAFFIC_LOGFOLDER"); |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 87 | m_logLocation = ""; |
Davide Pesavento | d0b5998 | 2015-02-27 19:15:15 +0100 | [diff] [blame^] | 88 | if (envVar != nullptr) |
| 89 | m_logLocation = envVar; |
| 90 | |
| 91 | std::string logFilename; |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 92 | if (boost::filesystem::exists(boost::filesystem::path(m_logLocation))) |
| 93 | { |
| 94 | if (boost::filesystem::is_directory(boost::filesystem::path(m_logLocation))) |
| 95 | { |
| 96 | logFilename = m_logLocation + "/" + m_module + "_" + instanceId + ".log"; |
| 97 | m_logFile.open(logFilename.c_str(), std::ofstream::out | std::ofstream::trunc); |
| 98 | if (m_logFile.is_open()) |
| 99 | std::cout << "Log File Initialized: " << logFilename << std::endl; |
| 100 | else |
| 101 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 102 | std::cout << "ERROR - Unable To Initialize A Log File At: " |
| 103 | << m_logLocation << std::endl |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 104 | << "Using Default Output For Logging." << std::endl; |
| 105 | m_logLocation = ""; |
| 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 110 | std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Should Be A Folder." |
| 111 | << std::endl |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 112 | << "Using Default Output For Logging." << std::endl; |
| 113 | m_logLocation = ""; |
| 114 | } |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Not Set." << std::endl |
| 119 | << "Using Default Output For Logging." << std::endl; |
| 120 | m_logLocation = ""; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | std::string m_module; |
| 126 | std::string m_logLocation; |
| 127 | std::ofstream m_logFile; |
| 128 | }; |
| 129 | |
| 130 | } // namespace ndn |
| 131 | |
| 132 | #endif // NTG_LOGGER_HPP |