-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
201c815
0b0c425
80e9a93
2c3c24a
4e729a4
dd486cf
123a525
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||
} | ||||
} | ||||
kimkulling marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
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; | ||||
} | ||||
|
||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
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 | ||||
} | ||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
Logger::~Logger() { | ||||
for (size_t i = 0; i < mLogStreams.size(); ++i) { | ||||
delete mLogStreams[i]; | ||||
} | ||||
} | ||||
|
||||
String Logger::getDateTime() { | ||||
static const uint32_t Space = 2; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix unused variable warning. The variable - static const uint32_t Space = 2; 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Actions: CMake[warning] 253-253: Compiler warning: unused variable ‘Space’ [-Wunused-variable] 🤖 Prompt for AI Agents
|
||||
/* 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 |
Uh oh!
There was an error while loading. Please reload this page.