Skip to content

Commit

Permalink
fix(logging): Use native string formatting functions.
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Nana <[email protected]>
  • Loading branch information
na2axl committed Jun 23, 2024
1 parent fd5eea6 commit ef918fb
Show file tree
Hide file tree
Showing 25 changed files with 121 additions and 111 deletions.
11 changes: 10 additions & 1 deletion include/SparkyStudios/Audio/Amplitude/Core/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
*/
#define amLogger SparkyStudios::Audio::Amplitude::Logger::GetLogger()

#if defined(AM_WCHAR_SUPPORTED)
#define amLogFormat std::snwprintf
#else
#define amLogFormat std::snprintf
#endif

/**
* @brief Logs a message with the given level.
*
Expand All @@ -34,7 +40,10 @@
#define amLog(_level_, _message_, ...) \
if (amLogger != nullptr) \
{ \
amLogger->_level_(__FILE__, __LINE__, std::format((_message_), ##__VA_ARGS__)); \
constexpr size_t bufferLen = 2048; \
char buffer[bufferLen]; \
int formatted = amLogFormat(buffer, bufferLen, _message_, ##__VA_ARGS__); \
amLogger->_level_(__FILE__, __LINE__, std::string(buffer).substr(0, formatted)); \
} \
(void)0

Expand Down
6 changes: 3 additions & 3 deletions plugins/codec-flac/Codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void FlacCodec::FlacDecoderInternal::metadata_callback(const ::FLAC__StreamMetad

void FlacCodec::FlacDecoderInternal::error_callback(::FLAC__StreamDecoderErrorStatus status)
{
amLogError("Got error callback: {}", FLAC__StreamDecoderErrorStatusString[status]);
amLogError("Got error callback: %s", FLAC__StreamDecoderErrorStatusString[status]);
}

::FLAC__StreamDecoderReadStatus FlacCodec::FlacDecoderInternal::read_callback(FLAC__byte buffer[], size_t* bytes)
Expand Down Expand Up @@ -123,7 +123,7 @@ bool FlacCodec::FlacDecoder::Open(std::shared_ptr<File> file)
if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
{
_file.reset();
amLogError("Initializing FLAC decoder: {}", FLAC__StreamDecoderInitStatusString[init_status]);
amLogError("Initializing FLAC decoder: %s", FLAC__StreamDecoderInitStatusString[init_status]);
return false;
}

Expand All @@ -133,7 +133,7 @@ bool FlacCodec::FlacDecoder::Open(std::shared_ptr<File> file)
if (!_flac.process_until_end_of_metadata())
{
_file.reset();
amLogError("Unable to read metadata for FLAC file: {}", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("Unable to read metadata for FLAC file: " AM_OS_CHAR_FMT, file->GetPath().c_str());
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/codec-vorbis/Codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool VorbisCodec::VorbisDecoder::Open(std::shared_ptr<File> file)
{
if (!m_codec->CanHandleFile(file))
{
amLogError("The Vorbis codec cannot handle the file: '{}'.", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("The Vorbis codec cannot handle the file: '" AM_OS_CHAR_FMT "'.", file->GetPath().c_str());
return false;
}

Expand All @@ -58,7 +58,7 @@ bool VorbisCodec::VorbisDecoder::Open(std::shared_ptr<File> file)
if (ov_open_callbacks(_file.get(), &_vorbis, nullptr, 0, OV_CALLBACKS) < 0)
{
_file.reset();
amLogError("Unable to open the file: '{}'.", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("Unable to open the file: '" AM_OS_CHAR_FMT "'.", file->GetPath().c_str());
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion samples/sample_01/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ int main(int argc, char* argv[])
#if !defined(AM_NO_MEMORY_STATS)
printMemoryStats();

amLogInfo("{}", amMemory->InspectMemoryLeaks());
amLogInfo("%s", amMemory->InspectMemoryLeaks().c_str());
#endif

MemoryManager::Deinitialize();
Expand Down
12 changes: 6 additions & 6 deletions samples/sample_02/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ static void device_notification(DeviceNotification notification, const DeviceDes
switch (notification)
{
case DeviceNotification::Opened:
amLogInfo("Device opened: {}", device.mDeviceName);
amLogInfo("Device opened: %s", device.mDeviceName.c_str());
break;
case DeviceNotification::Started:
amLogInfo("Device started: {}", device.mDeviceName);
amLogInfo("Device started: %s", device.mDeviceName.c_str());
break;
case DeviceNotification::Stopped:
amLogInfo("Device stopped: {}", device.mDeviceName);
amLogInfo("Device stopped: %s", device.mDeviceName.c_str());
break;
case DeviceNotification::Rerouted:
amLogInfo("Device rerouted: {}", device.mDeviceName);
amLogInfo("Device rerouted: %s", device.mDeviceName.c_str());
break;
case DeviceNotification::Closed:
amLogInfo("Device closed: {}", device.mDeviceName);
amLogInfo("Device closed: %s", device.mDeviceName.c_str());
break;
}
}
Expand Down Expand Up @@ -531,7 +531,7 @@ int main(int argc, char* argv[])
Engine::UnregisterDefaultPlugins();

#if !defined(AM_NO_MEMORY_STATS)
amLogInfo("{}", amMemory->InspectMemoryLeaks());
amLogInfo("%s", amMemory->InspectMemoryLeaks().c_str());
#endif

MemoryManager::Deinitialize();
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace SparkyStudios::Audio::Amplitude

if (Find(codec->GetName()) != nullptr)
{
amLogWarning("Failed to register codec '{}' as it is already registered", codec->GetName());
amLogWarning("Failed to register codec '%s' as it is already registered", codec->GetName().c_str());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Core/Codecs/AMS/Codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ namespace SparkyStudios::Audio::Amplitude

if (!ReadHeader(_file, m_format, _blockSize))
{
amLogError("The AMS codec cannot handle the file: '{}'", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("The AMS codec cannot handle the file: '" AM_OS_CHAR_FMT "'", AM_OS_STRING_TO_STRING(file->GetPath()));
return false;
}

Expand Down Expand Up @@ -598,7 +598,7 @@ namespace SparkyStudios::Audio::Amplitude

if (!WriteHeader(_file, m_format, _samplesPerBlock))
{
amLogError("The AMS codec was unable to write the file: '{}'", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("The AMS codec was unable to write the file: '" AM_OS_CHAR_FMT "'", AM_OS_STRING_TO_STRING(file->GetPath()));
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Core/Codecs/MP3/Codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace SparkyStudios::Audio::Amplitude
{
if (!m_codec->CanHandleFile(file))
{
amLogError("The MP3 codec cannot handle the file: '{}'", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("The MP3 codec cannot handle the file: '" AM_OS_CHAR_FMT "'", file->GetPath().c_str());
return false;
}

Expand All @@ -73,14 +73,14 @@ namespace SparkyStudios::Audio::Amplitude

if (drmp3_init(&_mp3, onRead, onSeek, _file.get(), &codec->m_allocationCallbacks) == DRMP3_FALSE)
{
amLogError("Cannot load the MP3 file: '{}'", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("Cannot load the MP3 file: '" AM_OS_CHAR_FMT "'", file->GetPath().c_str());
return false;
}

const drmp3_uint64 framesCount = drmp3_get_pcm_frame_count(&_mp3);
if (framesCount == DRMP3_FALSE)
{
amLogError("Cannot load the MP3 file: '{}'.", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("Cannot load the MP3 file: '" AM_OS_CHAR_FMT "'.", file->GetPath().c_str());
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Core/Codecs/WAV/Codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace SparkyStudios::Audio::Amplitude
{
if (!m_codec->CanHandleFile(file))
{
amLogError("The WAV codec cannot handle the file: '{}'.", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("The WAV codec cannot handle the file: '" AM_OS_CHAR_FMT "'.", file->GetPath().c_str());
return false;
}

Expand All @@ -79,7 +79,7 @@ namespace SparkyStudios::Audio::Amplitude

if (drwav_init(&_wav, onRead, onSeek, _file.get(), &codec->m_allocationCallbacks) == DRWAV_FALSE)
{
amLogError("Cannot load the WAV file: '{}'.", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("Cannot load the WAV file: '" AM_OS_CHAR_FMT "'.", file->GetPath().c_str());
return false;
}

Expand Down Expand Up @@ -141,8 +141,8 @@ namespace SparkyStudios::Audio::Amplitude
if (!_isFormatSet)
{
amLogError(
"The WAV codec cannot open the file '{}' without a format set. Have you missed to call SetFormat()?",
AM_OS_STRING_TO_STRING(file->GetPath()));
"The WAV codec cannot open the file '" AM_OS_CHAR_FMT "' without a format set. Have you missed to call SetFormat()?",
file->GetPath().c_str());
return false;
}

Expand All @@ -159,7 +159,7 @@ namespace SparkyStudios::Audio::Amplitude
if (drwav_init_write_sequential_pcm_frames(
&_wav, &format, m_format.GetFramesCount(), onWrite, _file.get(), &codec->m_allocationCallbacks) == DRWAV_FALSE)
{
amLogError("Cannot load the WAV file: '{}'.", AM_OS_STRING_TO_STRING(file->GetPath()));
amLogError("Cannot load the WAV file: '" AM_OS_CHAR_FMT "'.", file->GetPath().c_str());
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace SparkyStudios::Audio::Amplitude

if (Find(driver->GetName()) != nullptr)
{
amLogWarning("Failed to register driver '{}' as it is already registered.", driver->GetName());
amLogWarning("Failed to register driver '%s' as it is already registered.", driver->GetName().c_str());
return;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Core/Drivers/MiniAudio/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ namespace SparkyStudios::Audio::Amplitude
switch (level)
{
case MA_LOG_LEVEL_DEBUG:
amLogDebug("{}", pMessage);
amLogDebug("%s", pMessage);
break;
case MA_LOG_LEVEL_INFO:
amLogInfo("{}", pMessage);
amLogInfo("%s", pMessage);
break;
case MA_LOG_LEVEL_WARNING:
amLogWarning("{}", pMessage);
amLogWarning("%s", pMessage);
break;
case MA_LOG_LEVEL_ERROR:
amLogError("{}", pMessage);
amLogError("%s", pMessage);
break;
default:
amLogCritical("{}", pMessage);
amLogCritical("%s", pMessage);
break;
}
}
Expand Down
Loading

0 comments on commit ef918fb

Please sign in to comment.