logging reflow
This commit is contained in:
51
src/Logger/Logger.cpp
Normal file
51
src/Logger/Logger.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by bagira on 6/13/20.
|
||||
//
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
Logger::Logger( int LOG_LEVEL, std::string mask )
|
||||
{
|
||||
this->LOG_LEVEL = LOG_LEVEL;
|
||||
this->mask = mask.c_str();
|
||||
|
||||
setlogmask( LOG_UPTO( this->LOG_LEVEL ) );
|
||||
openlog( this->mask, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_PERROR | LOG_LOCAL1 );
|
||||
|
||||
}
|
||||
|
||||
void Logger::log( int LOG_LEVEL, std::string msg )
|
||||
{
|
||||
std::string ERR = "XXXX";
|
||||
|
||||
if ( LOG_LEVEL <= this->LOG_LEVEL )
|
||||
{
|
||||
switch ( LOG_LEVEL )
|
||||
{
|
||||
case E_DEBUG: ERR = "DBUG"; break;
|
||||
case E_FATAL: ERR = "FATL"; break;
|
||||
case E_INFO: ERR = "INFO"; break;
|
||||
case E_WARN: ERR = "WARN"; break;
|
||||
}
|
||||
|
||||
std::string s_msg = "[" + ERR + "] " + msg;
|
||||
syslog( this->LOG_LEVEL, s_msg.c_str() );
|
||||
|
||||
if ( LOG_LEVEL == E_FATAL | LOG_LEVEL == E_WARN )
|
||||
{
|
||||
std::cerr << "[" << this->get_8601() << "]\t[" << this->mask << "]\t[" << ERR << "]\t" << msg.c_str() << std::endl;
|
||||
} else {
|
||||
std::cout << "[" << this->get_8601() << "]\t[" << this->mask << "]\t[" << ERR << "]\t" << msg.c_str() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string Logger::get_8601()
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto itt = std::chrono::system_clock::to_time_t(now);
|
||||
std::ostringstream ss;
|
||||
// ss << std::put_time(gmtime(&itt), "%FT%TZ");
|
||||
ss << std::put_time(localtime(&itt), "%Y-%m-%d_%H:%M:%S");
|
||||
return ss.str();
|
||||
}
|
||||
35
src/Logger/Logger.h
Normal file
35
src/Logger/Logger.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by bagira on 6/13/20.
|
||||
//
|
||||
|
||||
#ifndef EXAMPLAR_LOGGER_H
|
||||
#define EXAMPLAR_LOGGER_H
|
||||
|
||||
#include <syslog.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
enum L_LVL {
|
||||
E_INFO,
|
||||
E_FATAL,
|
||||
E_WARN,
|
||||
E_DEBUG
|
||||
};
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
Logger( int LOG_LEVEL, std::string mask );
|
||||
void log( int LOG_LEVEL, std::string msg );
|
||||
|
||||
private:
|
||||
int LOG_LEVEL;
|
||||
const char * mask;
|
||||
std::string get_8601();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //EXAMPLAR_LOGGER_H
|
||||
Reference in New Issue
Block a user