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> |
| 14 | #include <boost/filesystem.hpp> |
| 15 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 16 | |
| 17 | namespace ndn { |
| 18 | |
| 19 | class Logger |
| 20 | { |
| 21 | public: |
| 22 | Logger(const std::string& module) |
| 23 | : m_module(module) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | shutdownLogger() |
| 29 | { |
| 30 | if (m_logFile.is_open()) |
| 31 | { |
| 32 | log("Terminating Logging Operations" , true, true); |
| 33 | m_logFile.close(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | static std::string |
| 38 | getTimestamp() |
| 39 | { |
| 40 | boost::posix_time::ptime now; |
| 41 | now = boost::posix_time::second_clock::local_time(); |
| 42 | return to_simple_string(now); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | log(const std::string& logLine, bool printTime, bool printToConsole) |
| 47 | { |
| 48 | if( m_logLocation.length() > 0 ) |
| 49 | { |
| 50 | if (printTime) |
| 51 | m_logFile << getTimestamp() << " - "; |
| 52 | m_logFile << logLine << std::endl; |
| 53 | m_logFile.flush(); |
| 54 | if (printToConsole) |
| 55 | { |
| 56 | if (printTime) |
| 57 | std::cout << getTimestamp() << " - "; |
| 58 | std::cout << logLine << std::endl; |
| 59 | } |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | if (printTime) |
| 64 | std::cout << getTimestamp() << " - "; |
| 65 | std::cout << logLine << std::endl; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | initializeLog(const std::string& instanceId) |
| 71 | { |
| 72 | char* variableValue = std::getenv("NDN_TRAFFIC_LOGFOLDER"); |
| 73 | std::string logFilename; |
| 74 | m_logLocation = ""; |
| 75 | if (variableValue != NULL) |
| 76 | m_logLocation = variableValue; |
| 77 | if (boost::filesystem::exists(boost::filesystem::path(m_logLocation))) |
| 78 | { |
| 79 | if (boost::filesystem::is_directory(boost::filesystem::path(m_logLocation))) |
| 80 | { |
| 81 | logFilename = m_logLocation + "/" + m_module + "_" + instanceId + ".log"; |
| 82 | m_logFile.open(logFilename.c_str(), std::ofstream::out | std::ofstream::trunc); |
| 83 | if (m_logFile.is_open()) |
| 84 | std::cout << "Log File Initialized: " << logFilename << std::endl; |
| 85 | else |
| 86 | { |
| 87 | std::cout << "ERROR - Unable To Initialize A Log File At: " << m_logLocation << std::endl |
| 88 | << "Using Default Output For Logging." << std::endl; |
| 89 | m_logLocation = ""; |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Should Be A Folder." << std::endl |
| 95 | << "Using Default Output For Logging." << std::endl; |
| 96 | m_logLocation = ""; |
| 97 | } |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Not Set." << std::endl |
| 102 | << "Using Default Output For Logging." << std::endl; |
| 103 | m_logLocation = ""; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | std::string m_module; |
| 109 | std::string m_logLocation; |
| 110 | std::ofstream m_logFile; |
| 111 | }; |
| 112 | |
| 113 | } // namespace ndn |
| 114 | |
| 115 | #endif // NTG_LOGGER_HPP |