This commit is contained in:
Erik Frojdh 2020-03-04 15:38:21 +01:00
parent 661adaf4ed
commit c6ddd19c0f
3 changed files with 32 additions and 28 deletions

View File

@ -3,8 +3,9 @@
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
int main(){ int main() {
//compare old and new
std::cout << "Compare output between old and new:\n"; std::cout << "Compare output between old and new:\n";
FILE_LOG(logINFO) << "Old message"; FILE_LOG(logINFO) << "Old message";
LOG(logINFO) << "New message"; LOG(logINFO) << "New message";
@ -13,20 +14,33 @@ int main(){
FILE_LOG(logWARNING) << "Old warning"; FILE_LOG(logWARNING) << "Old warning";
LOG(logWARNING) << "New warning"; LOG(logWARNING) << "New warning";
// // sls::Logger::ReportingLevel() = logERROR; //Logging level can be configure at runtime
std::cout << "\n\n";
std::cout << "The default macro controlled level is: "
<< sls::Logger::ToString(LOG_MAX_REPORTING_LEVEL) << '\n';
sls::Logger::ReportingLevel() = logERROR;
LOG(logINFO) << "Now this is not written";
LOG(logWARNING) << "and also not this";
// const auto N = 100000; sls::Logger::ReportingLevel() = logINFO;
// auto t0 = std::chrono::steady_clock::now(); LOG(logINFO) << "But now we can see it";
// for (int i = 0; i!=N; ++i){
// // LOG(logWARNING) << "HEY";
// FILE_LOG(logWARNING) << "HEY";
// }
// auto t1 = std::chrono::steady_clock::now();
// for (int i = 0; i!=N; ++i){
// LOG(logWARNING) << "HEY";
// }
// auto t2 = std::chrono::steady_clock::now();
// std::cout << "Old: " << (t1-t0).count() << "\n";
// std::cout << "New: " << (t2-t1).count() << "\n"; //The output can be redirected to another buffer
std::ostringstream local;
auto clog_buff = std::clog.rdbuf();
std::clog.rdbuf(local.rdbuf());
LOG(logINFOBLUE) << "A message";
LOG(logWARNING) << "And another one";
std::clog.rdbuf(clog_buff); // restore
std::cout << "local buf:\n" << local.str();
LOG(logINFO) << "After reset we should print directly";
LOG(logINFOBLUE) << "some infoBLUE text";
LOG(logINFOGREEN) << "some infoGREEN text";
LOG(logINFORED) << "some infoRED text";
} }

View File

@ -18,10 +18,10 @@ class Logger {
public: public:
Logger() = default; Logger() = default;
Logger(TLogLevel level) : level(level){}; explicit Logger(TLogLevel level) : level(level){};
~Logger() { ~Logger() {
// output in the destructor to allow for << syntax // output in the destructor to allow for << syntax
os << Reset() << '\n'; os << RESET << '\n';
std::clog << os.str(); // Single write std::clog << os.str(); // Single write
} }
@ -33,16 +33,11 @@ class Logger {
// Danger this buffer need as many elements as TLogLevel // Danger this buffer need as many elements as TLogLevel
static const char *Color(TLogLevel level) noexcept { static const char *Color(TLogLevel level) noexcept {
static const char *const colors[] = { static const char *const colors[] = {
RED BOLD, YELLOW BOLD, RESET, BLUE, RED, RESET, RED BOLD, YELLOW BOLD, BLUE, GREEN, RED, RESET,
RESET, RESET, RESET, RESET, RESET, RESET}; RESET, RESET, RESET, RESET, RESET, RESET};
return colors[level]; return colors[level];
} }
static const char *Reset() noexcept {
static const char *reset = RESET;
return reset;
}
// Danger this buffer need as many elements as TLogLevel // Danger this buffer need as many elements as TLogLevel
static std::string ToString(TLogLevel level) { static std::string ToString(TLogLevel level) {
static const char *const buffer[] = { static const char *const buffer[] = {

View File

@ -23,11 +23,6 @@ TEST_CASE("LogLevel to string") {
CHECK(Logger::ToString(logDEBUG5) == "DEBUG5"); CHECK(Logger::ToString(logDEBUG5) == "DEBUG5");
} }
TEST_CASE("get reset string"){
std::string reset(Logger::Reset());
CHECK(reset == RESET);
}
TEST_CASE("Test output") { TEST_CASE("Test output") {
auto old_value = Logger::ReportingLevel(); auto old_value = Logger::ReportingLevel();