-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.c
More file actions
112 lines (85 loc) · 2.85 KB
/
console.c
File metadata and controls
112 lines (85 loc) · 2.85 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
/* console.c - David C. Bishop - 17 Nov 2008
*
* Thie file handles the pretty colourful text output, logging and the smiley
* faces and such.
*
* [TODO]: It should probably be truly unicode aware. I had a look at using
* w_char, but its really designed to be a drop in replacment for printf and it
* was looking to complicated doing that.
* [TODO]: I tried to compress all the function into one main function with a
* wrapper put passing va_lists was stripping the unicode (see above todo note).
* [TODO]: Selecting unicode/colour should be dooable runtime, not #defined.
*/
#include "console.h"
/**
* Outputs data in the LOG format.
*/
void logit(const char* format, ...) {
char prefix[] = "LOG";
char* colour = COLOUR_LIGHT_GREEN;
int newsize = strlen(COLOUR_WHITE)*2 + strlen(colour) +
strlen(prefix) + strlen(COLOUR_NONE) + strlen(format) + 6;
va_list ap;
char* newformat = (char*)malloc(newsize);
snprintf(newformat, newsize, "%s[%s%s%s]: %s%s\n",
COLOUR_WHITE, colour, prefix, COLOUR_WHITE, COLOUR_NONE, format);
va_start(ap, format);
vprintf(newformat, ap);
va_end(ap);
free(newformat);
}
/**
* Outputs an error message.
*/
void errorit(const char* format, ...) {
char prefix[] = "ERROR";
char* colour = COLOUR_LIGHT_RED;
int newsize = strlen(COLOUR_WHITE)*2 + strlen(colour) +
strlen(prefix) + strlen(COLOUR_NONE) + strlen(format) + 6;
va_list ap;
char* newformat = (char*)malloc(newsize);
snprintf(newformat, newsize, "%s[%s%s%s]: %s%s\n",
COLOUR_WHITE, colour, prefix, COLOUR_WHITE, COLOUR_NONE, format);
va_start(ap, format);
vfprintf(stderr, newformat, ap);
va_end(ap);
free(newformat);
}
/**
* Outputs some debbuging info providing the level is less than the set one.
* @param level The debug level.
* @param format The string format.
* @param ... The string args.
*/
void debugit(int level, const char* format, ...) {
return; // Quick hack to stop all debuging...
if(DEBUG_LEVEL < level) {
return;
}
char prefix[] = "DEBUG";
char* colour = COLOUR_YELLOW;
int newsize = strlen(COLOUR_WHITE)*2 + strlen(colour) +
strlen(prefix) + strlen(COLOUR_NONE) + strlen(format) + 6;
va_list ap;
char* newformat = (char*)malloc(newsize);
snprintf(newformat, newsize, "%s[%s%s%s]: %s%s\n",
COLOUR_WHITE, colour, prefix, COLOUR_WHITE, COLOUR_NONE, format);
va_start(ap, format);
vprintf(newformat, ap);
va_end(ap);
free(newformat);
}
void warnit(const char* format, ...) {
char prefix[] = "WARNING";
char* colour = COLOUR_LIGHT_MAGENTA;
int newsize = strlen(COLOUR_WHITE)*2 + strlen(colour) +
strlen(prefix) + strlen(COLOUR_NONE) + strlen(format) + 6;
va_list ap;
char* newformat = (char*)malloc(newsize);
snprintf(newformat, newsize, "%s[%s%s%s]: %s%s\n",
COLOUR_WHITE, colour, prefix, COLOUR_WHITE, COLOUR_NONE, format);
va_start(ap, format);
vprintf(newformat, ap);
va_end(ap);
free(newformat);
}