Skip to content

Make use of Singleton design pattern #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: logging-improvements
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Arduino_DebugUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
16 changes: 8 additions & 8 deletions src/Arduino_DebugUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down