Browse Source

pEpLog: Add termCol support

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

30
src/pEpLog.cc

@ -7,6 +7,7 @@
#include <mutex> #include <mutex>
#include <atomic> #include <atomic>
#include <cmath> #include <cmath>
#include "std_utils.hh"
#ifdef ANDROID #ifdef ANDROID
#include <android/log.h> #include <android/log.h>
@ -34,34 +35,35 @@ namespace pEp {
} }
// Common "print" function implementing the actual "backends" // 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); lock_guard<mutex> l(mtx);
#ifdef ANDROID #ifdef ANDROID
__android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str()); __android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str());
#else #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 #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) string decorate_three_lines(const string& msg, char decoration)
@ -111,20 +113,20 @@ namespace pEp {
this->set_instancename(to_string(auto_instance_nr)); 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_; std::stringstream msg_;
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]";
msg_ << " - "; msg_ << " - ";
msg_ << this->get_classname() << "[" << this->get_instancename() << "]"; msg_ << this->get_classname() << "[" << this->get_instancename() << "]";
msg_ << " - " << msg; 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) { if (this->is_enabled) {
_log(msg); _log(msg, col);
} }
} }

13
src/pEpLog.hh

@ -7,6 +7,7 @@
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include <unistd.h> #include <unistd.h>
#include "std_utils.hh"
// pEpLog // pEpLog
// ====== // ======
@ -133,10 +134,10 @@ namespace pEp {
// Logging functions to control pEpLog() macro // Logging functions to control pEpLog() macro
void set_enabled(const bool& is_enabled); void set_enabled(const bool& is_enabled);
bool get_enabled(); bool get_enabled();
void log(const std::string& msg); void log(const std::string& msg, Utils::Color col = Utils::Color::WHITE);
void logH1(const std::string& msg); void logH1(const std::string& msg, Utils::Color col = Utils::Color::WHITE);
void logH2(const std::string& msg); void logH2(const std::string& msg, Utils::Color col = Utils::Color::WHITE);
void logH3(const std::string& msg); 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_three_lines(const std::string& msg, char decoration = '-');
std::string decorate_centered(const std::string& msg, char decoration = '-'); std::string decorate_centered(const std::string& msg, char decoration = '-');
} // namespace pEpLog } // namespace pEpLog
@ -197,9 +198,9 @@ namespace pEp {
pEpLogger() = delete; pEpLogger() = delete;
pEpLogger(const std::string& classname, const bool& enabled); pEpLogger(const std::string& classname, const bool& enabled);
// Print a logging message in the format "thread - classname[instancename] - <msg>" // 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>" // 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); void set_enabled(const bool& enabled);
bool get_enabled() const; bool get_enabled() const;
std::string get_classname() const; std::string get_classname() const;

Loading…
Cancel
Save