diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-09-29 16:15:49 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-09-29 16:15:49 +0200 |
commit | feea4cbb648d67e46b413880ddbf203c88c2a2b1 (patch) | |
tree | 7c3b6bfddfef49e42b7f64256e3c027c43744f8e | |
parent | 3cb7227c3c9678141ff74915331b706265c380cb (diff) |
implement debug logging functions and fix sound system segfault
-rw-r--r-- | src/crepe/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/Sound.cpp | 17 | ||||
-rw-r--r-- | src/crepe/Sound.h | 3 | ||||
-rw-r--r-- | src/crepe/SoundSystem.cpp | 11 | ||||
-rw-r--r-- | src/crepe/SoundSystem.h | 8 | ||||
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/crepe/util/color.h | 42 | ||||
-rw-r--r-- | src/crepe/util/debug.cpp | 0 | ||||
-rw-r--r-- | src/crepe/util/debug.h | 0 | ||||
-rw-r--r-- | src/crepe/util/log.cpp | 51 | ||||
-rw-r--r-- | src/crepe/util/log.h | 33 | ||||
-rw-r--r-- | src/dummy_audio.cpp | 3 |
12 files changed, 167 insertions, 11 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 13d9be5..9f7c91c 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -9,4 +9,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ) add_subdirectory(api) +add_subdirectory(util) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index d48393c..94f97af 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -1,24 +1,29 @@ +#include "util/log.h" + #include "Sound.h" #include "SoundSystem.h" using namespace crepe; -Sound::Sound(std::unique_ptr<api::Resource> res, SoundSystem & system) : system(system) { +Sound::Sound(std::unique_ptr<api::Resource> res) { + dbg_trace(); this->res = std::move(res); } void Sound::play() { - if (this->system.engine.getPause(this->handle)) { + SoundSystem & system = SoundSystem::instance(); + if (system.engine.getPause(this->handle)) { // resume if paused - this->system.engine.setPause(this->handle, false); + system.engine.setPause(this->handle, false); } else { // or start new sound - this->handle = this->system.engine.play(this->sample); + this->handle = system.engine.play(this->sample); } } void Sound::pause() { - if (this->system.engine.getPause(this->handle)) return; - this->system.engine.setPause(this->handle, true); + SoundSystem & system = SoundSystem::instance(); + if (system.engine.getPause(this->handle)) return; + system.engine.setPause(this->handle, true); } diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index 71fe390..333465b 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -55,9 +55,8 @@ public: void set_looping(bool looping); private: - Sound(std::unique_ptr<api::Resource> res, SoundSystem & system); - SoundSystem & system; friend class SoundSystem; + Sound(std::unique_ptr<api::Resource> res); private: std::unique_ptr<api::Resource> res; diff --git a/src/crepe/SoundSystem.cpp b/src/crepe/SoundSystem.cpp index 30b0157..29bd196 100644 --- a/src/crepe/SoundSystem.cpp +++ b/src/crepe/SoundSystem.cpp @@ -1,9 +1,14 @@ +#include "util/log.h" + #include "SoundSystem.h" #include <memory> using namespace crepe; -SoundSystem SoundSystem::instance { }; +SoundSystem & SoundSystem::instance() { + static SoundSystem instance; + return instance; +} std::unique_ptr<Sound> SoundSystem::sound(const std::string & src) { auto res = std::make_unique<api::Resource>(src); @@ -11,15 +16,17 @@ std::unique_ptr<Sound> SoundSystem::sound(const std::string & src) { } std::unique_ptr<Sound> SoundSystem::sound(std::unique_ptr<api::Resource> res) { - Sound * out = new Sound(std::move(res), SoundSystem::instance); + Sound * out = new Sound(std::move(res)); return std::unique_ptr<Sound>(out); } SoundSystem::SoundSystem() { + dbg_trace(); engine.init(); } SoundSystem::~SoundSystem() { + dbg_trace(); engine.deinit(); } diff --git a/src/crepe/SoundSystem.h b/src/crepe/SoundSystem.h index 23bb00a..515cb29 100644 --- a/src/crepe/SoundSystem.h +++ b/src/crepe/SoundSystem.h @@ -16,7 +16,13 @@ public: private: SoundSystem(); virtual ~SoundSystem(); - static SoundSystem instance; + + // singleton + static SoundSystem & instance(); + SoundSystem(const SoundSystem &) = delete; + SoundSystem(SoundSystem &&) = delete; + SoundSystem &operator=(const SoundSystem &) = delete; + SoundSystem &operator=(SoundSystem &&) = delete; private: SoLoud::Soloud engine; diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index e69de29..100f028 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -0,0 +1,9 @@ +target_sources(crepe PUBLIC + log.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + color.h + log.h +) + diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h new file mode 100644 index 0000000..1af6c8f --- /dev/null +++ b/src/crepe/util/color.h @@ -0,0 +1,42 @@ +#pragma once + +namespace crepe::util::color { + +constexpr const char * RESET = "\e[0m"; + +constexpr const char * FG_BLACK = "\e[30m"; +constexpr const char * FG_RED = "\e[31m"; +constexpr const char * FG_GREEN = "\e[32m"; +constexpr const char * FG_YELLOW = "\e[33m"; +constexpr const char * FG_BLUE = "\e[34m"; +constexpr const char * FG_MAGENTA = "\e[35m"; +constexpr const char * FG_CYAN = "\e[36m"; +constexpr const char * FG_WHITE = "\e[37m"; +constexpr const char * BG_BLACK = "\e[40m"; +constexpr const char * BG_RED = "\e[41m"; +constexpr const char * BG_GREEN = "\e[42m"; +constexpr const char * BG_YELLOW = "\e[43m"; +constexpr const char * BG_BLUE = "\e[44m"; +constexpr const char * BG_MAGENTA = "\e[45m"; +constexpr const char * BG_CYAN = "\e[46m"; +constexpr const char * BG_WHITE = "\e[47m"; + +constexpr const char * FG_BLACK_BRIGHT = "\e[90m"; +constexpr const char * FG_RED_BRIGHT = "\e[91m"; +constexpr const char * FG_GREEN_BRIGHT = "\e[92m"; +constexpr const char * FG_YELLOW_BRIGHT = "\e[93m"; +constexpr const char * FG_BLUE_BRIGHT = "\e[94m"; +constexpr const char * FG_MAGENTA_BRIGHT = "\e[95m"; +constexpr const char * FG_CYAN_BRIGHT = "\e[96m"; +constexpr const char * FG_WHITE_BRIGHT = "\e[97m"; +constexpr const char * BG_BLACK_BRIGHT = "\e[100m"; +constexpr const char * BG_RED_BRIGHT = "\e[101m"; +constexpr const char * BG_GREEN_BRIGHT = "\e[102m"; +constexpr const char * BG_YELLOW_BRIGHT = "\e[103m"; +constexpr const char * BG_BLUE_BRIGHT = "\e[104m"; +constexpr const char * BG_MAGENTA_BRIGHT = "\e[105m"; +constexpr const char * BG_CYAN_BRIGHT = "\e[106m"; +constexpr const char * BG_WHITE_BRIGHT = "\e[107m"; + +} + diff --git a/src/crepe/util/debug.cpp b/src/crepe/util/debug.cpp deleted file mode 100644 index e69de29..0000000 --- a/src/crepe/util/debug.cpp +++ /dev/null diff --git a/src/crepe/util/debug.h b/src/crepe/util/debug.h deleted file mode 100644 index e69de29..0000000 --- a/src/crepe/util/debug.h +++ /dev/null diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp new file mode 100644 index 0000000..796df49 --- /dev/null +++ b/src/crepe/util/log.cpp @@ -0,0 +1,51 @@ +#include <cstdarg> +#include <cstdio> +#include <cstdlib> +#include <string> + +#include "log.h" + +using namespace crepe::util; + +static const char * const LOG_PREFIX[] = { + [log_level::debug] = "[DBG] ", + [log_level::info] = "[INFO] ", + [log_level::warning] = "[WARN] ", + [log_level::error] = "[ERR] ", +}; + +static void va_logf(enum log_level level, va_list args, const std::string fmt) { + va_list args_copy; + va_copy(args_copy, args); + + // prepend log level and ensure newline + std::string format_fixed = LOG_PREFIX[level] + fmt; + if (!format_fixed.ends_with("\n")) format_fixed += "\n"; + + size_t sz = vsnprintf(NULL, 0, format_fixed.c_str(), args_copy) + 1; + char * msg = (char *) malloc(sz); + va_end(args_copy); + + vsnprintf(msg, sz, format_fixed.c_str(), args); + + // TODO: also log to file or smth + printf("%s", msg); + fflush(stdout); + + free(msg); +} + +void crepe::util::logf(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + va_logf(crepe::util::log_level::debug, args, fmt); + va_end(args); +} + +void crepe::util::logf(log_level level, const char * fmt, ...) { + va_list args; + va_start(args, fmt); + va_logf(level, args, fmt); + va_end(args); +} + diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h new file mode 100644 index 0000000..5295cb9 --- /dev/null +++ b/src/crepe/util/log.h @@ -0,0 +1,33 @@ +#pragma once + +// allow user to disable debug macros +#ifndef CREPE_DISABLE_MACROS + +#include "color.h" + +// utility macros +#define _crepe_logf_here(fmt, ...) \ + crepe::util::logf(util::log_level::debug, "%s%s (%s:%d)" fmt "\n", \ + crepe::util::color::FG_WHITE, \ + __PRETTY_FUNCTION__, \ + __FILE_NAME__, \ + __LINE__, \ + crepe::util::color::RESET, \ + __VA_ARGS__) + +#define dbg_logf(fmt, ...) _crepe_logf_here(": " fmt, __VA_ARGS__) +#define dbg_log(str) _crepe_logf_here(": %s", str) +#define dbg_trace() _crepe_logf_here("%s", "") + +#endif + +namespace crepe::util { + +enum log_level { debug, info, warning, error, }; + +void logf(const char * fmt, ...); +void logf(enum log_level level, const char * fmt, ...); + +} + + diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp index 5e0000e..fb1e3ab 100644 --- a/src/dummy_audio.cpp +++ b/src/dummy_audio.cpp @@ -1,4 +1,5 @@ #include "crepe/SoundSystem.h" +#include "crepe/util/log.h" #include <chrono> #include <thread> @@ -8,6 +9,8 @@ using namespace std; using namespace std::chrono_literals; int main() { + dbg_trace(); + auto bgm = SoundSystem::sound("../mwe/audio/bgm.ogg"); auto sfx1 = SoundSystem::sound("../mwe/audio/sfx1.wav"); auto sfx2 = SoundSystem::sound("../mwe/audio/sfx2.wav"); |