Skip to content

Commit

Permalink
Allow logging to revert to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
daid committed Jun 10, 2024
1 parent 27a2428 commit 67a9565
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
#define SP_LOGGING_FALLBACK_STDIO 0
#endif

#if SP_LOGGING_FALLBACK_STDIO
#include <cstdio>
#else
#include <SDL_rwops.h>
#endif


namespace
{
Expand All @@ -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<FILE*>(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<SDL_RWops*>(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)
Expand Down Expand Up @@ -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(&current, &current_data);
if (current == &sdlCallback)
{
#if SP_LOGGING_FALLBACK_STDIO
auto stream = static_cast<FILE*>(current_data);
fclose(stream);
#else
auto stream = static_cast<SDL_RWops*>(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(&current, &current_data);
if (current == &stdioCallback) {
auto stream = static_cast<FILE*>(current_data);
if (stream != stdout)
fclose(stream);
}
if (current == &sdlCallback) {
auto stream = static_cast<SDL_RWops*>(current_data);
SDL_RWclose(stream);
}
}
4 changes: 4 additions & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

1 comment on commit 67a9565

@daid-tinyci
Copy link

@daid-tinyci daid-tinyci bot commented on 67a9565 Jun 10, 2024

Choose a reason for hiding this comment

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

TinyCI build failure:

[git fetch --tags] returned [1]:


 ! [rejected]        EE-2024.05.16 -> EE-2024.05.16  (would clobber existing tag)

Please sign in to comment.