Browse Source

pEpLog: Add termCol support

LIB-11
heck 4 years ago
parent
commit
8cb1fe5ea0
  1. 30
      src/pEpLog.cc
  2. 21
      src/pEpLog.hh

30
src/pEpLog.cc

@ -7,6 +7,7 @@
#include <mutex>
#include <atomic>
#include <cmath>
#include "std_utils.hh"
#ifdef ANDROID
#include <android/log.h>
@ -34,34 +35,35 @@ namespace pEp {
}
// Common "print" function implementing the actual "backends"
void _log(const string& msg)
void _log(const string& msg, Utils::Color col = Utils::Color::WHITE)
{
lock_guard<mutex> l(mtx);
#ifdef ANDROID
__android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str());
#else
cerr << msg << endl; //endl also flushes, but cerr is unbuffered anyways
cerr << Utils::to_termcol(col) << msg << Utils::to_termcol(Utils::Color::RESET)
<< endl; //endl also flushes, but cerr is unbuffered anyways
#endif
}
void log(const string& msg)
void log(const string& msg, Utils::Color col)
{
_log(msg);
_log(msg, col);
}
void logH1(const string& msg)
void logH1(const string& msg, Utils::Color col)
{
log(decorate_three_lines(msg, '='));
log(decorate_three_lines(msg, '='), col);
}
void logH2(const string& msg)
void logH2(const string& msg, Utils::Color col)
{
log(decorate_centered(msg, '='));
log(decorate_centered(msg, '='), col);
}
void logH3(const string& msg)
void logH3(const string& msg, Utils::Color col)
{
log(decorate_centered(msg, '-'));
log(decorate_centered(msg, '-'), col);
}
string decorate_three_lines(const string& msg, char decoration)
@ -111,20 +113,20 @@ namespace pEp {
this->set_instancename(to_string(auto_instance_nr));
}
void pEpLogger::log(const string& msg) const
void pEpLogger::log(const string& msg, Utils::Color col) const
{
std::stringstream msg_;
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]";
msg_ << " - ";
msg_ << this->get_classname() << "[" << this->get_instancename() << "]";
msg_ << " - " << msg;
this->logRaw(msg_.str());
this->logRaw(msg_.str(), col);
}
void pEpLogger::logRaw(const string& msg) const
void pEpLogger::logRaw(const string& msg, Utils::Color col) const
{
if (this->is_enabled) {
_log(msg);
_log(msg, col);
}
}

21
src/pEpLog.hh

@ -7,6 +7,7 @@
#include <sstream>
#include <thread>
#include <unistd.h>
#include "std_utils.hh"
// pEpLog
// ======
@ -38,7 +39,7 @@
do { \
if (pEp::Adapter::pEpLog::get_enabled()) { \
std::stringstream msg_; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
msg_ << " - " << msg; \
pEp::Adapter::pEpLog::log(msg_.str()); \
@ -56,7 +57,7 @@
do { \
if (pEp::Adapter::pEpLog::get_enabled()) { \
std::stringstream msg_; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
msg_ << " - " << pEp::Adapter::pEpLog::decorateH1(msg); \
pEp::Adapter::pEpLog::log(msg_.str()); \
@ -74,7 +75,7 @@
do { \
if (pEp::Adapter::pEpLog::get_enabled()) { \
std::stringstream msg_; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
msg_ << " - " << pEp::Adapter::pEpLog::decorateH2(msg); \
pEp::Adapter::pEpLog::log(msg_.str()); \
@ -133,10 +134,10 @@ namespace pEp {
// Logging functions to control pEpLog() macro
void set_enabled(const bool& is_enabled);
bool get_enabled();
void log(const std::string& msg);
void logH1(const std::string& msg);
void logH2(const std::string& msg);
void logH3(const std::string& msg);
void log(const std::string& msg, Utils::Color col = Utils::Color::WHITE);
void logH1(const std::string& msg, Utils::Color col = Utils::Color::WHITE);
void logH2(const std::string& msg, Utils::Color col = Utils::Color::WHITE);
void logH3(const std::string&msg, Utils::Color col = Utils::Color::WHITE);
std::string decorate_three_lines(const std::string& msg, char decoration = '-');
std::string decorate_centered(const std::string& msg, char decoration = '-');
} // namespace pEpLog
@ -165,7 +166,7 @@ namespace pEp {
#define pEpLogClass(msg) \
do { \
std::stringstream msg_; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
msg_ << " - " << this->m4gic_logger_n4me.get_classname(); \
msg_ << "[" << this->m4gic_logger_n4me.get_instancename() << "]"; \
msg_ << "::" << __FUNCTION__; \
@ -197,9 +198,9 @@ namespace pEp {
pEpLogger() = delete;
pEpLogger(const std::string& classname, const bool& enabled);
// Print a logging message in the format "thread - classname[instancename] - <msg>"
void log(const std::string& msg) const;
void log(const std::string& msg, Utils::Color col = Utils::Color::WHITE) const;
// Prints just "<msg>"
void logRaw(const std::string& msg) const;
void logRaw(const std::string& msg, Utils::Color col = Utils::Color::WHITE) const;
void set_enabled(const bool& enabled);
bool get_enabled() const;
std::string get_classname() const;

Loading…
Cancel
Save