From ae5692a675b52fa8ef2998a1d0a27023cacaf5a6 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Tue, 8 Apr 2025 16:16:57 +0200 Subject: [PATCH] Implementing DebugUtils as a real singleton --- src/Arduino_DebugUtils.cpp | 9 ++++++--- src/Arduino_DebugUtils.h | 16 ++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Arduino_DebugUtils.cpp b/src/Arduino_DebugUtils.cpp index 0d9ee4b..cb4f648 100644 --- a/src/Arduino_DebugUtils.cpp +++ b/src/Arduino_DebugUtils.cpp @@ -247,12 +247,15 @@ bool Arduino_DebugUtils::shouldPrint(Arduino_DebugUtils::Level const debug_level CLASS INSTANTIATION ******************************************************************************/ -Arduino_DebugUtils Debug; +Arduino_DebugUtils& Arduino_DebugUtils::getInstance() { + static Arduino_DebugUtils instance; + return instance; +} void setDebugMessageLevel(Arduino_DebugUtils::Level const debug_level) { - Debug.setDebugLevel(debug_level); + Arduino_DebugUtils::getInstance().setDebugLevel(debug_level); } Arduino_DebugUtils::Level getDebugMessageLevel() { - return Debug.getDebugLevel(); + return Arduino_DebugUtils::getInstance().getDebugLevel(); } diff --git a/src/Arduino_DebugUtils.h b/src/Arduino_DebugUtils.h index cb1e633..337155a 100644 --- a/src/Arduino_DebugUtils.h +++ b/src/Arduino_DebugUtils.h @@ -73,7 +73,7 @@ class Arduino_DebugUtils { void print(Level const debug_level, const char * fmt, ...); void print(Level const debug_level, const __FlashStringHelper * fmt, ...); - + static Arduino_DebugUtils& getInstance(); private: bool _timestamp_on; @@ -106,10 +106,10 @@ static constexpr Arduino_DebugUtils::Level DBG_VERBOSE = Arduino_DebugUtils::Lev static constexpr Arduino_DebugUtils::Level DBG_ALL = Arduino_DebugUtils::Level::All; /****************************************************************************** - EXTERN + Global reference ******************************************************************************/ -extern Arduino_DebugUtils Debug; +inline Arduino_DebugUtils& Debug = Arduino_DebugUtils::getInstance(); /************************************************************************************** * DEFINE @@ -121,31 +121,31 @@ extern Arduino_DebugUtils Debug; #endif #if !defined(DEBUG_ERROR) && ((DEBUG_LEVEL & DEBUG_LEVEL_ERROR) == DEBUG_LEVEL_ERROR) -# define DEBUG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) +# define DEBUG_ERROR(fmt, ...) Arduino_DebugUtils::getInstance().print(DBG_ERROR, fmt, ## __VA_ARGS__) #else # define DEBUG_ERROR(fmt, ...) (void) 0 #endif #if !defined(DEBUG_WARNING) && ((DEBUG_LEVEL & DEBUG_LEVEL_WARNING) == DEBUG_LEVEL_WARNING) -# define DEBUG_WARNING(fmt, ...) Debug.print(DBG_WARNING, fmt, ## __VA_ARGS__) +# define DEBUG_WARNING(fmt, ...) Arduino_DebugUtils::getInstance().print(DBG_WARNING, fmt, ## __VA_ARGS__) #else # define DEBUG_WARNING(fmt, ...) (void) 0 #endif #if !defined(DEBUG_INFO) && ((DEBUG_LEVEL & DEBUG_LEVEL_INFO) == DEBUG_LEVEL_INFO) -# define DEBUG_INFO(fmt, ...) Debug.print(DBG_INFO, fmt, ## __VA_ARGS__) +# define DEBUG_INFO(fmt, ...) Arduino_DebugUtils::getInstance().print(DBG_INFO, fmt, ## __VA_ARGS__) #else # define DEBUG_INFO(fmt, ...) (void) 0 #endif #if !defined(DEBUG_DEBUG) && ((DEBUG_LEVEL & DEBUG_LEVEL_DEBUG) == DEBUG_LEVEL_DEBUG) -# define DEBUG_DEBUG(fmt, ...) Debug.print(DBG_DEBUG, fmt, ## __VA_ARGS__) +# define DEBUG_DEBUG(fmt, ...) Arduino_DebugUtils::getInstance().print(DBG_DEBUG, fmt, ## __VA_ARGS__) #else # define DEBUG_DEBUG(fmt, ...) (void) 0 #endif #if !defined(DEBUG_VERBOSE) && ((DEBUG_LEVEL & DEBUG_LEVEL_VERBOSE) == DEBUG_LEVEL_VERBOSE) -# define DEBUG_VERBOSE(fmt, ...) Debug.print(DBG_VERBOSE, fmt, ## __VA_ARGS__) +# define DEBUG_VERBOSE(fmt, ...) Arduino_DebugUtils::getInstance().print(DBG_VERBOSE, fmt, ## __VA_ARGS__) #else # define DEBUG_VERBOSE(fmt, ...) (void) 0 #endif