Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions src/geode/basic/file_logger_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <geode/basic/file_logger_client.hpp>

// clang-format off
#include <spdlog/spdlog.h>

Check failure on line 27 in src/geode/basic/file_logger_client.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

src/geode/basic/file_logger_client.cpp:27:10 [clang-diagnostic-error]

'spdlog/spdlog.h' file not found

#include <spdlog/sinks/basic_file_sink.h>
// clang-format on
Expand All @@ -46,14 +46,19 @@
void always_flush()
{
logger_impl_->flush_on( spdlog::level::level_enum::trace );
always_flush_ = true;
}

void set_file_path( std::string_view file_path )
{
static constexpr auto logger_name = "geode_logger_file";
spdlog::drop( logger_name );
static constexpr auto LOGGER_NAME = "geode_logger_file";
spdlog::drop( LOGGER_NAME );

Check warning on line 55 in src/geode/basic/file_logger_client.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

src/geode/basic/file_logger_client.cpp:55:27 [readability-identifier-naming]

invalid case style for variable 'LOGGER_NAME'
logger_impl_ = spdlog::basic_logger_mt(
logger_name, std::string( file_path ) );
LOGGER_NAME, std::string( file_path ) );
if( always_flush_ )
{
always_flush();
}
}

void trace( const std::string &message )
Expand Down Expand Up @@ -88,6 +93,7 @@

private:
std::shared_ptr< spdlog::logger > logger_impl_{ nullptr };
bool always_flush_{ false };
};

FileLoggerClient::FileLoggerClient( std::string_view file_path )
Expand Down
129 changes: 88 additions & 41 deletions tests/basic/test-logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,63 +29,110 @@
#include <geode/basic/logger.hpp>
#include <geode/basic/logger_client.hpp>
#include <geode/basic/logger_manager.hpp>
#include <geode/basic/range.hpp>

#include <geode/tests/common.hpp>

class CustomClient : public geode::LoggerClient
namespace
{
public:
void trace( const std::string &message ) override
{
std::cout << "Old school logger => " << message << std::endl;
}

void debug( const std::string &message ) override
class CustomClient : public geode::LoggerClient
{
std::cout << "Old school logger => " << message << std::endl;
}
public:
void trace( const std::string &message ) override
{
std::cout << "Old school logger => " << message << '\n';
}

void info( const std::string &message ) override
{
std::cout << "Old school logger => " << message << std::endl;
}
void debug( const std::string &message ) override
{
std::cout << "Old school logger => " << message << '\n';
}

void info( const std::string &message ) override
{
std::cout << "Old school logger => " << message << '\n';
}

void warn( const std::string &message ) override
{
std::cout << "Old school logger => " << message << '\n';
}

void warn( const std::string &message ) override
void error( const std::string &message ) override
{
std::cout << "Old school logger => " << message << '\n';
}

void critical( const std::string &message ) override
{
std::cout << "Old school logger => " << message << '\n';
}
};

void test_logger()
{
std::cout << "Old school logger => " << message << std::endl;
geode::Logger::trace( "test ", "trace" );
geode::Logger::debug( "test ", "debug" );
geode::Logger::info( "test ", "info" );
geode::Logger::warn( "test ", "warn" );
geode::Logger::error( "test ", "error" );
geode::Logger::critical( "test ", "critial" );
}

void error( const std::string &message ) override
std::string test_huge_message()
{
std::cout << "Old school logger => " << message << std::endl;
std::string huge_message;
const geode::index_t MSG_SIZE{ 100000 };

Check warning on line 85 in tests/basic/test-logger.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/basic/test-logger.cpp:85:30 [readability-identifier-naming]

invalid case style for variable 'MSG_SIZE'
huge_message.reserve( MSG_SIZE );
for( const auto count : geode::Range{ MSG_SIZE } )
{
huge_message.push_back( 'A' + ( count % 26 ) );

Check warning on line 89 in tests/basic/test-logger.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/basic/test-logger.cpp:89:53 [cppcoreguidelines-avoid-magic-numbers]

26 is a magic number; consider replacing it with a named constant

Check warning on line 89 in tests/basic/test-logger.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/basic/test-logger.cpp:89:37 [bugprone-narrowing-conversions]

narrowing conversion from 'unsigned int' to signed type 'char' is implementation-defined
}
geode::Logger::info( "Huge message size = ", huge_message.size() );
geode::Logger::info( huge_message );

geode::Logger::info(
"Huge message begin = ", huge_message.substr( 0, 50 ) );

Check warning on line 95 in tests/basic/test-logger.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/basic/test-logger.cpp:95:62 [cppcoreguidelines-avoid-magic-numbers]

50 is a magic number; consider replacing it with a named constant
geode::Logger::info( "Huge message end = ",
huge_message.substr( huge_message.size() - 50 ) );

Check warning on line 97 in tests/basic/test-logger.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/basic/test-logger.cpp:97:56 [cppcoreguidelines-avoid-magic-numbers]

50 is a magic number; consider replacing it with a named constant
return huge_message;
}

void critical( const std::string &message ) override
void test_change_log_file( const std::string &huge_msg )
{
std::cout << "Old school logger => " << message << std::endl;
geode::Logger::info( "==============================" );
geode::Logger::info( "TEST CHANGE LOG FILE" );
geode::Logger::info( "==============================" );

static constexpr auto FILENAME1 = "first.log";
auto file_logger =
std::make_unique< geode::FileLoggerClient >( FILENAME1 );
auto &registered_file_logger =
dynamic_cast< geode::FileLoggerClient & >(
geode::LoggerManager::register_client(
std::move( file_logger ) ) );
geode::Logger::info(
absl::StrCat( "Message written in first.log", "\n", huge_msg ) );
static constexpr auto FILENAME2 = "second.log";
registered_file_logger.set_file_path( FILENAME2 );
geode::Logger::info(
absl::StrCat( "Message written in second.log", "\n", huge_msg ) );
}
};

void test_logger()
{
geode::Logger::trace( "test ", "trace" );
geode::Logger::debug( "test ", "debug" );
geode::Logger::info( "test ", "info" );
geode::Logger::warn( "test ", "warn" );
geode::Logger::error( "test ", "error" );
geode::Logger::critical( "test ", "critial" );
}

void test()
{
geode::OpenGeodeBasicLibrary::initialize();
geode::LoggerManager::register_client( std::make_unique< CustomClient >() );
geode::LoggerManager::register_client(
std::make_unique< geode::FileLoggerClient >( "geode.log" ) );
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 );
test_logger();
}
test_logger();
const auto &huge_msg = test_huge_message();
test_change_log_file( huge_msg );

geode::Logger::set_level( geode::Logger::LEVEL::err );
test_logger();
}
} // namespace
OPENGEODE_TEST( "logger" )
Loading