blob: 192f28fcec8544f6a44fc19f312e19fe88370715 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/**
* Copyright (C) 2014 University of Arizona.
*
* GNU 3.0 License, see the LICENSE file for more information
*
* Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
*/
#ifndef NTG_LOGGER_HPP
#define NTG_LOGGER_HPP
#include <string>
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
namespace ndn {
class Logger
{
public:
Logger(const std::string& module)
: m_module(module)
{
}
void
shutdownLogger()
{
if (m_logFile.is_open())
{
log("Terminating Logging Operations" , true, true);
m_logFile.close();
}
}
static std::string
getTimestamp()
{
boost::posix_time::ptime now;
now = boost::posix_time::second_clock::local_time();
return to_simple_string(now);
}
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)
{
char* variableValue = std::getenv("NDN_TRAFFIC_LOGFOLDER");
std::string logFilename;
m_logLocation = "";
if (variableValue != NULL)
m_logLocation = variableValue;
if (boost::filesystem::exists(boost::filesystem::path(m_logLocation)))
{
if (boost::filesystem::is_directory(boost::filesystem::path(m_logLocation)))
{
logFilename = m_logLocation + "/" + m_module + "_" + instanceId + ".log";
m_logFile.open(logFilename.c_str(), std::ofstream::out | std::ofstream::trunc);
if (m_logFile.is_open())
std::cout << "Log File Initialized: " << logFilename << 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 << "Environment Variable NDN_TRAFFIC_LOGFOLDER Should Be A Folder." << std::endl
<< "Using Default Output For Logging." << std::endl;
m_logLocation = "";
}
}
else
{
std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Not Set." << std::endl
<< "Using Default Output For Logging." << std::endl;
m_logLocation = "";
}
}
private:
std::string m_module;
std::string m_logLocation;
std::ofstream m_logFile;
};
} // namespace ndn
#endif // NTG_LOGGER_HPP