Browse Source

pEpLog remove pEpEngine logging API / Add #ifdef and __android_log_print()

android-build
heck 2 years ago
parent
commit
082aec794c
2 changed files with 14 additions and 81 deletions
  1. +9
    -60
      pEpLog.cc
  2. +5
    -21
      pEpLog.hh

+ 9
- 60
pEpLog.cc View File

@ -2,10 +2,9 @@
// see LICENSE.txt
#include "pEpLog.hh"
#include <iostream> // cout and cerr
#include <sstream> // for stringstream
#include <pEp/pEpEngine.h> // for log_event()
#include "Adapter.hh" // for session()
#include <iostream>
#include <sstream>
#include <atomic>
namespace pEp {
namespace Adapter {
@ -13,12 +12,7 @@ namespace pEpLog {
std::mutex mtx;
std::atomic_bool is_initialized{false};
std::atomic_bool is_enabled{false};
std::atomic_bool is_enabled_backend_pEpEngine{true};
std::atomic_bool is_enabled_backend_cout{false};
std::atomic_bool is_enabled_backend_cerr{false};
void set_enabled(bool enabled) {
is_enabled.store(enabled);
@ -28,59 +22,14 @@ bool get_enabled() {
return is_enabled.load();
}
void set_enabled_cout(bool enabled){
is_enabled_backend_cout.store(enabled);
}
bool get_enabled_cout(){
return is_enabled_backend_cout.load();
}
void set_enabled_cerr(bool enabled){
is_enabled_backend_cerr.store(enabled);
}
bool get_enabled_cerr(){
return is_enabled_backend_cerr.load();
}
void set_enabled_pEpEngine(bool enabled){
is_enabled_backend_pEpEngine.store(enabled);
}
bool get_enabled_pEpEngine(){
return is_enabled_backend_pEpEngine.load();
}
void log_pEpEngine(std::string &msg) {
if (!is_initialized.load()) {
::config_service_log(pEp::Adapter::session(), true);
::log_service(pEp::Adapter::session(), "pEpLog init", nullptr, nullptr, nullptr);
is_initialized.store(true);
}
::log_service(pEp::Adapter::session(), msg.c_str(), nullptr, nullptr, nullptr);
}
void log_cout(std::string &msg) {
std::cout << msg << std::endl; //std::endl also flushes
}
void log_cerr(std::string &msg) {
std::cerr << msg << std::endl; //std::endl also flushes
}
void log(std::string msg) {
std::lock_guard<std::mutex> l(mtx);
if (is_enabled.load()) {
if (is_enabled_backend_cout.load()) {
log_cout(msg);
}
if (is_enabled_backend_cerr.load()) {
log_cerr(msg);
}
if (is_enabled_backend_pEpEngine.load()) {
log_pEpEngine(msg);
}
std::lock_guard<std::mutex> l(mtx);
#if defined(ANDROID) && !defined(NDEBUG)
__android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str());
#else
std::cout << msg << std::endl; //std::endl also flushes
#endif
}
}


+ 5
- 21
pEpLog.hh View File

@ -9,15 +9,14 @@
// pEpLog
// ======
// a "to be kept small and simple" logging unit.
// a "to be kept ultra small and simple" logging unit.
// featuring:
// * pEpLog macro that will be eliminated in release-builds (-DNDEBUG=1)
// * all functions thread safe (no interleave when logging from diff threads)
// * logging backend: pEpEngine logging API (providing platform abstractions and portability)
// * logging backend: cout
// * logging backend: cerr
// * thread safe (no interleave when logging from diff threads)
// * OS dependent backend switches:
// * android: __android_log_print
// * all other OS: cout
// * runtime enabled/disabled switch (global)
// * runtime enabled/disabled switch per backend
//
// You might want more and more features, but the feature-policy is very restrictive, and there is a
// primary design goal to keep it simple, maintainable and portable.
@ -28,9 +27,6 @@
// use NDEBUG=1 to turn logging on/off at compile-time
// use set_enabled(bool) to turn logging on/off at runtime
// use set_enabled_<backend>(bool) to turn logging on/off per backend
// All these functions are thread-safe
//
// Thats all there is to it.
#ifdef NDEBUG
#define pEpLog(msg) do{}while(0)
@ -53,18 +49,6 @@ void set_enabled(bool is_enabled);
bool get_enabled();
void set_enabled_cout(bool is_enabled);
bool get_enabled_cout();
void set_enabled_cerr(bool is_enabled);
bool get_enabled_cerr();
void set_enabled_pEpEngine(bool is_enabled);
bool get_enabled_pEpEngine();
} // pEpLog
} // Adapter
} // pEp


Loading…
Cancel
Save