-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cpp
111 lines (86 loc) · 2.25 KB
/
Log.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include "log.h"
#include <iostream>
#include <stdarg.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>
bool LogCreated = false;
void Log(const char file[], int line, const char* format, ...) {
static char tmp_string[4096];
static char message[4096];
static va_list ap;
// Construct the string from variable arguments
va_start(ap, format);
vsprintf_s(tmp_string, format, ap);
va_end(ap);
sprintf_s(message, "\n%s(%d) : %s", file, line, tmp_string);
#if defined(OPTION_OUTPUT)
#if OPTION_OUTPUT==IOSTREAM_OUTPUT
//using simple console, redirect output
if (start == false) {
start = true;
RedirectIOToConsole();
}
#else
//using visual studio
OutputDebugString(message);
#endif
#endif
//using simple console with windows or unix
#if (defined(OPTION_OUTPUT) && OPTION_OUTPUT==IOSTREAM_OUTPUT) || !defined(OPTION_OUTPUT)
//improve, add an option to indicate the level of log (error, warning, info...)
//std::cerr << tmp_string2;
std::clog << message;
#endif
#if defined(FILE_OUTPUT)
#if FILE_OUTPUT==true
FILE *fileLog;
if (!LogCreated) {
fopen_s(&fileLog, LOGFILE, "w");
LogCreated = true;
} else
fopen_s(&fileLog, LOGFILE, "a");
if (file == NULL) {
if (LogCreated)
LogCreated = false;
return;
} else {
fputs(message, fileLog);
fclose(fileLog);
}
if (file)
fclose(fileLog);
#endif
#endif
}
#if defined(OPTION_OUTPUT) && OPTION_OUTPUT==IOSTREAM_OUTPUT
#pragma warning(push)
#pragma warning(disable:4302)
#pragma warning(disable:4311)
#pragma warning(disable:4068)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
void RedirectIOToConsole() {
int hConHandle;
long lStdHandle;
FILE *fp;
// allocate a console for this app
AllocConsole();
// redirect unbuffered STDOUT to the console
lStdHandle = (long) GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen(hConHandle, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
// redirect unbuffered STDERR to the console
lStdHandle = (long) GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen(hConHandle, "w");
*stderr = *fp;
setvbuf(stderr, NULL, _IONBF, 0);
}
#pragma GCC diagnostic pop
#pragma warning(pop)
#endif