-
Notifications
You must be signed in to change notification settings - Fork 0
/
ILog.c
134 lines (105 loc) · 3.95 KB
/
ILog.c
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "ILog.h"
#if defined(__cplusplus)
extern "C" {
#endif
#define FILE_OPEN_SUCCESS 0
#ifdef __linux__
#include <string.h>
#endif
void _platformLog(FILE* stream, const char* msg, int colour) {
switch (colour) {
case _I_COLOUR_WHITE: {
// Check out https://www.tutorialspoint.com/how-to-output-colored-text-to-a-linux-terminal
// this is where I found out how this stuff works
fprintf(stream, "\033[0;37m%s\033[0m", msg);
break;
}
case _I_COLOUR_GREEN: {
fprintf(stream, "\033[0;32m%s\033[0m", msg);
break;
}
case _I_COLOUR_YELLOW: {
fprintf(stream, "\033[0;33m%s\033[0m", msg);
break;
}
case _I_COLOUR_RED: {
fprintf(stream, "\033[0;31m%s\033[0m", msg);
break;
}
case _I_COLOUR_FATAL_RED: {
fprintf(stream, "\033[;41m\033"); // Sets the background to red
fprintf(stream, "\033[4;30m%s\033[0m", msg);
break;
}
case 0: {
fprintf(stream, "%s", msg);
}
};
}
int _i_vscprintf (const char * format, va_list pargs) {
int retval;
va_list argcopy;
va_copy(argcopy, pargs);
retval = vsnprintf(NULL, 0, format, argcopy);
va_end(argcopy);
return retval;
}
void _i_log(FILE* stream, const char* prefix, const char* msg, int colour, ...) {
char* fmtBuffer = (char*)malloc( (strlen(prefix) + 1) * sizeof(char) + (strlen(msg) + 1) * sizeof(char) );
// Copy formated string into the fmtBuffer
sprintf(fmtBuffer, "%s%s", prefix, msg);
va_list args;
va_start(args, colour);
// returns the length of the formated string
// I'm not sure why you have to add 1 to this but I
// think _vscprintf and _i_vscprintf doesn't count \0 as a character
// but vsprintf_s and vsprintf_s needs that \0
size_t msgLen = _i_vscprintf(fmtBuffer, args) + 1;
char* msgBuffer = (char*)malloc( msgLen * 2 * sizeof(char) );
// This is different from sprintf becuase it takes in a va_list instead of variable args
#if defined(_MSVC_VER) || defined(_WIN32) && defined(__clang__)
vsprintf_s(msgBuffer, msgLen, fmtBuffer, args);
#else
// MSVC complains about this function being depracted
vsprintf(msgBuffer, fmtBuffer, args);
#endif
va_end(args);
_platformLog(stream, msgBuffer, colour);
fprintf(stream, "\n");
free(fmtBuffer);
free(msgBuffer);
}
void _f_i_log(const char* fileName, const char* msg, const char* mode, ...) {
va_list args;
va_start(args, mode);
// returns the length of the formated string
// I'm not sure why you have to add 1 to this but I
// think _vscprintf and _i_vscprintf doesn't count \0 as a character
// but vsprintf_s and vsprintf_s needs that \0
size_t msgLen = _i_vscprintf(msg, args) + 1;
//char* msgBuffer = new char[msgLen];
char* msgBuffer = (char*)malloc( (msgLen + 1) * sizeof(char) );
FILE* fStream;
// This is different from sprintf becuase it takes in a va_list instead of variable args
#if defined(_MSVC_VER) || defined(_WIN32) && defined(__clang__)
vsprintf_s(msgBuffer, msgLen, msg, args);
if (fopen_s(&fStream, fileName, mode) != FILE_OPEN_SUCCESS) {
fprintf(stderr, "ILog Internal Error: Failed to open file: %s", fileName);
}
#else
// MSVC complains about this function being depracted
vsprintf(msgBuffer, msg, args);
fStream = fopen(fileName, mode);
if (!fStream) {
fprintf(stderr, "ILog Internal Error: Failed to open file: %s", fileName);
}
#endif
va_end(args);
_platformLog(fStream, msgBuffer, 0);
fprintf(fStream, "\n");
free(msgBuffer);
fclose(fStream);
}
#if defined(__cplusplus)
}
#endif