34 void appendDomain(
const String &domain,
String &logMsg) {
35 if (!domain.isEmpty()) {
42 ::std::string stripFilename(const ::std::string &filename) {
43 if (filename.empty()) {
47 ::std::string::size_type pos = filename.find_last_of(
"/");
48 if (pos == ::std::string::npos) {
51 const ::std::string strippedName = filename.substr(pos + 1, filename.size() - pos - 1);
56 void addTraceInfo(
const String &file,
int line,
String &msg) {
59 std::string stripped = stripFilename(file.c_str());
60 msg +=
String(stripped.c_str(), stripped.size());
64 std::string lineno = ss.str();
65 msg +=
String(lineno.c_str(), lineno.size());
83Logger *Logger::sLogger =
nullptr;
86 if (sLogger ==
nullptr) {
94 if (logger == sLogger) {
102 if (sLogger ==
nullptr) {
103 static_cast<void>(
create());
110 if (sLogger !=
nullptr) {
127 logMsg +=
String(
"Trace: ", 6);
129 appendDomain(domain, logMsg);
138 logMsg +=
String(
"Dbg: ", 6);
140 appendDomain(domain, logMsg);
153 logMsg +=
String(
"Info: ", 6);
156 appendDomain(domain, logMsg);
167 if (msg.
size() > 8 && msg[6] ==
'<' && msg[7] ==
'=') {
172 if (0 != mIntention) {
173 for (uint32_t i = 0; i < mIntention; i++) {
180 logMsg +=
String(
" (", 2);
181 logMsg += this->getDateTime();
185 logMsg +=
String(
" \n", 2);
186 for (
size_t i = 0; i < mLogStreams.size(); ++i) {
188 if (stream !=
nullptr) {
189 stream->
write(logMsg);
193 if (msg.
size() > 8 && msg[6] ==
'=' && msg[7] ==
'>') {
200 logMsg +=
String(
"Warn: ", 6);
202 appendDomain(domain, logMsg);
209 logMsg +=
String(
"Err: ", 6);
211 appendDomain(domain, logMsg);
218 logMsg +=
String(
"Fatal:", 6);
220 appendDomain(domain, logMsg);
226 if (
nullptr == pLogStream) {
230 mLogStreams.add(pLogStream);
234 if (
nullptr == logStream) {
238 for (
size_t i = 0; i < mLogStreams.size(); ++i) {
239 if (mLogStreams[i] == logStream) {
240 delete mLogStreams[i];
241 mLogStreams.remove(i);
247 mLogStreams.
add(
new StdLogStream);
251 for (
size_t i = 0; i < mLogStreams.
size(); ++i) {
252 delete mLogStreams[i];
256String Logger::getDateTime()
const {
257 static constexpr uint32_t Space = 2;
258 DateTime currentDateTime;
259 std::stringstream stream;
261 stream << std::setw(Space) << currentDateTime.year <<
"."
262 << std::setw(Space) << currentDateTime.month <<
"."
263 << std::setw(Space * 2) << currentDateTime.day <<
" "
264 << std::setw(Space) << currentDateTime.hour <<
":"
265 << std::setw(Space) << currentDateTime.minute <<
":"
266 << std::setw(Space) << currentDateTime.second;
267 const std::string tmp(stream.str());
268 String dateTime(tmp.c_str(), tmp.size());
272void Logger::StdLogStream::write(
const String &msg) {
273 std::cout << msg.c_str();
279 addTraceInfo(file, line, message);
286 addTraceInfo(file, line, message);
293 addTraceInfo(file, line, message);
300 addTraceInfo(file, line, msg);
307 addTraceInfo(file, line, msg);
314 addTraceInfo(file, line, msg);
This pure abstract interface class declares the protocol for log-stream implementation.
virtual void deactivate()
The stream will be deactivated.
virtual bool isActive() const
Returns true, if the stream is active.
virtual void write(const String &message)=0
Will write a message into the attached log stream.
virtual void activate()
The stream will be activated.
This class implements a simple logger.
void setVerboseMode(VerboseMode sev)
Set the severity of the logger.
VerboseMode
Describes the verbose mode.
@ Normal
Only warnings and errors will be logged.
@ Debug
All debug messages will be logged as well.
@ Verbose
Normal messages will be logged as well.
@ Trace
Will enable the tracing.
void unregisterLogStream(const AbstractLogStream *pLogStream)
Unregisters a registered log stream.
static Logger * getInstance()
returns the singleton instance pointer of the logger.
void info(const String &domain, const String &msg)
Logs an info message.
static void set(Logger *logger)
Will set a user-defined logger instance.
static void kill()
Kills the only instance of the logger.
PrintMode
Describes the mode for prints into the active log stream.
@ WithDateTime
A dateTime string will put be in front of the entry.
void trace(const String &domain, const String &msg)
Logs a trace message.
void warn(const String &domain, const String &msg)
Logs a warn message.
void print(const String &message, PrintMode mode=PrintMode::WithoutDateTime)
Logs a print message.
void debug(const String &domain, const String &msg)
Logs a debug message.
void error(const String &domain, const String &msg)
Logs a error message.
static Logger * create()
Creates the unique logger instance and returns a pointer showing to it.
VerboseMode getVerboseMode() const
Returns the current severity of the logger.
void fatal(const String &domain, const String &msg)
Logs a fatal error message.
void registerLogStream(AbstractLogStream *pLogStream)
Registers a new log stream.
size_t size() const
The current size of the array will be returned.
void add(const T &newValue)
A new item will be added to the array.
size_t size() const
Returns the size of the string buffer.
bool isEmpty() const
Will return true, if the string is empty.
TStringBase< char > String
void tracePrint(const String &domain, const String &file, int line, const String &msg)
void errorPrint(const String &domain, const String &file, int line, const String &msg)
void warnPrint(const String &domain, const String &file, int line, const String &msg)
void debugPrint(const String &domain, const String &file, int line, const String &msg)
void fatalPrint(const String &domain, const String &file, int line, const String &msg)
void infoPrint(const String &domain, const String &file, int line, const String &msg)