Robustify logging code

This commit is contained in:
Thulinma 2024-03-28 01:26:23 +01:00
parent 7a8e90e1a2
commit 6118a005f7

View file

@ -46,37 +46,31 @@ static const char *DBG_LVL_LIST[] ={"NONE", "FAIL", "ERROR", "WARN", "IN
#define PRIu32 "lu" #define PRIu32 "lu"
#endif #endif
#if !defined(__APPLE__) && !defined(__MACH__) && defined(__GNUC__) // Find program name best we can
#if defined(__APPLE__) || defined(__MACH__)
#define MIST_PROG getprogname()
#else
#if defined(__GNUC__)
#include <errno.h> #include <errno.h>
#define MIST_PROG program_invocation_short_name
#else
#define MIST_PROG "MistProcess"
#endif
#endif
#if DEBUG >= DLVL_DEVEL #if DEBUG >= DLVL_DEVEL
#define DEBUG_MSG(lvl, msg, ...) \ // Compiled at 4 or higher default? Include line numbers.
if (Util::printDebugLevel >= lvl){\ #define DEBUG_MSG(lvl, msg, ...) if (Util::printDebugLevel >= lvl){\
fprintf(stderr, "%s|%s|%d|%s:%d|%s|" msg "\n", DBG_LVL_LIST[lvl], program_invocation_short_name, \ fprintf(stderr, "%.8s|%.30s|%d|%.100s:%d|%.200s|" msg "\n",\
getpid(), __FILE__, __LINE__, Util::streamName, ##__VA_ARGS__); \ DBG_LVL_LIST[lvl], MIST_PROG, getpid(), __FILE__, __LINE__, Util::streamName, ##__VA_ARGS__);\
} }
#else #else
#define DEBUG_MSG(lvl, msg, ...) \ // Under 4? Then don't include line numbers.
if (Util::printDebugLevel >= lvl){\ #define DEBUG_MSG(lvl, msg, ...) if (Util::printDebugLevel >= lvl){\
fprintf(stderr, "%s|%s|%d||%s|" msg "\n", DBG_LVL_LIST[lvl], program_invocation_short_name, \ fprintf(stderr, "%.8s|%.30s|%d||%.200s|" msg "\n",\
getpid(), Util::streamName, ##__VA_ARGS__); \ DBG_LVL_LIST[lvl], MIST_PROG, getpid(), Util::streamName, ##__VA_ARGS__);\
} }
#endif #endif
#else
#if DEBUG >= DLVL_DEVEL
#define DEBUG_MSG(lvl, msg, ...) \
if (Util::printDebugLevel >= lvl){\
fprintf(stderr, "%s|%s|%d|%s:%d|%s|" msg "\n", DBG_LVL_LIST[lvl], getprogname(), getpid(), __FILE__, \
__LINE__, Util::streamName, ##__VA_ARGS__); \
}
#else
#define DEBUG_MSG(lvl, msg, ...) \
if (Util::printDebugLevel >= lvl){\
fprintf(stderr, "%s|MistProcess|%d||%s|" msg "\n", DBG_LVL_LIST[lvl], getpid(), \
Util::streamName, ##__VA_ARGS__); \
}
#endif
#endif
#if defined(_WIN32) || defined(__CYGWIN__) #if defined(_WIN32) || defined(__CYGWIN__)
static inline void show_stackframe(){} static inline void show_stackframe(){}