Skip to content

Feature/add logger #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ SET(cppcore_src

SET(cppcore_common_src
include/cppcore/Common/Hash.h
include/cppcore/Common/Logger.h
code/Common/Logger.cpp
include/cppcore/Common/TStringBase.h
include/cppcore/Common/TStringView.h
include/cppcore/Common/Variant.h
Expand Down
318 changes: 318 additions & 0 deletions code/Common/Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2014-2025 Kim Kulling

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#include <cppcore/Common/Logger.h>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

namespace cppcore {

static constexpr char Line[] =
"====================================================================================================";

static void appendDomain(const String &domain, String &logMsg) {
if (!domain.isEmpty()) {
logMsg += '(';
logMsg += domain;
logMsg += ')';
}
}

static ::std::string stripFilename(const ::std::string &filename) {
if (filename.empty()) {
return filename;
}

::std::string::size_type pos = filename.find_last_of("/");
if (pos == ::std::string::npos) {
return filename;
}
const ::std::string strippedName = filename.substr(pos + 1, filename.size() - pos - 1);

return strippedName;
}

static void addTraceInfo(const String &file, int line, String &msg) {
if (Logger::getInstance()->getVerboseMode() == Logger::VerboseMode::Trace) {
msg += String(" (", 2);
std::string stripped = stripFilename(file.c_str());
msg += String(stripped.c_str(), stripped.size());
msg += String(", ", 2);
std::stringstream ss;
ss << line;
std::string lineno = ss.str();
msg += String(lineno.c_str(), lineno.size());
msg += ')';
}
}

void AbstractLogStream::activate() {
mIsActive = true;
}

void AbstractLogStream::desactivate() {
mIsActive = false;
}

bool AbstractLogStream::isActive() const {
return mIsActive;
}

Logger *Logger::sLogger = nullptr;

Logger *Logger::create() {
if (nullptr == sLogger) {
sLogger = new Logger;
}

return sLogger;
}

Logger *Logger::getInstance() {
if (nullptr == sLogger) {
static_cast<void>(create());
}

return sLogger;
}

void Logger::kill() {
if (sLogger) {
delete sLogger;
sLogger = nullptr;
}
}

void Logger::setVerboseMode(VerboseMode sev) {
mVerboseMode = sev;
}

Logger::VerboseMode Logger::getVerboseMode() const {
return mVerboseMode;
}

void Logger::trace(const String &domain, const String &msg) {
if (getVerboseMode() == VerboseMode::Trace) {
String logMsg;
logMsg += String("Trace: ", 6);
logMsg += msg;
appendDomain(domain, logMsg);

print(logMsg);
}
}

void Logger::debug(const String &domain, const String &msg) {
if (getVerboseMode() == VerboseMode::Debug || getVerboseMode() == VerboseMode::Trace) {
String logMsg;
logMsg += String("Dbg: ", 6);
logMsg += msg;
appendDomain(domain, logMsg);

print(logMsg);
}
}

void Logger::info(const String &domain, const String &msg) {
if (getVerboseMode() == VerboseMode::Normal || getVerboseMode() == VerboseMode::Verbose || getVerboseMode() == VerboseMode::Debug || getVerboseMode() == VerboseMode::Trace) {
String logMsg;

logMsg += String("Info: ", 6);
logMsg += msg;

appendDomain(domain, logMsg);

print(logMsg);
}
}

void Logger::print(const String &msg, PrintMode mode) {
if (msg.isEmpty()) {
return;
}

if (msg.size() > 8) {
if (msg[6] == '<' && msg[7] == '=') {
mIntention -= 2;
}
}

String logMsg;
if (0 != mIntention) {
for (uint32_t i = 0; i < mIntention; i++) {
logMsg += ' ';
}
}

logMsg += msg;
if (PrintMode::WithDateTime == mode) {
logMsg += String(" (", 2);
logMsg += this->getDateTime();
logMsg += ')';
}

logMsg += String(" \n", 2);
for (size_t i = 0; i < mLogStreams.size(); ++i) {
AbstractLogStream *stream = mLogStreams[i];
if (stream != nullptr) {
stream->write(logMsg);
}
}

if (msg.size() > 8) {
if (msg[6] == '=' && msg[7] == '>') {
mIntention += 2;
}
}
}

void Logger::warn(const String &domain, const String &msg) {
String logMsg;
logMsg += String("Warn: ", 6);
logMsg += msg;
appendDomain(domain, logMsg);

print(logMsg);
}

void Logger::error(const String &domain, const String &msg) {
String logMsg;
logMsg += String("Err: ", 6);
logMsg += msg;
appendDomain(domain, logMsg);

print(logMsg);
}

void Logger::fatal(const String &domain, const String &msg) {
String logMsg;
logMsg += String("Fatal:", 6);
logMsg += msg;
appendDomain(domain, logMsg);

print(logMsg);
}

void Logger::registerLogStream(AbstractLogStream *pLogStream) {
if (nullptr == pLogStream) {
return;
}

mLogStreams.add(pLogStream);
}

void Logger::unregisterLogStream(AbstractLogStream *logStream) {
if (nullptr != logStream) {
return;
}

for (size_t i = 0; i < mLogStreams.size(); ++i) {
if (mLogStreams[i] == logStream) {
delete mLogStreams[i];
mLogStreams.remove(i);
}
}
}

Logger::Logger() :
mLogStreams(),
mVerboseMode(VerboseMode::Normal),
mIntention(0) {
mLogStreams.add(new StdLogStream);

#ifdef OSRE_WINDOWS
mLogStreams.add(new Platform::Win32DbgLogStream);
#endif // OSRE_WINDOWS
}

Logger::~Logger() {
for (size_t i = 0; i < mLogStreams.size(); ++i) {
delete mLogStreams[i];
}
}

String Logger::getDateTime() {
static const uint32_t Space = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix unused variable warning.

The variable Space is declared but unused since the DateTime implementation is commented out. Either implement the DateTime functionality or remove the unused variable to fix the compiler warning.

-    static const uint32_t Space = 2;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static const uint32_t Space = 2;
🧰 Tools
🪛 GitHub Actions: CMake

[warning] 253-253: Compiler warning: unused variable ‘Space’ [-Wunused-variable]

🤖 Prompt for AI Agents
In code/Common/Logger.cpp at line 253, the variable 'Space' is declared but
unused because the DateTime implementation is commented out. To fix the compiler
warning, either remove the declaration of 'Space' if the DateTime functionality
is not needed, or uncomment and implement the DateTime code that uses 'Space' so
the variable is utilized.

/* DateTime currentDateTime = DateTime::getCurrentUTCTime();
std::stringstream stream;
stream.fill('0');
stream << std::setw(Space) << currentDateTime.getCurrentDay() << "."
<< std::setw(Space) << currentDateTime.getCurrentMonth() << "."
<< std::setw(Space * 2) << currentDateTime.getCurrentYear() << " "
<< std::setw(Space) << currentDateTime.getCurrentHour() << ":"
<< std::setw(Space) << currentDateTime.getCurrentMinute() << ":"
<< std::setw(Space) << currentDateTime.getCurrentSeconds();
*/
String todo("none", 4);
return todo;
}

void Logger::StdLogStream::write(const String &msg) {
std::cout << msg.c_str();
}

void tracePrint(const String &domain, const String &file, int line, const String &msg) {
String message;
message += msg;
addTraceInfo(file, line, message);
Logger::getInstance()->trace(domain, message);
}

void debugPrint(const String &domain, const String &file, int line, const String &msg) {
String message;
message += msg;
addTraceInfo(file, line, message);
Logger::getInstance()->debug(domain, message);
}

void infoPrint(const String &domain, const String &file, int line, const String &msg) {
String message;
message += msg;
addTraceInfo(file, line, message);
Logger::getInstance()->info(domain, message);
}

void warnPrint(const String &domain, const String &file, int line, const String &message) {
String msg;
msg += message;
addTraceInfo(file, line, msg);
Logger::getInstance()->warn(domain, msg);
}

void errorPrint(const String &domain, const String &file, int line, const String &message) {
String msg;
msg += message;
addTraceInfo(file, line, msg);
Logger::getInstance()->error(domain, msg);
}

void fatalPrint(const String &domain, const String &file, int line, const String &message) {
String msg;
msg += message;
addTraceInfo(file, line, msg);
Logger::getInstance()->fatal(domain, msg);
}

} // namespace cppcore
Loading