Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 University of Arizona. |
| 4 | * |
| 5 | * GNU 3.0 License, see the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 8 | */ |
| 9 | |
| 10 | #ifndef NTG_LOGGER_HPP |
| 11 | #define NTG_LOGGER_HPP |
| 12 | |
| 13 | #include <string> |
Alexander Afanasyev | 976c397 | 2014-05-26 17:03:40 +0300 | [diff] [blame] | 14 | #include <fstream> |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 15 | #include <boost/filesystem.hpp> |
| 16 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 17 | |
| 18 | namespace ndn { |
| 19 | |
| 20 | class Logger |
| 21 | { |
| 22 | public: |
| 23 | Logger(const std::string& module) |
| 24 | : m_module(module) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | void |
| 29 | shutdownLogger() |
| 30 | { |
| 31 | if (m_logFile.is_open()) |
| 32 | { |
| 33 | log("Terminating Logging Operations" , true, true); |
| 34 | m_logFile.close(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static std::string |
| 39 | getTimestamp() |
| 40 | { |
| 41 | boost::posix_time::ptime now; |
| 42 | now = boost::posix_time::second_clock::local_time(); |
| 43 | return to_simple_string(now); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | log(const std::string& logLine, bool printTime, bool printToConsole) |
| 48 | { |
| 49 | if( m_logLocation.length() > 0 ) |
| 50 | { |
| 51 | if (printTime) |
| 52 | m_logFile << getTimestamp() << " - "; |
| 53 | m_logFile << logLine << std::endl; |
| 54 | m_logFile.flush(); |
| 55 | if (printToConsole) |
| 56 | { |
| 57 | if (printTime) |
| 58 | std::cout << getTimestamp() << " - "; |
| 59 | std::cout << logLine << std::endl; |
| 60 | } |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | if (printTime) |
| 65 | std::cout << getTimestamp() << " - "; |
| 66 | std::cout << logLine << std::endl; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | initializeLog(const std::string& instanceId) |
| 72 | { |
| 73 | char* variableValue = std::getenv("NDN_TRAFFIC_LOGFOLDER"); |
| 74 | std::string logFilename; |
| 75 | m_logLocation = ""; |
| 76 | if (variableValue != NULL) |
| 77 | m_logLocation = variableValue; |
| 78 | if (boost::filesystem::exists(boost::filesystem::path(m_logLocation))) |
| 79 | { |
| 80 | if (boost::filesystem::is_directory(boost::filesystem::path(m_logLocation))) |
| 81 | { |
| 82 | logFilename = m_logLocation + "/" + m_module + "_" + instanceId + ".log"; |
| 83 | m_logFile.open(logFilename.c_str(), std::ofstream::out | std::ofstream::trunc); |
| 84 | if (m_logFile.is_open()) |
| 85 | std::cout << "Log File Initialized: " << logFilename << std::endl; |
| 86 | else |
| 87 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 88 | std::cout << "ERROR - Unable To Initialize A Log File At: " |
| 89 | << m_logLocation << std::endl |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 90 | << "Using Default Output For Logging." << std::endl; |
| 91 | m_logLocation = ""; |
| 92 | } |
| 93 | } |
| 94 | else |
| 95 | { |
Alexander Afanasyev | fda32a3 | 2014-03-20 10:50:00 -0700 | [diff] [blame] | 96 | std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Should Be A Folder." |
| 97 | << std::endl |
Alexander Afanasyev | a8f2a92 | 2014-02-26 14:21:56 -0800 | [diff] [blame] | 98 | << "Using Default Output For Logging." << std::endl; |
| 99 | m_logLocation = ""; |
| 100 | } |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Not Set." << std::endl |
| 105 | << "Using Default Output For Logging." << std::endl; |
| 106 | m_logLocation = ""; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | std::string m_module; |
| 112 | std::string m_logLocation; |
| 113 | std::ofstream m_logFile; |
| 114 | }; |
| 115 | |
| 116 | } // namespace ndn |
| 117 | |
| 118 | #endif // NTG_LOGGER_HPP |