pEpLog - add copy assign operator for pEpLogger AND a lot of comestic changes

pull/1/head
heck 3 months ago
parent 5c7a02fec8
commit 431327b0b5
  1. 91
      src/pEpLog.cc
  2. 10
      src/pEpLog.hh

@ -13,7 +13,7 @@
#include <android/log.h>
#endif
using namespace std;
//using namespace std;
namespace pEp {
namespace Adapter {
@ -21,8 +21,8 @@ namespace pEp {
int line_width = 120;
// NON CLASS
mutex mtx;
atomic_bool is_enabled{ false };
std::mutex mtx;
std::atomic_bool is_enabled{ false };
void set_enabled(const bool& enabled)
{
@ -35,52 +35,52 @@ namespace pEp {
}
// 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
__android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str());
#else
cerr << Utils::to_termcol(col) << msg << Utils::to_termcol(Utils::Color::RESET)
<< endl; //endl also flushes, but cerr is unbuffered anyways
std::cerr << Utils::to_termcol(col) << msg << Utils::to_termcol(Utils::Color::RESET)
<< std::endl; //endl also flushes, but cerr is unbuffered anyways
#endif
}
void log(const string& msg, Utils::Color col)
void log(const std::string& msg, Utils::Color 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);
}
void logH2(const string& msg, Utils::Color col)
void logH2(const std::string& msg, Utils::Color 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);
}
string decorate_three_lines(const string& msg, char decoration)
std::string decorate_three_lines(const std::string& msg, char decoration)
{
stringstream tmp;
tmp << std::string(line_width, decoration) << endl
<< msg << endl
std::stringstream tmp;
tmp << std::string(line_width, decoration) << std::endl
<< msg << std::endl
<< std::string(line_width, decoration);
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;
// truncate msg
string msg_truncated = msg;
std::string msg_truncated = msg;
if (msg.length() >= max_len) {
msg_truncated = msg.substr(0, max_len);
msg_truncated += "...";
@ -104,54 +104,73 @@ namespace pEp {
namespace pEpLog {
// Class pEpLogger
int pEpLogger::auto_instance_nr = 0;
pEpLogger::pEpLogger(const string& classname, const bool& enabled) :
classname(classname), is_enabled(enabled)
int pEpLogger::_auto_instance_nr = 0;
pEpLogger::pEpLogger(const std::string& classname, const bool& enabled) :
_is_enabled(enabled),
_classname(classname)
{
auto_instance_nr++;
this->set_instancename(to_string(auto_instance_nr));
_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_;
msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]";
msg_ << " - ";
msg_ << this->get_classname() << "[" << this->get_instancename() << "]";
msg_ << get_classname() << "[" << get_instancename() << "]";
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);
}
}
void pEpLogger::set_enabled(const bool& enabled)
{
this->is_enabled = enabled;
_is_enabled = enabled;
}
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 Adapter

@ -203,6 +203,8 @@ namespace pEp {
public:
pEpLogger() = delete;
pEpLogger(const std::string& classname, const bool& enabled);
pEpLogger(const pEpLogger& rhs);
pEpLogger& operator=(const pEpLogger& rhs);
// Print a logging message in the format "thread - classname[instancename] - <msg>"
void log(const std::string& msg, Utils::Color col = Utils::Color::WHITE) const;
// Prints just "<msg>"
@ -215,10 +217,10 @@ namespace pEp {
std::string get_instancename() const;
private:
static int auto_instance_nr;
bool is_enabled;
std::string classname;
std::string instancename;
static int _auto_instance_nr;
bool _is_enabled;
std::string _classname;
std::string _instancename;
};
} // namespace pEpLog
} // namespace Adapter

Loading…
Cancel
Save