| 1 |
/** |
| 2 |
* @class ProtoDebug |
| 3 |
* |
| 4 |
* @brief Debugging routines |
| 5 |
* |
| 6 |
* PROTOLIB DEBUG LEVELS: |
| 7 |
* |
| 8 |
* @li PL_FATAL=0 // The FATAL level designates very severe error events that will presumably lead the application to abort. |
| 9 |
* @li PL_ERROR=1; // The ERROR level designates error events that might still allow the application to continue running. |
| 10 |
* @li PL_WARN=2; // The WARN level designates potentially harmful situations. |
| 11 |
* @li PL_INFO=3; // The INFO level designates informational messages that highlight the progress of the application at coarse-grained level. |
| 12 |
* @li PL_DEBUG=4; // The DEBUG level designates fine-grained informational events that are most useful to debug an application. |
| 13 |
* @li PL_TRACE=5; // The TRACE level designates finer-grained informational events than the DEBUG |
| 14 |
* @li PL_DETAIL=6; // The TRACE level designates even finer-grained informational events than the DEBUG |
| 15 |
* @li PL_MAX=7; // Turn all comments on |
| 16 |
* @li PL_ALWAYS // Messages at this level are always printed regardless of debug level |
| 17 |
*/ |
| 18 |
#ifndef _PROTO_DEBUG |
| 19 |
#define _PROTO_DEBUG |
| 20 |
|
| 21 |
#include "protoDefs.h" |
| 22 |
|
| 23 |
#ifdef WIN32 |
| 24 |
#include <winsock2.h> |
| 25 |
#else |
| 26 |
#include <string.h> // for strerror() |
| 27 |
#include <errno.h> // for errno |
| 28 |
#endif // if/else WIN32/UNIX |
| 29 |
|
| 30 |
enum ProtoDebugLevel {PL_FATAL, PL_ERROR, PL_WARN, PL_INFO, PL_DEBUG, PL_TRACE, PL_DETAIL, PL_MAX, PL_ALWAYS}; |
| 31 |
|
| 32 |
#if defined(PROTO_DEBUG) || defined(PROTO_MSG) |
| 33 |
void SetDebugLevel(unsigned int level); |
| 34 |
unsigned int GetDebugLevel(); |
| 35 |
|
| 36 |
bool OpenDebugLog(const char *path); // log debug messages to the file named "path" |
| 37 |
void CloseDebugLog(); |
| 38 |
bool OpenDebugPipe(const char* pipeName); // log debug messages to a datagram ProtoPipe (PLOG only) |
| 39 |
void CloseDebugPipe(); |
| 40 |
void ProtoDMSG(unsigned int level, const char *format, ...); |
| 41 |
void ProtoLog(ProtoDebugLevel level, const char *format, ...); |
| 42 |
FILE* GetDebugLog(); |
| 43 |
#ifdef WIN32 |
| 44 |
void OpenDebugWindow(); |
| 45 |
void PopupDebugWindow(); |
| 46 |
void CloseDebugWindow(); |
| 47 |
#endif // WIN32 |
| 48 |
#else |
| 49 |
inline void SetDebugLevel(unsigned int level) {} |
| 50 |
inline unsigned int GetDebugLevel() {return PL_FATAL;} |
| 51 |
inline bool OpenDebugLog(const char *path) {return true;} |
| 52 |
inline void CloseDebugLog() {} |
| 53 |
inline bool OpenDebugPipe(const char* pipeName) {return true;} |
| 54 |
inline void CloseDebugPipe() {} |
| 55 |
inline void ProtoDMSG(unsigned int level, const char *format, ...) {} |
| 56 |
inline void ProtoLog(ProtoDebugLevel level, const char *format, ...) {} |
| 57 |
#ifdef WIN32 |
| 58 |
inline void OpenDebugWindow() {} |
| 59 |
inline void PopupDebugWindow() {} |
| 60 |
inline void CloseDebugWindow() {} |
| 61 |
#endif // WIN32 |
| 62 |
#endif // if/else PROTO_DEBUG || PROTO_MSG |
| 63 |
|
| 64 |
// printf()-like debug logging routines (Note DMSG() is deprecated) |
| 65 |
// Call the PLOG() macro for better performance |
| 66 |
inline void ProtoNoop() {} |
| 67 |
#define PLOG(X, ...) ((((unsigned int)X <= ::GetDebugLevel()) || (X == PL_ALWAYS)) ? \ |
| 68 |
ProtoLog(X, ##__VA_ARGS__) : ProtoNoop()) |
| 69 |
#define DMSG(X, ...) ((unsigned int)X <= ::GetDebugLevel() ? ProtoDMSG(X, ##__VA_ARGS__) : ProtoNoop()) |
| 70 |
|
| 71 |
#if defined(PROTO_DEBUG) || defined(PROTO_MSG) |
| 72 |
|
| 73 |
// The following prototype and "SetAssertFunction()" allows the behavior of the PROTO_ASSERT macro |
| 74 |
// to be overridden. The default behavior is just a call to the "assert()" system call (or ABORT() if no "assert()" call is available) |
| 75 |
// IMPORTANT NOTE: The "NDEBUG" macro can be enabled to disable the "assert()" system call even |
| 76 |
// if the "PROTO_DEBUG" is kept in place. |
| 77 |
|
| 78 |
typedef void (ProtoAssertFunction)(bool condition, const char* conditionText, const char* fileName, int lineNumber, void* userData); |
| 79 |
void SetAssertFunction(ProtoAssertFunction* assertFunction, void* userData = NULL); |
| 80 |
void ClearAssertFunction(); |
| 81 |
bool HasAssertFunction(); |
| 82 |
void ProtoAssertHandler(bool condition, const char* conditionText, const char* fileName, int lineNumber); |
| 83 |
|
| 84 |
void PROTO_ABORT(const char *format, ...); |
| 85 |
|
| 86 |
#ifdef HAVE_ASSERT |
| 87 |
#ifdef __ANDROID__ |
| 88 |
#include <android/log.h> |
| 89 |
#define PROTO_ASSERT(X) {if (HasAssertFunction()) ProtoAssertHandler(X, #X, __FILE__, __LINE__); \ |
| 90 |
else if (!((bool)(X))) __android_log_assert(#X, "protolib", "%s line:%d", __FILE__, __LINE__);} |
| 91 |
#else |
| 92 |
#include <assert.h> |
| 93 |
#define PROTO_ASSERT(X) {if (HasAssertFunction()) ProtoAssertHandler(X, #X, __FILE__, __LINE__); else assert(X);} |
| 94 |
#endif // if/else __ANDROID__ |
| 95 |
#else |
| 96 |
#define PROTO_ASSERT(X) \ |
| 97 |
{if (HasAssertFunction()) ProtoAssertHandler(X, #X, __FILE__, __LINE__); \ |
| 98 |
else if (!((bool)(X))) PROTO_ABORT("ASSERT(%s) failed at line %d in source file \"%s\"\n", #X, __LINE__, __FILE__);} |
| 99 |
#endif // if/else HAVE_ASSERT |
| 100 |
|
| 101 |
|
| 102 |
#ifdef TRACE |
| 103 |
#undef TRACE |
| 104 |
#endif // TRACE |
| 105 |
void TRACE(const char *format, ...); |
| 106 |
|
| 107 |
#else // !PROTO_DEBUG |
| 108 |
|
| 109 |
#define PROTO_ASSERT(X) |
| 110 |
#ifndef ABORT |
| 111 |
#define ABORT(X) |
| 112 |
#endif // !ABORT |
| 113 |
#ifndef TRACE |
| 114 |
inline void TRACE(const char *format, ...) {} |
| 115 |
#endif // !TRACE |
| 116 |
|
| 117 |
#endif // if/else PROTO_DEBUG |
| 118 |
|
| 119 |
// Historically, "protolib" has used an "ASSERT()" macro for assertions. It now makes sense to |
| 120 |
// transition to a more explicit "PROTO_ASSERT()" - we should/will probably deprecate this |
| 121 |
// "ASSERT()" macro definition but for now it is in many projects that use Protolib |
| 122 |
#undef ASSERT |
| 123 |
#define ASSERT(X) PROTO_ASSERT(X) |
| 124 |
|
| 125 |
inline const char* GetErrorString() |
| 126 |
{ |
| 127 |
#ifdef WIN32 |
| 128 |
static char errorString[256]; |
| 129 |
errorString[255] = '\0'; |
| 130 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | |
| 131 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 132 |
NULL, |
| 133 |
GetLastError(), |
| 134 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
| 135 |
(LPTSTR) errorString, 255, NULL); |
| 136 |
return errorString; |
| 137 |
#else |
| 138 |
return strerror(errno); |
| 139 |
#endif // if/else WIN32/UNIX |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
#ifdef WIN32 |
| 144 |
typedef DWORD ProtoErrorCode; |
| 145 |
#else |
| 146 |
typedef int ProtoErrorCode; |
| 147 |
#endif // if/else WIN32/UNIX |
| 148 |
|
| 149 |
inline const char* GetErrorString(ProtoErrorCode errorCode) |
| 150 |
{ |
| 151 |
#ifdef WIN32 |
| 152 |
static char errorString[256]; |
| 153 |
errorString[255] = '\0'; |
| 154 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | |
| 155 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 156 |
NULL, |
| 157 |
errorCode, |
| 158 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
| 159 |
(LPTSTR) errorString, 255, NULL); |
| 160 |
return errorString; |
| 161 |
#else |
| 162 |
return strerror(errorCode); |
| 163 |
#endif // if/else WIN32/UNIX |
| 164 |
} |
| 165 |
|
| 166 |
#endif // _PROTO_DEBUG |