diff --git a/src/logging.cpp b/src/logging.cpp index a1b5de2..30059f7 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -16,11 +16,9 @@ #define SP_LOGGING_FALLBACK_STDIO 0 #endif -#if SP_LOGGING_FALLBACK_STDIO #include -#else #include -#endif + namespace { @@ -34,28 +32,31 @@ namespace "[CRITICAL]: " }; - void sdlCallback(void* userdata, int /*category*/, SDL_LogPriority priority, const char* message) + void stdioCallback(void* userdata, int /*category*/, SDL_LogPriority priority, const char* message) { -#if SP_LOGGING_FALLBACK_STDIO auto stream = static_cast(userdata); auto write = [stream](const void* buffer, size_t size, size_t num) { return fwrite(buffer, size, num, stream); }; -#else + const auto& label = priority_labels[priority]; + write(label.data(), label.size(), 1); + write(message, SDL_strlen(message), 1); + write("\n", 1, 1); + fflush(stream); + } + + void sdlCallback(void* userdata, int /*category*/, SDL_LogPriority priority, const char* message) + { auto stream = static_cast(userdata); auto write = [stream](const void* buffer, size_t size, size_t num) { return SDL_RWwrite(stream, buffer, size, num); }; -#endif const auto& label = priority_labels[priority]; write(label.data(), label.size(), 1); write(message, SDL_strlen(message), 1); write("\n", 1, 1); -#if SP_LOGGING_FALLBACK_STDIO - fflush(stream); -#endif } constexpr SDL_LogPriority asSDLPriority(ELogLevel level) @@ -128,25 +129,35 @@ void Logging::setLogLevel(ELogLevel level) void Logging::setLogFile(std::string_view filename) { - SDL_LogOutputFunction current = nullptr; - void* current_data = nullptr; - SDL_LogGetOutputFunction(¤t, ¤t_data); - if (current == &sdlCallback) - { -#if SP_LOGGING_FALLBACK_STDIO - auto stream = static_cast(current_data); - fclose(stream); -#else - auto stream = static_cast(current_data); - SDL_RWclose(stream); -#endif - } + closeCurrentLogStream(); #if SP_LOGGING_FALLBACK_STDIO auto handle = fopen(filename.data(), "wt"); + SDL_LogSetOutputFunction(&stdioCallback, handle); #else auto handle = SDL_RWFromFile(filename.data(), "wt"); + SDL_LogSetOutputFunction(&sdlCallback, handle); #endif +} - SDL_LogSetOutputFunction(&sdlCallback, handle); +void Logging::setLogStdout() +{ + closeCurrentLogStream(); + SDL_LogSetOutputFunction(&stdioCallback, stdout); } + +void Logging::closeCurrentLogStream() +{ + SDL_LogOutputFunction current = nullptr; + void* current_data = nullptr; + SDL_LogGetOutputFunction(¤t, ¤t_data); + if (current == &stdioCallback) { + auto stream = static_cast(current_data); + if (stream != stdout) + fclose(stream); + } + if (current == &sdlCallback) { + auto stream = static_cast(current_data); + SDL_RWclose(stream); + } +} \ No newline at end of file diff --git a/src/logging.h b/src/logging.h index 0467220..ab3181b 100644 --- a/src/logging.h +++ b/src/logging.h @@ -43,8 +43,12 @@ class Logging : sp::NonCopyable static void setLogLevel(ELogLevel level); static void setLogFile(std::string_view filename); + static void setLogStdout(); friend const Logging& operator<<(const Logging& log, const char* str); + +private: + static void closeCurrentLogStream(); }; const Logging& operator<<(const Logging& log, const char* str);