Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add verbose console logger for troubleshooting purposes, activated by…
… setting an environment variable
- Loading branch information
1 parent
50611b0
commit b5038de
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#pragma once | ||
|
||
#include <sstream> | ||
#include <iostream> | ||
#include <vector> | ||
#include <memory> | ||
#include <mutex> | ||
#include "string/replace.h" | ||
#include "ilogwriter.h" | ||
|
||
namespace test | ||
{ | ||
|
||
/** | ||
* The ConsoleLogger writes all messages to the std output streams | ||
*/ | ||
class ConsoleLogger : | ||
public applog::ILogDevice | ||
{ | ||
private: | ||
applog::LogLevel _currentLevel; | ||
std::string _buffer; | ||
|
||
public: | ||
/** | ||
* greebo: This method gets called by the Writer with | ||
* a logging string as argument. | ||
*/ | ||
void writeLog(const std::string& outputStr, applog::LogLevel level) override | ||
{ | ||
// The text usually arrives in single characters at a time | ||
// In case the level changes, we need to flush the line | ||
if (_currentLevel != level) | ||
{ | ||
flushLine(); | ||
} | ||
|
||
// Write to the buffer first | ||
_currentLevel = level; | ||
_buffer.append(outputStr); | ||
|
||
// Once we hit a single newline, flush the line | ||
if (outputStr == "\n") | ||
{ | ||
flushLine(); | ||
} | ||
} | ||
|
||
private: | ||
void flushLine() | ||
{ | ||
if (!_buffer.empty()) | ||
{ | ||
// Replace NULL characters | ||
string::replace_all(_buffer, std::string(1, '\0'), "NULL"); | ||
|
||
switch (_currentLevel) | ||
{ | ||
case applog::LogLevel::Verbose: | ||
std::cout << "[VRB]: " << _buffer; | ||
break; | ||
case applog::LogLevel::Standard: | ||
std::cout << "[STD]: " << _buffer; | ||
break; | ||
case applog::LogLevel::Warning: | ||
std::cout << "[WRN]: " << _buffer; | ||
break; | ||
case applog::LogLevel::Error: | ||
std::cerr << "[ERR]: " << _buffer; | ||
break; | ||
default: | ||
std::cout << "[???]: " << _buffer; | ||
}; | ||
|
||
_buffer.clear(); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace applog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters