|
@ -13,7 +13,7 @@ |
|
|
#include <android/log.h> |
|
|
#include <android/log.h> |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
using namespace std; |
|
|
//using namespace std;
|
|
|
|
|
|
|
|
|
namespace pEp { |
|
|
namespace pEp { |
|
|
namespace Adapter { |
|
|
namespace Adapter { |
|
@ -21,8 +21,8 @@ namespace pEp { |
|
|
int line_width = 120; |
|
|
int line_width = 120; |
|
|
|
|
|
|
|
|
// NON CLASS
|
|
|
// NON CLASS
|
|
|
mutex mtx; |
|
|
std::mutex mtx; |
|
|
atomic_bool is_enabled{ false }; |
|
|
std::atomic_bool is_enabled{ false }; |
|
|
|
|
|
|
|
|
void set_enabled(const bool& enabled) |
|
|
void set_enabled(const bool& enabled) |
|
|
{ |
|
|
{ |
|
@ -35,52 +35,52 @@ namespace pEp { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Common "print" function implementing the actual "backends"
|
|
|
// Common "print" function implementing the actual "backends"
|
|
|
void _log(const string& msg, Utils::Color col = Utils::Color::WHITE) |
|
|
void _log(const std::string& msg, Utils::Color col = Utils::Color::WHITE) |
|
|
{ |
|
|
{ |
|
|
lock_guard<mutex> l(mtx); |
|
|
std::lock_guard<std::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 << Utils::to_termcol(col) << msg << Utils::to_termcol(Utils::Color::RESET) |
|
|
std::cerr << Utils::to_termcol(col) << msg << Utils::to_termcol(Utils::Color::RESET) |
|
|
<< endl; //endl also flushes, but cerr is unbuffered anyways
|
|
|
<< std::endl; //endl also flushes, but cerr is unbuffered anyways
|
|
|
#endif |
|
|
#endif |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void log(const string& msg, Utils::Color col) |
|
|
void log(const std::string& msg, Utils::Color col) |
|
|
{ |
|
|
{ |
|
|
_log(msg, col); |
|
|
_log(msg, col); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void logH1(const string& msg, Utils::Color col) |
|
|
void logH1(const std::string& msg, Utils::Color col) |
|
|
{ |
|
|
{ |
|
|
log(decorate_three_lines(msg, '='), col); |
|
|
log(decorate_three_lines(msg, '='), col); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void logH2(const string& msg, Utils::Color col) |
|
|
void logH2(const std::string& msg, Utils::Color col) |
|
|
{ |
|
|
{ |
|
|
log("\n" + decorate_centered(msg, '='), col); |
|
|
log("\n" + decorate_centered(msg, '='), col); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void logH3(const string& msg, Utils::Color col) |
|
|
void logH3(const std::string& msg, Utils::Color col) |
|
|
{ |
|
|
{ |
|
|
log(decorate_centered(msg, '-'), col); |
|
|
log(decorate_centered(msg, '-'), col); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
string decorate_three_lines(const string& msg, char decoration) |
|
|
std::string decorate_three_lines(const std::string& msg, char decoration) |
|
|
{ |
|
|
{ |
|
|
stringstream tmp; |
|
|
std::stringstream tmp; |
|
|
tmp << std::string(line_width, decoration) << endl |
|
|
tmp << std::string(line_width, decoration) << std::endl |
|
|
<< msg << endl |
|
|
<< msg << std::endl |
|
|
<< std::string(line_width, decoration); |
|
|
<< std::string(line_width, decoration); |
|
|
return tmp.str(); |
|
|
return tmp.str(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
string decorate_centered(const string& msg, char decoration) |
|
|
std::string decorate_centered(const std::string& msg, char decoration) |
|
|
{ |
|
|
{ |
|
|
stringstream tmp; |
|
|
std::stringstream tmp; |
|
|
size_t max_len = line_width - 10; |
|
|
size_t max_len = line_width - 10; |
|
|
// truncate msg
|
|
|
// truncate msg
|
|
|
string msg_truncated = msg; |
|
|
std::string msg_truncated = msg; |
|
|
if (msg.length() >= max_len) { |
|
|
if (msg.length() >= max_len) { |
|
|
msg_truncated = msg.substr(0, max_len); |
|
|
msg_truncated = msg.substr(0, max_len); |
|
|
msg_truncated += "..."; |
|
|
msg_truncated += "..."; |
|
@ -104,54 +104,73 @@ namespace pEp { |
|
|
namespace pEpLog { |
|
|
namespace pEpLog { |
|
|
// Class pEpLogger
|
|
|
// Class pEpLogger
|
|
|
|
|
|
|
|
|
int pEpLogger::auto_instance_nr = 0; |
|
|
int pEpLogger::_auto_instance_nr = 0; |
|
|
pEpLogger::pEpLogger(const string& classname, const bool& enabled) : |
|
|
pEpLogger::pEpLogger(const std::string& classname, const bool& enabled) : |
|
|
classname(classname), is_enabled(enabled) |
|
|
_is_enabled(enabled), |
|
|
|
|
|
_classname(classname) |
|
|
{ |
|
|
{ |
|
|
auto_instance_nr++; |
|
|
_auto_instance_nr++; |
|
|
this->set_instancename(to_string(auto_instance_nr)); |
|
|
set_instancename(std::to_string(_auto_instance_nr)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void pEpLogger::log(const string& msg, Utils::Color col) const |
|
|
pEpLogger::pEpLogger(const pEpLogger& rhs) : |
|
|
|
|
|
_is_enabled{ rhs._is_enabled }, |
|
|
|
|
|
_classname{ rhs._classname }, |
|
|
|
|
|
_instancename{ rhs._instancename } |
|
|
|
|
|
{ |
|
|
|
|
|
_auto_instance_nr++; |
|
|
|
|
|
set_instancename(std::to_string(_auto_instance_nr)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pEpLogger& pEpLogger::operator=(const pEpLogger& rhs) |
|
|
|
|
|
{ |
|
|
|
|
|
_is_enabled = rhs._is_enabled; |
|
|
|
|
|
_classname = rhs._classname; |
|
|
|
|
|
_auto_instance_nr++; |
|
|
|
|
|
set_instancename(std::to_string(_auto_instance_nr)); |
|
|
|
|
|
return *this; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void pEpLogger::log(const std::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_ << get_classname() << "[" << get_instancename() << "]"; |
|
|
msg_ << " - " << msg; |
|
|
msg_ << " - " << msg; |
|
|
this->logRaw(msg_.str(), col); |
|
|
logRaw(msg_.str(), col); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void pEpLogger::logRaw(const string& msg, Utils::Color col) const |
|
|
void pEpLogger::logRaw(const std::string& msg, Utils::Color col) const |
|
|
{ |
|
|
{ |
|
|
if (this->is_enabled) { |
|
|
if (_is_enabled) { |
|
|
_log(msg, col); |
|
|
_log(msg, col); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void pEpLogger::set_enabled(const bool& enabled) |
|
|
void pEpLogger::set_enabled(const bool& enabled) |
|
|
{ |
|
|
{ |
|
|
this->is_enabled = enabled; |
|
|
_is_enabled = enabled; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bool pEpLogger::get_enabled() const |
|
|
bool pEpLogger::get_enabled() const |
|
|
{ |
|
|
{ |
|
|
return this->is_enabled; |
|
|
return _is_enabled; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
string pEpLogger::get_classname() const |
|
|
std::string pEpLogger::get_classname() const |
|
|
{ |
|
|
{ |
|
|
return this->classname; |
|
|
return _classname; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void pEpLogger::set_instancename(const string& name) |
|
|
void pEpLogger::set_instancename(const std::string& name) |
|
|
{ |
|
|
{ |
|
|
this->instancename = name; |
|
|
_instancename = name; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
string pEpLogger::get_instancename() const |
|
|
std::string pEpLogger::get_instancename() const |
|
|
{ |
|
|
{ |
|
|
return this->instancename; |
|
|
return _instancename; |
|
|
} |
|
|
} |
|
|
} // namespace pEpLog
|
|
|
} // namespace pEpLog
|
|
|
} // namespace Adapter
|
|
|
} // namespace Adapter
|
|
|