lokinet/llarp/util/logging/ostream_logger.cpp

57 lines
1.3 KiB
C++
Raw Normal View History

2019-09-01 12:10:49 +00:00
#include <util/logging/ostream_logger.hpp>
#include <util/logging/logger_internal.hpp>
2019-04-11 12:58:23 +00:00
namespace llarp
{
2019-09-16 19:40:31 +00:00
OStreamLogStream::OStreamLogStream(bool withColours, std::ostream& out)
: m_withColours(withColours), m_Out(out)
2019-04-11 12:58:23 +00:00
{
}
void
OStreamLogStream::PreLog(std::stringstream& ss, LogLevel lvl,
2019-06-13 13:26:34 +00:00
const char* fname, int lineno,
const std::string& nodename) const
2019-04-11 12:58:23 +00:00
{
2019-09-16 19:40:31 +00:00
if(m_withColours)
2019-04-11 12:58:23 +00:00
{
2019-09-16 19:40:31 +00:00
switch(lvl)
{
case eLogNone:
break;
case eLogDebug:
ss << (char)27 << "[0m";
break;
case eLogInfo:
ss << (char)27 << "[1m";
break;
case eLogWarn:
ss << (char)27 << "[1;33m";
break;
case eLogError:
ss << (char)27 << "[1;31m";
break;
}
2019-04-11 12:58:23 +00:00
}
2019-06-13 13:26:34 +00:00
ss << "[" << LogLevelToString(lvl) << "] ";
ss << "[" << nodename << "]"
<< "(" << thread_id_string() << ") " << log_timestamp() << " " << fname
2019-04-11 12:58:23 +00:00
<< ":" << lineno << "\t";
}
void
OStreamLogStream::PostLog(std::stringstream& ss) const
{
2019-09-16 19:40:31 +00:00
if(m_withColours)
ss << (char)27 << "[0;0m";
ss << std::endl;
2019-04-11 12:58:23 +00:00
}
void
OStreamLogStream::Print(LogLevel, const char*, const std::string& msg)
2019-04-11 12:58:23 +00:00
{
m_Out << msg << std::flush;
2019-04-11 12:58:23 +00:00
}
} // namespace llarp