Skip to content

Commit

Permalink
Cleanup: Remove dead/commented code and minor styling
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierJF committed Sep 26, 2024
1 parent 705f50e commit 6af8b84
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 151 deletions.
69 changes: 18 additions & 51 deletions include/proxysql_debug.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@

/*
#ifdef DEBUG
#ifndef DEBUG_EXTERN
#define DEBUG_EXTERN
extern debug_level *gdbg_lvl;
extern int gdbg;
#endif
#endif
*/

#ifndef __PROXYSQL_DEBUG_H
#define __PROXYSQL_DEBUG_H

Expand Down Expand Up @@ -46,7 +35,6 @@ class Timer {

#ifdef DEBUG
#define PROXY_TRACE() { proxy_debug(PROXY_DEBUG_GENERIC,10,"TRACE\n"); }
//#define PROXY_TRACE2() { proxy_info("TRACE\n"); }
#define PROXY_TRACE2()
#else
#define PROXY_TRACE()
Expand All @@ -64,7 +52,6 @@ class Timer {
} \
} while (0)
#elif defined(__linux__)
//#ifdef SYS_gettid
#define proxy_debug(module, verbosity, fmt, ...) \
do { if (GloVars.global.gdbg) { \
proxy_debug_func(module, verbosity, syscall(SYS_gettid), __FILE__, __LINE__, __func__ , fmt, ## __VA_ARGS__); \
Expand All @@ -76,9 +63,6 @@ class Timer {
#define proxy_debug(module, verbosity, fmt, ...)
#endif /* DEBUG */

/*
#ifdef DEBUG
*/
#define proxy_error(fmt, ...) \
do { \
time_t __timer; \
Expand Down Expand Up @@ -111,23 +95,7 @@ class Timer {
strftime(__buffer, 25, "%Y-%m-%d %H:%M:%S", &__tm_info); \
proxy_error_func(0, "%s %s:%d:%s(): [ERROR] " fmt, __buffer, fi, li, fu , ## __VA_ARGS__); \
} while(0)
/*
#else
#define proxy_error(fmt, ...) \
do { \
time_t __timer; \
char __buffer[25]; \
struct tm *__tm_info; \
time(&__timer); \
__tm_info = localtime(&__timer); \
strftime(__buffer, 25, "%Y-%m-%d %H:%M:%S", __tm_info); \
proxy_error_func("%s [ERROR] " fmt , __buffer , ## __VA_ARGS__); \
} while(0)
#endif
*/
/*
#ifdef DEBUG
*/

#define proxy_warning(fmt, ...) \
do { \
time_t __timer; \
Expand All @@ -150,20 +118,6 @@ class Timer {
proxy_error_func(ecode, "%s %s:%d:%s(): [WARNING] " fmt, __buffer, __FILE__, __LINE__, __func__ , ## __VA_ARGS__); \
} while(0)

/*
#else
#define proxy_warning(fmt, ...) \
do { \
time_t __timer; \
char __buffer[25]; \
struct tm *__tm_info; \
time(&__timer); \
__tm_info = localtime(&__timer); \
strftime(__buffer, 25, "%Y-%m-%d %H:%M:%S", __tm_info); \
proxy_error_func("%s [WARNING] " fmt , __buffer , ## __VA_ARGS__); \
} while(0)
#endif
*/
#ifdef DEBUG
#define proxy_info(fmt, ...) \
do { \
Expand Down Expand Up @@ -211,13 +165,26 @@ class Timer {
#endif

#ifdef DEBUG
//void *debug_logger();
#endif

#define NULL_DB_MSG "The pointer to sqlite3 database is NULL. Cannot get error message."

#define ASSERT_SQLITE_OK(rc, db) \
do { \
if (rc!=SQLITE_OK) { \
proxy_error("SQLite3 error with return code %d. Error message: %s. Shutting down.\n", rc, db?(*proxy_sqlite3_errmsg)(db->get_db()):"The pointer to sqlite3 database is null. Cannot get error message."); \
proxy_error( \
"SQLite3 error. Shutting down rc=%d msg='%s'\n", \
rc, db ? (*proxy_sqlite3_errmsg)(db->get_db()) : NULL_DB_MSG); \
assert(0); \
} \
} while(0)

#define ASSERT_SQLITE3_OK(rc, db) \
do { \
if (rc!=SQLITE_OK) { \
proxy_error( \
"SQLite3 error. Shutting down rc=%d msg='%s'\n", \
rc, db ? (*proxy_sqlite3_errmsg)(db) : NULL_DB_MSG); \
assert(0); \
} \
} while(0)
Expand All @@ -243,12 +210,12 @@ SQLite3_result* proxysql_get_message_stats(bool reset=false);
*/
void proxysql_init_debug_prometheus_metrics();


class SQLite3DB;
/**
* @brief Set or unset if Admin has debugdb_disk fully initialized
*/
void proxysql_set_admin_debugdb_disk(SQLite3DB *_db);

void proxysql_set_admin_debug_output(unsigned int _do);

#endif
#endif // DEBUG
51 changes: 16 additions & 35 deletions lib/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,16 @@ using std::string;
using std::unordered_map;

#ifdef DEBUG
#ifdef DEBUG_EXTERN
#undef DEBUG_EXTERN
#endif /* DEBUG_EXTERN */
#endif /* DEBUG */

#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC SYSTEM_CLOCK
#endif // CLOCK_MONOTONIC

#ifdef DEBUG
__thread unsigned long long pretime=0;
static pthread_mutex_t debug_mutex;
static pthread_rwlock_t filters_rwlock;
static SQLite3DB * debugdb_disk = NULL;
sqlite3_stmt *statement1=NULL;
static unsigned int debug_output = 1;
#endif /* DEBUG */

/*
static inline unsigned long long debug_monotonic_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
}
*/

#define DEBUG_MSG_MAXSIZE 1024

#ifdef DEBUG


/**
* @brief Contains all filters related to debug.
* @details The convention for key value is `filename:line:function`. This key structure also applies also
Expand All @@ -58,7 +37,6 @@ static inline unsigned long long debug_monotonic_time() {
std::set<std::string>* debug_filters = nullptr;

static bool filter_debug_entry(const char *__file, int __line, const char *__func) {
//pthread_mutex_lock(&debug_mutex);
pthread_rwlock_rdlock(&filters_rwlock);
bool to_filter = false;
if (debug_filters && debug_filters->size()) { // if the set is empty we aren't performing any filter, so we won't search
Expand Down Expand Up @@ -99,27 +77,23 @@ static bool filter_debug_entry(const char *__file, int __line, const char *__fun
}
}
}
//pthread_mutex_unlock(&debug_mutex);
pthread_rwlock_unlock(&filters_rwlock);
return to_filter;
}

// we use this function to sent the filters to Admin
// we hold here the lock on filters_rwlock
void proxy_debug_get_filters(std::set<std::string>& f) {
//pthread_mutex_lock(&debug_mutex);
pthread_rwlock_rdlock(&filters_rwlock);
if (debug_filters) {
f = *debug_filters;
}
pthread_rwlock_unlock(&filters_rwlock);
//pthread_mutex_unlock(&debug_mutex);
}

// we use this function to get the filters from Admin
// we hold here the lock on filters_rwlock
void proxy_debug_load_filters(std::set<std::string>& f) {
//pthread_mutex_lock(&debug_mutex);
pthread_rwlock_wrlock(&filters_rwlock);
if (debug_filters) {
debug_filters->erase(debug_filters->begin(), debug_filters->end());
Expand All @@ -128,11 +102,19 @@ void proxy_debug_load_filters(std::set<std::string>& f) {
debug_filters = new std::set<std::string>(f);
}
pthread_rwlock_unlock(&filters_rwlock);
//pthread_mutex_unlock(&debug_mutex);
}

// REMINDER: This function should always save/restore 'errno', otherwise it could influence error handling.
void proxy_debug_func(enum debug_module module, int verbosity, int thr, const char *__file, int __line, const char *__func, const char *fmt, ...) {
void proxy_debug_func(
enum debug_module module,
int verbosity,
int thr,
const char *__file,
int __line,
const char *__func,
const char *fmt,
...
) {
int saved_errno = errno;
assert(module<PROXY_DEBUG_UNKNOWN);
if (pretime == 0) { // never initialized
Expand All @@ -147,17 +129,22 @@ void proxy_debug_func(enum debug_module module, int verbosity, int thr, const ch
errno = saved_errno;
return;
}

char origdebugbuff[DEBUG_MSG_MAXSIZE];
char debugbuff[DEBUG_MSG_MAXSIZE];
char longdebugbuff[DEBUG_MSG_MAXSIZE*8];
char longdebugbuff2[DEBUG_MSG_MAXSIZE*8];

longdebugbuff[0]=0;
longdebugbuff2[0]=0;

unsigned long long curtime=realtime_time();
bool write_to_disk = false;

if (debugdb_disk != NULL && (debug_output == 2 || debug_output == 3)) {
write_to_disk = true;
}

if (
GloVars.global.foreground
||
Expand All @@ -167,7 +154,6 @@ void proxy_debug_func(enum debug_module module, int verbosity, int thr, const ch
va_start(ap, fmt);
vsnprintf(origdebugbuff, DEBUG_MSG_MAXSIZE,fmt,ap);
va_end(ap);
//fprintf(stderr, "%d:%s:%d:%s(): MOD#%d LVL#%d : %s" , thr, __file, __line, __func, module, verbosity, debugbuff);
sprintf(longdebugbuff, "%llu(%llu): %d:%s:%d:%s(): MOD#%d#%s LVL#%d : %s" , curtime, curtime-pretime, thr, __file, __line, __func, module, GloVars.global.gdbg_lvl[module].name, verbosity, origdebugbuff);
}
#ifdef __GLIBC__
Expand All @@ -176,14 +162,12 @@ void proxy_debug_func(enum debug_module module, int verbosity, int thr, const ch
char **strings;
int s;
s = backtrace(arr, 20);
//backtrace_symbols_fd(arr, s, STDERR_FILENO);
strings=backtrace_symbols(arr,s);
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
for (int i=0; i<s; i++) {
//printf("%s\n", strings[i]);
debugbuff[0]=0;
sscanf(strings[i], "%*[^(](%100[^+]", debugbuff);
int status;
Expand All @@ -194,11 +178,7 @@ void proxy_debug_func(enum debug_module module, int verbosity, int thr, const ch
strcat(longdebugbuff2,debugbuff);
}
}
//printf("\n");
//strcat(longdebugbuff2,"\n");
free(strings);
// } else {
// fprintf(stderr, "%s", longdebugbuff);
}
#endif
pthread_mutex_lock(&debug_mutex);
Expand Down Expand Up @@ -518,4 +498,5 @@ void proxysql_set_admin_debugdb_disk(SQLite3DB * _db) {
void proxysql_set_admin_debug_output(unsigned int _do) {
debug_output = _do;
}

#endif /* DEBUG */
Loading

0 comments on commit 6af8b84

Please sign in to comment.