C++11 library providing functionality common to all adapters.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.6 KiB

  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #include "pEpLog.hh"
  4. #include <iostream>
  5. #include <sstream>
  6. #include <mutex>
  7. #include <atomic>
  8. #include <cmath>
  9. #include "std_utils.hh"
  10. #ifdef ANDROID
  11. #include <android/log.h>
  12. #endif
  13. using namespace std;
  14. namespace pEp {
  15. namespace Adapter {
  16. namespace pEpLog {
  17. int line_width = 120;
  18. // NON CLASS
  19. mutex mtx;
  20. atomic_bool is_enabled{ false };
  21. void set_enabled(const bool& enabled)
  22. {
  23. is_enabled.store(enabled);
  24. }
  25. bool get_enabled()
  26. {
  27. return is_enabled.load();
  28. }
  29. // Common "print" function implementing the actual "backends"
  30. void _log(const string& msg, Utils::Color col = Utils::Color::WHITE)
  31. {
  32. lock_guard<mutex> l(mtx);
  33. #ifdef ANDROID
  34. __android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str());
  35. #else
  36. cerr << Utils::to_termcol(col) << msg << Utils::to_termcol(Utils::Color::RESET)
  37. << endl; //endl also flushes, but cerr is unbuffered anyways
  38. #endif
  39. }
  40. void log(const string& msg, Utils::Color col)
  41. {
  42. _log(msg, col);
  43. }
  44. void logH1(const string& msg, Utils::Color col)
  45. {
  46. log(decorate_three_lines(msg, '='), col);
  47. }
  48. void logH2(const string& msg, Utils::Color col)
  49. {
  50. log( "\n" +decorate_centered(msg, '='), col);
  51. }
  52. void logH3(const string& msg, Utils::Color col)
  53. {
  54. log( decorate_centered(msg, '-'), col);
  55. }
  56. string decorate_three_lines(const string& msg, char decoration)
  57. {
  58. stringstream tmp;
  59. tmp << std::string(line_width, decoration) << endl
  60. << msg << endl
  61. << std::string(line_width, decoration);
  62. return tmp.str();
  63. }
  64. string decorate_centered(const string& msg, char decoration)
  65. {
  66. stringstream tmp;
  67. size_t max_len = line_width - 10;
  68. // truncate msg
  69. string msg_truncated = msg;
  70. if (msg.length() >= max_len) {
  71. msg_truncated = msg.substr(0, max_len);
  72. msg_truncated += "...";
  73. }
  74. // define decolen
  75. int decolen = static_cast<int>(
  76. floor((double(line_width - msg_truncated.length()))) / 2.0);
  77. tmp << std::string(decolen, decoration) << ' ' << msg_truncated << ' '
  78. << std::string(decolen, decoration);
  79. return tmp.str();
  80. }
  81. } // namespace pEpLog
  82. } // namespace Adapter
  83. } // namespace pEp
  84. namespace pEp {
  85. namespace Adapter {
  86. namespace pEpLog {
  87. // Class pEpLogger
  88. int pEpLogger::auto_instance_nr = 0;
  89. pEpLogger::pEpLogger(const string& classname, const bool& enabled) :
  90. classname(classname), is_enabled(enabled)
  91. {
  92. auto_instance_nr++;
  93. this->set_instancename(to_string(auto_instance_nr));
  94. }
  95. void pEpLogger::log(const string& msg, Utils::Color col) const
  96. {
  97. std::stringstream msg_;
  98. msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]";
  99. msg_ << " - ";
  100. msg_ << this->get_classname() << "[" << this->get_instancename() << "]";
  101. msg_ << " - " << msg;
  102. this->logRaw(msg_.str(), col);
  103. }
  104. void pEpLogger::logRaw(const string& msg, Utils::Color col) const
  105. {
  106. if (this->is_enabled) {
  107. _log(msg, col);
  108. }
  109. }
  110. void pEpLogger::set_enabled(const bool& enabled)
  111. {
  112. this->is_enabled = enabled;
  113. }
  114. bool pEpLogger::get_enabled() const
  115. {
  116. return this->is_enabled;
  117. }
  118. string pEpLogger::get_classname() const
  119. {
  120. return this->classname;
  121. }
  122. void pEpLogger::set_instancename(const string& name)
  123. {
  124. this->instancename = name;
  125. }
  126. string pEpLogger::get_instancename() const
  127. {
  128. return this->instancename;
  129. }
  130. } // namespace pEpLog
  131. } // namespace Adapter
  132. } // namespace pEp