Skip to content

Conversation

@javiertringol
Copy link

The images are old, I’m too lazy to make them again.


image

terminal

image

file .log

image
image

test in windows , vs solution

I have a code in python that modifies all std::cout << xxxx << std::endl; to g_logger().info(xxxx)

but because it would make the review more difficult, I will do it later.

@MillhioreBT MillhioreBT added the feature New feature or functionality label Oct 29, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a structured logging system using spdlog to replace direct console output (std::cout) throughout the codebase. The new logger provides log levels (TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL), file rotation, formatted output, and signal-safe shutdown handling.

Key changes:

  • Added spdlog dependency and new Logger class with configurable log levels
  • Replaced std::cout and fmt::print statements with g_logger() calls
  • Added Lua bindings for logging functions (logInfo, logWarning, logError)

Reviewed Changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
vcpkg.json, CMakeLists.txt Added spdlog dependency
src/logger.h, src/logger.cpp New logger implementation using spdlog
src/otserv.cpp Logger initialization and converted startup messages to use logger
src/script.cpp, src/scriptmanager.cpp Converted script loading messages to use logger
src/luascript.h, src/luascript.cpp Added Lua bindings for logging functions
src/configmanager.h, src/configmanager.cpp Added LOG_LEVEL configuration option
src/databasemanager.cpp Converted database messages to use logger
src/iomap.cpp, src/iomapserialize.cpp Converted map loading messages to use logger
data/scripts/xml/actions.lua, data/scripts/globalevents/raids/raids.lua, data/lib/debugging/lua_version.lua Replaced io.write/print with new Lua logging functions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

src/logger.h Outdated
if (isEnabled(LogLevel::TRACE)) {
log(LogLevel::TRACE, msg);
}
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Nah, if there's a configuration entry for this, it should not be controlled by compile flags

src/logger.h Outdated
virtual void log(LogLevel level, std::string_view message) = 0;
};

Logger& g_logger();
Copy link
Member

Choose a reason for hiding this comment

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

If it's a function, call it getLogger. g_ is reserved for global variables (🤮)

default:
return LogLevel::INFO;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Why not directly use the spdlog enum?

src/logger.cpp Outdated
std::filesystem::path path(basePath);
std::string directory = path.parent_path().string();
std::string baseName = path.stem().string();
std::string extension = path.extension().string();
Copy link
Member

Choose a reason for hiding this comment

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

None of these need to be converted to string

src/logger.cpp Outdated
}
catch (const std::exception& e) {
fprintf(stderr, "Error creating logger: %s\n", e.what());
throw;
Copy link
Member

Choose a reason for hiding this comment

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

Rethrow if you won't handle, this will swallow the stack

src/logger.cpp Outdated

class LogWithSpdLog final : public Logger {
public:
LogWithSpdLog(std::string_view filePath, size_t rotateSize, size_t rotateFiles) {
Copy link
Member

Choose a reason for hiding this comment

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

This belongs to a free function createLogger that does all the preparation and then creates a new LogWithSpdlog when everything is checked

Copy link
Member

Choose a reason for hiding this comment

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

Could we use std::format facilities over fmt::format?

@ranisalt
Copy link
Member

Logging is a great addition, but do we need to wrap it? We could use spdlog directly, the API seems quite clean and easy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants