Files
spice2x.github.io/src/spice2x/util/deferlog.cpp
T
bicarus-dev cffee2b7f8 logger: add mutex to deferred logger (#391)
## Link to GitHub Issue, if one exists
[#390 ](https://github.com/spice2x/spice2x.github.io/pull/390)

## Description of change
Just like the normal logger, deferred logging can be called from any
component at any time, so accessing the vector needs to be guarded.

## Testing
2025-10-02 23:38:11 -07:00

48 lines
1.7 KiB
C++

#include <mutex>
#include "deferlog.h"
#include "util/logging.h"
namespace deferredlogs {
const std::initializer_list<std::string> SUPERSTEP_SOUND_ERROR_MESSAGE = {
"audio initialization error was previously detected during boot!",
" (W:SuperstepSound: Audio device is not available!!!)",
" this crash is most likely related to audio init failure",
" see if the default audio device changed, fix your audio configuration (e.g., sample rate)",
" double check your spice audio options/patches, and try again"
};
std::mutex deferred_errors_mutex;
std::vector<std::vector<std::string>> deferred_errors;
void defer_error_messages(std::initializer_list<std::string> messages) {
std::unique_lock<std::mutex> lock(deferred_errors_mutex);
deferred_errors.emplace_back(messages);
}
void dump_to_logger() {
static std::once_flag printed;
std::call_once(printed, []() {
std::unique_lock<std::mutex> lock(deferred_errors_mutex);
if (!deferred_errors.empty()) {
log_warning("deferred_error",
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
log_warning("deferred_error", "");
}
for (auto messages : deferred_errors) {
for (auto message : messages) {
log_warning("deferred_error", "{}", message);
}
log_warning("deferred_error", "");
}
if (!deferred_errors.empty()) {
log_warning("deferred_error",
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
});
}
}