-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDBG_MACROS.hpp
102 lines (87 loc) · 2.9 KB
/
DBG_MACROS.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <stdarg.h>
#include <string.h>
//----------------------------------------------------------
// color macros
//----------------------------------------------------------
#define TXT_RED "\x1b[31m"
#define TXT_GREEN "\x1b[32m"
#define TXT_YELLOW "\x1b[33m"
#define TXT_BLUE "\x1b[34m"
#define TXT_MAGENTA "\x1b[35m"
#define TXT_CYAN "\x1b[36m"
#define TXT_RESET "\x1b[0m"
//----------------------------------------------------------
// enums
//----------------------------------------------------------
enum eDBG_TAG
{
eDBGMSG_TAG,
eDBGWRN_TAG,
eDBGERR_TAG
};
//----------------------------------------------------------
// function prototypes
//----------------------------------------------------------
void printMsg(const eDBG_TAG tag, const char* file, const char* func, const char *fmt, ...);
const char* getTag(const eDBG_TAG tag);
void setColor(const eDBG_TAG tag);
//----------------------------------------------------------
// DEBUG MACROS
//----------------------------------------------------------
#define DBG_MSG(...) printMsg(eDBGMSG_TAG, __FILE__, __FUNCTION__, __VA_ARGS__)
#define DBG_WRN(...) printMsg(eDBGWRN_TAG, __FILE__, __FUNCTION__, __VA_ARGS__)
#define DBG_ERR(...) printMsg(eDBGERR_TAG, __FILE__, __FUNCTION__, __VA_ARGS__)
//----------------------------------------------------------
// printMsg
// function that prints the tag, file, function,
// and message to the console in the appropriate color
//----------------------------------------------------------
void printMsg(const eDBG_TAG tag, const char* file, const char* func, const char *fmt, ...)
{
// set the color
setColor(tag);
// print the message information, type, file/funct/etc
fprintf( stderr, "%s - %s::%s ", getTag(tag), file, func);
// print the variadic args
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
// send new line and reset the color
fprintf(stderr, "%s \n", TXT_RESET);
}
//----------------------------------------------------------
// getTag
// returns the string of the dbg msg type
//----------------------------------------------------------
const char* getTag(const eDBG_TAG tag)
{
return ( tag == eDBGMSG_TAG ? "[M]" :
( tag == eDBGWRN_TAG ? "[W]" :
( tag == eDBGERR_TAG ? "[E]" : "[?]" )));
}
//----------------------------------------------------------
// setColor
// sets the color on the console
//----------------------------------------------------------
void setColor(const eDBG_TAG tag)
{
switch( tag )
{
case eDBGMSG_TAG:
fprintf( stderr, "%s", TXT_GREEN );
break;
case eDBGWRN_TAG:
fprintf( stderr, "%s", TXT_BLUE );
break;
case eDBGERR_TAG:
fprintf( stderr, "%s", TXT_RED );
break;
default:
fprintf( stderr, "%s", TXT_RESET );
break;
}
}