-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathlogging.cpp
More file actions
143 lines (125 loc) · 3.56 KB
/
Copy pathlogging.cpp
File metadata and controls
143 lines (125 loc) · 3.56 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (C) 2013-2014 LINK/2012 <dma_2012@hotmail.com>
* Licensed under the MIT License, see LICENSE at top level directory.
*
*/
#include <stdinc.hpp>
#include "loader.hpp"
// Not thread-safe
static FILE* logfile = 0;
/*
* Loader::OpenLog
* Opens the logging stream
*/
void Loader::OpenLog()
{
// If the stream isn't open yet, open it
if(!logfile)
{
// Opens the logging stream
this->TruncateLog();
// Write the log header, with version number and IsDev information
Log("========================== Mod Loader %d.%d.%d %s==========================\n",
MODLOADER_VERSION_MAJOR, MODLOADER_VERSION_MINOR, MODLOADER_VERSION_REVISION,
MODLOADER_VERSION_ISDEV? "Development Build " : "");
}
}
/*
* Loader::TruncateLog
* Clears the log file
*/
void Loader::TruncateLog()
{
// Clears the ammount of bytes written...
auto path = this->modloaderPath + "modloader.log";
numBytesInLog = 0;
if(logfile == nullptr)
{
// Log file hadn't been open before, open it now.
logfile = fopen(path.c_str(), "w");
if(!logfile) Error("Failed to open the log file");
}
else
{
// Reopen the file for truncation
if((logfile = freopen(path.c_str(), "w", logfile)) == 0)
{
// Wuut, we couldn't do it?
Error("Failed to truncate log! Closing it for safeness %s.", strerror(errno) );
CloseLog();
}
}
}
/*
* Loader::CloseLog
* Closes the logging stream
*/
void Loader::CloseLog()
{
// If the logging stream is open...
if(logfile)
{
// ...close it
fclose(logfile);
logfile = 0;
}
}
/*
* Loader::Log
* Logs the message (msg, ..., '\n') into the logging stream
*/
void Loader::Log(const char* msg, ...)
{
va_list va; va_start(va, msg);
vLog(msg, va);
va_end(va);
}
/*
* Loader::Log
* Logs the message (msg, va, '\n') into the logging stream
*/
void Loader::vLog(const char* msg, va_list va)
{
if(logfile)
{
loader.numBytesInLog += vfprintf(logfile, msg, va) + 2;
fputc('\n', logfile);
if(loader.bImmediateFlush) fflush(logfile);
// Truncate log if it's too big
if(loader.numBytesInLog >= loader.maxBytesInLog)
loader.TruncateLog();
}
}
/*
* Loader::Error
* Shows a message box with the content of (msg, ...)
*/
void Loader::Error(const char* msg, ...)
{
char buffer[1024];
// Print message into buffer
va_list va; va_start(va, msg);
vsprintf(buffer, msg, va);
va_end(va);
// Show message box with the message
MessageBoxA(NULL, buffer, "Mod Loader", MB_ICONERROR);
}
/*
* Loader::FatalError
* Shows a message box with the content of ("Fatal Error: ", msg, ...) then terminates application
* This should be called only on very unhandleable cases.
*/
void Loader::FatalError(const char* msg, ...)
{
char buffer[1024];
// Print message into buffer
va_list va; va_start(va, msg);
vsprintf(buffer, msg, va);
va_end(va);
strcat(buffer, "\n\n"
"You can either continue the program execution or terminate it. Continuation of the execution WILL cause problems in-game or with Mod Loader itself!!!\n"
"Do you want to continue program execution? It is NOT recommended to do so.");
// Fatal error, continuing the execution may be a problem
if(MessageBoxA(NULL, buffer, "Mod Loader", MB_ICONERROR | MB_YESNO) == IDNO)
std::terminate();
}