log.cpp 2.62 KB
#include "log.h"

#include "threadcontext.h"

// std
#include <ios>
#include <iomanip>
#include <sstream>
#include <syslog.h>
#include <sys/types.h>
#include <thread>
#include <unistd.h>

using namespace osdev::components::mqtt;

namespace {

std::string toString( LogLevel level )
{
    switch (level)
    {
        case LogLevel::Trace:
            return "T";
        case LogLevel::Debug:
            return "D";
        case LogLevel::Info:
            return "I";
        case LogLevel::Warning:
            return "W";
        case LogLevel::Error:
            return "E";
    }
    return "U";
}

} // namespace

const std::string Log::s_startMarker = std::string("|________________________________________________________________________________________________________________________________|");
std::string Log::s_context = std::string();
std::string Log::s_fileName = std::string();
LogLevel Log::s_logLevel = LogLevel::Info;

void Log::init( const std::string &context, const std::string &logFile, LogLevel logDepth )
{
    s_logLevel = logDepth;
    s_context = context;

    if( !logFile.empty())
    {
        s_fileName = logFile;
    }
}

void Log::terminate()
{
    s_context.clear();
    s_fileName.clear();
    s_logLevel = LogLevel::Info;
}

void Log::log( const std::string &category, const std::string &message, LogLevel level )
{
    std::string logCategory = s_context + '|' + toString( level ) + '|' + ThreadContext::instance().context() + '|' + category;
    std::string logMessage = message;
    if( logMessage.empty() )
    {
        static const std::string emptyMessage( "--" );
        logMessage = emptyMessage;
    }
    else
    {
        // Replace % signs
        ReplaceAll( logMessage, "%", "%%" );
    }

    writeLog( logCategory, logMessage, level );
}

void Log::writeLog(const std::string &category, const std::string &message, LogLevel level)
{
    if( s_fileName.empty() )
    {
        (void)level;
        // To be implemented.
        return;
    }
    else
    {
        setlogmask( LOG_UPTO( LOG_NOTICE ) );
        openlog( category.c_str(), LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1 );

        syslog( LOG_NOTICE, "%s", message.c_str() );

        closelog();
    }
}

void Log::ReplaceAll( std::string &strToReplace,
                      const std::string& from_chars,
                      const std::string& to_chars )
{
    size_t start_pos = 0;
    while( ( start_pos = strToReplace.find( from_chars, start_pos ) ) != std::string::npos )
    {
        strToReplace.replace( start_pos, from_chars.length(), to_chars );
        start_pos += to_chars.length(); // Handles case where 'to' is a substring of 'from'
    }
}