Skip to content

Commit

Permalink
feat: forward logging from miniaudio (#4836)
Browse files Browse the repository at this point in the history
These logs are posted to the `chatterino.sound` category
miniaudio debug -> debug
miniaudio info -> debug
miniaudio warning -> warning
miniaudio error -> warning
  • Loading branch information
pajlada authored Nov 5, 2023
1 parent 7d145e3 commit 1d6c9ed
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion src/controllers/sound/SoundController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,48 @@
#include <limits>
#include <memory>

namespace {

using namespace chatterino;

void miniaudioLogCallback(void *userData, ma_uint32 level, const char *pMessage)
{
(void)userData;

QString message{pMessage};

switch (level)
{
case MA_LOG_LEVEL_DEBUG: {
qCDebug(chatterinoSound).noquote()
<< "ma debug: " << message.trimmed();
}
break;
case MA_LOG_LEVEL_INFO: {
qCDebug(chatterinoSound).noquote()
<< "ma info: " << message.trimmed();
}
break;
case MA_LOG_LEVEL_WARNING: {
qCWarning(chatterinoSound).noquote()
<< "ma warning:" << message.trimmed();
}
break;
case MA_LOG_LEVEL_ERROR: {
qCWarning(chatterinoSound).noquote()
<< "ma error: " << message.trimmed();
}
break;
default: {
qCWarning(chatterinoSound).noquote()
<< "ma unknown:" << message.trimmed();
}
break;
}
}

} // namespace

namespace chatterino {

// NUM_SOUNDS specifies how many simultaneous default ping sounds & decoders to create
Expand All @@ -31,8 +73,30 @@ void SoundController::initialize(Settings &settings, Paths &paths)

ma_result result{};

// We are leaking this log object on purpose
auto *logger = new ma_log;

result = ma_log_init(nullptr, logger);
if (result != MA_SUCCESS)
{
qCWarning(chatterinoSound) << "Error initializing logger:" << result;
return;
}

result = ma_log_register_callback(
logger, ma_log_callback_init(miniaudioLogCallback, nullptr));
if (result != MA_SUCCESS)
{
qCWarning(chatterinoSound)
<< "Error registering logger callback:" << result;
return;
}

auto contextConfig = ma_context_config_init();
contextConfig.pLog = logger;

/// Initialize context
result = ma_context_init(nullptr, 0, nullptr, this->context.get());
result = ma_context_init(nullptr, 0, &contextConfig, this->context.get());
if (result != MA_SUCCESS)
{
qCWarning(chatterinoSound) << "Error initializing context:" << result;
Expand Down

0 comments on commit 1d6c9ed

Please sign in to comment.