Project

General

Profile

« Previous | Next » 

Revision eee13fa3

Added by david.sorber over 2 years ago

First mostly functional version; let's call it v0.1.0.

View differences:

src/Logger.cc
* of the United States. *
********************************************************************/
#include <cstdio>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <stdexcept>
#include "Logger.h"
......
: r_options(*configObj)
{
m_outputStream.str("");
m_logFile.open("loggy"/*r_config.getLogPath()*/, std::ios::out | std::ios::app);
if (! m_logFile)
if (r_options.getLogToStderr() == false)
{
throw std::runtime_error("Unable to open log file: " /*+ r_config.getLogPath()*/);
// If configured to log to file attempt to open the specified log file
m_logFile.open(r_options.getLogFilePath(), std::ios::out | std::ios::app);
if (!m_logFile)
{
throw std::runtime_error("Unable to open log file: " +
r_options.getLogFilePath());
}
}
}
DBS::Logger::~Logger()
{
m_logFile.close();
// Close log file if it was opened
if (m_logFile.is_open())
{
m_logFile.close();
}
}
void DBS::Logger::log_msg(
enum LOG_LEVEL level,
const std::string& msg)
{
if (level > 3)//r_config.getLogLevel())
// Filter log messages as needed
if (r_options.getDisableLog() || level > r_options.getLogLevel())
{
return;
}
struct timeval tv;
struct tm *tm;
gettimeofday(&tv, NULL);
tm = localtime(&tv.tv_sec);
// Grab current time
struct timeval tv{};
gettimeofday(&tv, nullptr);
struct tm* tm = std::localtime(&tv.tv_sec);
char time_format[64], time_str[64];
char time_format[32], time_str[32];
const bool LOG_WITH_MILLISECONDS = true;
if (LOG_WITH_MILLISECONDS == true)
{
// Set format to include milliseconds
strftime(time_format, sizeof(time_format), "%a %b %d %H:%M:%S.%%03u %Y",
tm);
std::snprintf(time_str, sizeof(time_str), time_format, (tv.tv_usec / 1000));
std::strftime(time_format,
sizeof(time_format),
"%a %b %d %H:%M:%S.%%03u %Y",
tm);
std::snprintf(time_str,
sizeof(time_str),
time_format,
(tv.tv_usec / 1000));
}
else
{
// Set format to not include milliseconds
strftime(time_str, sizeof(time_str), "%a %b %d %H:%M:%S %Y", tm);
std::strftime(time_str,
sizeof(time_str),
"%a %b %d %H:%M:%S %Y",
tm);
}
// NOTE: "COMPONENT_ID" must be defined somewhere, ideally at compile time
switch (level)
{
case error:
m_logFile << time_str << "|ERROR|copytool|" << msg << std::endl;
getStream() << time_str << "|ERROR|copytool|" << msg << std::endl;
break;
case warn:
m_logFile << time_str << "|WARNING|copytool|" << msg << std::endl;
getStream() << time_str << "|WARNING|copytool|" << msg << std::endl;
break;
case info:
m_logFile << time_str << "|INFO|copytool|" << msg << std::endl;
getStream() << time_str << "|INFO|copytool|" << msg << std::endl;
break;
case debug:
m_logFile << time_str << "|DEBUG|copytool|" << msg << std::endl;
getStream() << time_str << "|DEBUG|copytool|" << msg << std::endl;
break;
case trace:
m_logFile << time_str << "|TRACE|copytool|" << msg << std::endl;
getStream() << time_str << "|TRACE|copytool|" << msg << std::endl;
break;
default:

Also available in: Unified diff