From 05153d0b05401c841ddf7a1ccc231a8fcc85162e Mon Sep 17 00:00:00 2001 From: Arnaud Botella Date: Thu, 5 Mar 2026 11:50:57 +0100 Subject: [PATCH] feat(Logger): add file logger client --- include/geode/basic/file_logger_client.hpp | 54 ++++++++++ src/geode/basic/CMakeLists.txt | 2 + src/geode/basic/file_logger_client.cpp | 117 +++++++++++++++++++++ tests/basic/test-logger.cpp | 3 + 4 files changed, 176 insertions(+) create mode 100644 include/geode/basic/file_logger_client.hpp create mode 100644 src/geode/basic/file_logger_client.cpp diff --git a/include/geode/basic/file_logger_client.hpp b/include/geode/basic/file_logger_client.hpp new file mode 100644 index 000000000..e499d1dd2 --- /dev/null +++ b/include/geode/basic/file_logger_client.hpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * 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. + * + */ + +#pragma once + +#include +#include +#include + +namespace geode +{ + class opengeode_basic_api FileLoggerClient : public LoggerClient + { + public: + FileLoggerClient( std::string_view file_path ); + ~FileLoggerClient(); + + private: + void trace( const std::string &message ) override; + + void debug( const std::string &message ) override; + + void info( const std::string &message ) override; + + void warn( const std::string &message ) override; + + void error( const std::string &message ) override; + + void critical( const std::string &message ) override; + + private: + IMPLEMENTATION_MEMBER( impl_ ); + }; +} // namespace geode diff --git a/src/geode/basic/CMakeLists.txt b/src/geode/basic/CMakeLists.txt index 4f7da125f..93b50b166 100644 --- a/src/geode/basic/CMakeLists.txt +++ b/src/geode/basic/CMakeLists.txt @@ -33,6 +33,7 @@ add_geode_library( "console_logger_client.cpp" "console_progress_logger_client.cpp" "file.cpp" + "file_logger_client.cpp" "filename.cpp" "identifier.cpp" "identifier_builder.cpp" @@ -64,6 +65,7 @@ add_geode_library( "constant_attribute.hpp" "factory.hpp" "file.hpp" + "file_logger_client.hpp" "filename.hpp" "growable.hpp" "identifier.hpp" diff --git a/src/geode/basic/file_logger_client.cpp b/src/geode/basic/file_logger_client.cpp new file mode 100644 index 000000000..6bf98c4b7 --- /dev/null +++ b/src/geode/basic/file_logger_client.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * 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 + +// clang-format off +#include + +#include +// clang-format on + +#include +#include + +namespace geode +{ + class FileLoggerClient::Impl + { + public: + Impl( std::string_view file_path ) + : logger_impl_( spdlog::basic_logger_mt( + "basic_logger", to_string( file_path ) ) ) + { + spdlog::set_level( spdlog::level::level_enum::trace ); + } + + void trace( const std::string &message ) + { + logger_impl_->trace( message ); + } + + void debug( const std::string &message ) + { + logger_impl_->debug( message ); + } + + void info( const std::string &message ) + { + logger_impl_->info( message ); + } + + void warn( const std::string &message ) + { + logger_impl_->warn( message ); + } + + void error( const std::string &message ) + { + logger_impl_->error( message ); + } + + void critical( const std::string &message ) + { + logger_impl_->critical( message ); + } + + private: + std::shared_ptr< spdlog::logger > logger_impl_; + }; + + FileLoggerClient::FileLoggerClient( std::string_view file_path ) + : impl_{ file_path } + { + } + + FileLoggerClient::~FileLoggerClient() = default; + + void FileLoggerClient::trace( const std::string &message ) + { + impl_->trace( message ); + } + + void FileLoggerClient::debug( const std::string &message ) + { + impl_->debug( message ); + } + + void FileLoggerClient::info( const std::string &message ) + { + impl_->info( message ); + } + + void FileLoggerClient::warn( const std::string &message ) + { + impl_->warn( message ); + } + + void FileLoggerClient::error( const std::string &message ) + { + impl_->error( message ); + } + + void FileLoggerClient::critical( const std::string &message ) + { + impl_->critical( message ); + } +} // namespace geode diff --git a/tests/basic/test-logger.cpp b/tests/basic/test-logger.cpp index d413fa53c..2a5e5fefe 100644 --- a/tests/basic/test-logger.cpp +++ b/tests/basic/test-logger.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -79,6 +80,8 @@ void test() { geode::OpenGeodeBasicLibrary::initialize(); geode::LoggerManager::register_client( std::make_unique< CustomClient >() ); + geode::LoggerManager::register_client( + std::make_unique< geode::FileLoggerClient >( "geode.log" ) ); test_logger(); geode::Logger::set_level( geode::Logger::LEVEL::err );