Code modernization
Change-Id: Ia15bb9e988b08bc81c0709d95d7d17e6078a5385
diff --git a/src/logger.hpp b/src/logger.hpp
index 0ed35b3..71cfbc2 100644
--- a/src/logger.hpp
+++ b/src/logger.hpp
@@ -1,6 +1,6 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2014-2015 University of Arizona.
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019, 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
@@ -33,21 +33,74 @@
class Logger
{
public:
+ explicit
Logger(const std::string& module)
: m_module(module)
{
}
void
- shutdownLogger()
+ log(const std::string& logLine, bool printTime, bool printToConsole)
{
- if (m_logFile.is_open())
- {
- log("Terminating Logging Operations" , true, true);
- m_logFile.close();
+ 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;
+ }
+
+ if (boost::filesystem::exists(boost::filesystem::path(m_logLocation))) {
+ if (boost::filesystem::is_directory(boost::filesystem::path(m_logLocation))) {
+ std::string logFilename = m_logLocation + "/" + m_module + "_" + instanceId + ".log";
+ m_logFile.open(logFilename.data(), 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 << "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()
{
@@ -56,71 +109,6 @@
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)
- {
- const char* envVar = std::getenv("NDN_TRAFFIC_LOGFOLDER");
- m_logLocation = "";
- if (envVar != nullptr)
- m_logLocation = envVar;
-
- std::string logFilename;
- 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;