Skip to content

Commit

Permalink
Fix debug logs going missing if printed early on during fb2k startup
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesh committed Aug 11, 2024
1 parent 7acf59d commit f3b2f78
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
21 changes: 16 additions & 5 deletions src/logging.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
#include "stdafx.h"

#include "logging.h"
#include "preferences.h"

static bool g_config_read_complete = false;

// We use this instead of console::printf because that function only supports a small subset of
// format specifiers. In particular it doesn't support 64-bit integers or floats.
void openlyrics_logging::printf(const char* fmt, ...)
void openlyrics_logging::printf(openlyrics_logging::Level lvl, const char* fmt, ...)
{
char short_buffer[512];
// Only skip printing debug logs if we're finished reading config, because if we haven't
// read config in yet then we can't really tell if debug logs have been enabled in config.
if(g_config_read_complete && !preferences::display::debug_logs_enabled() && (lvl == Level::Info))
{
return;
}

va_list varargs;
va_start(varargs, fmt);
char short_buffer[512];
va_list varargs;
va_start(varargs, fmt);
const int required_bytes = vsnprintf(short_buffer, sizeof(short_buffer), fmt, varargs);
va_end(varargs);
va_end(varargs);

// While unlikely, its possible that our short buffer wasn't big enough for this log message.
// In that case use the result that we have to allocate a large enough buffer and print that.
Expand All @@ -31,3 +40,5 @@ void openlyrics_logging::printf(const char* fmt, ...)
delete[] long_buffer;
}
}

FB2K_ON_INIT_STAGE([]() { g_config_read_complete = true; }, init_stages::after_config_read)
17 changes: 11 additions & 6 deletions src/logging.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include "preferences.h"

// NOTE: These macro expand to *two* statements:
// The first declares the callstack tracker (which adds an entry to the "Call path" at the top of the crash dump text file).
// The second is the actual log.
Expand All @@ -16,11 +14,18 @@
#define LOG_INFO_TRACKER_CONCAT(prefix, suffix) prefix##suffix
#define LOG_INFO_TRACKER_NAME(id) LOG_INFO_TRACKER_CONCAT(TRACKER_, id)

#define LOG_ERROR(MSG, ...) uCallStackTracker LOG_INFO_TRACKER_NAME(__LINE__)(#MSG); do { openlyrics_logging::printf("ERROR-OpenLyrics: " MSG, ##__VA_ARGS__); }while(false)
#define LOG_WARN(MSG, ...) uCallStackTracker LOG_INFO_TRACKER_NAME(__LINE__)(#MSG); do { openlyrics_logging::printf("WARN-OpenLyrics: " MSG, ##__VA_ARGS__); }while(false)
#define LOG_INFO(MSG, ...) uCallStackTracker LOG_INFO_TRACKER_NAME(__LINE__)(#MSG); do { if(preferences::display::debug_logs_enabled()) { openlyrics_logging::printf("INFO-OpenLyrics: " MSG, ##__VA_ARGS__); }}while(false)
#define LOG_ERROR(MSG, ...) uCallStackTracker LOG_INFO_TRACKER_NAME(__LINE__)(#MSG); do { openlyrics_logging::printf(openlyrics_logging::Level::Error, "ERROR-OpenLyrics: " MSG, ##__VA_ARGS__); }while(false)
#define LOG_WARN(MSG, ...) uCallStackTracker LOG_INFO_TRACKER_NAME(__LINE__)(#MSG); do { openlyrics_logging::printf(openlyrics_logging::Level::Warn, "WARN-OpenLyrics: " MSG, ##__VA_ARGS__); }while(false)
#define LOG_INFO(MSG, ...) uCallStackTracker LOG_INFO_TRACKER_NAME(__LINE__)(#MSG); do { openlyrics_logging::printf(openlyrics_logging::Level::Info, "INFO-OpenLyrics: " MSG, ##__VA_ARGS__); }while(false)

namespace openlyrics_logging
{
void printf(const char* fmt, ...);
enum class Level
{
Info,
Warn,
Error
};

void printf(Level lvl, const char* fmt, ...);
}
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void OpenLyricsVersion::get_about_message(pfc::string_base & out)
"- Fix rounding errors causing timestamps to randomly change when saving\n"
"- Fix LRC same-line collapsing sometimes deleting parts of a line\n"
"- Fix search avoidance not working for remote/internet radio tracks\n"
"- Fix debug logs going missing if printed early on during fb2k startup\n"
"- Many, many minor non-functional internal code improvements\n"
"\n";
out += "Version 1.9 (2024-06-12):\n"
Expand Down

0 comments on commit f3b2f78

Please sign in to comment.