-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExceptions.cpp
More file actions
184 lines (145 loc) · 4.46 KB
/
Exceptions.cpp
File metadata and controls
184 lines (145 loc) · 4.46 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*************************************************************************************
cpl - cross platform library - v. 0.1.0.
Copyright (C) 2017 Janus Lynggaard Thorborg (www.jthorborg.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
See \licenses\ for additional details on licenses associated with this program.
**************************************************************************************
file:Exceptions.cpp
Implementation of Exceptions.h
*************************************************************************************/
#include "Exceptions.h"
#include "Misc.h"
#include "CExclusiveFile.h"
#include "PlatformSpecific.h"
namespace cpl
{
int GetLastOSError()
{
#ifdef CPL_WINDOWS
return GetLastError();
#else
return errno;
#endif
}
std::string GetLastOSErrorMessage(int errorToUse)
{
auto lastError = errorToUse;
#ifdef CPL_WINDOWS
char * apiPointer = nullptr;
auto numChars = FormatMessageA
(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
0,
lastError,
0,
apiPointer,
0,
0
);
std::string ret(apiPointer, numChars);
LocalFree(apiPointer);
return ret;
#else
return "Error (" + std::to_string(lastError) + "): " + strerror(lastError);
#endif
}
std::string GetLastOSErrorMessage()
{
return GetLastOSErrorMessage(GetLastOSError());
}
const std::string& GetExceptionLogFilePath()
{
using namespace cpl::Misc;
static const std::string path = GetDirectoryPath() + "/" + programInfo.name + " exceptions.log";
return path;
}
void CheckPruneExceptionLogFile()
{
static std::once_flag flag;
std::call_once(
flag,
[] {
const auto& path = GetExceptionLogFilePath();
std::int64_t size = -1;
{
FILE* f = std::fopen(path.c_str(), "r");
if (!f)
return;
#ifdef CPL_WINDOWS
struct _stat fstatbuffer;
auto fd = _fileno(f);
if (_fstat(fd, &fstatbuffer) == 0)
size = fstatbuffer.st_size;
#else
struct stat fstatbuffer;
auto fd = fileno(f);
if (fstat(fd, &fstatbuffer) == 0)
size = fstatbuffer.st_size;
#endif
std::fclose(f);
}
// bigger than 2 megabytes?
if (size > 20e5)
{
char fsizebytes[256];
cpl::sprintfs(fsizebytes, "%.1f", size / 10e5);
auto answer = cpl::Misc::MsgBox(
std::string("A log file for this program is ") + fsizebytes + " MB big.\nDo you want to clean it (harmless unless you want to report issues)?",
programInfo.name + ": Large logfile detected",
Misc::MsgStyle::sYesNoCancel | Misc::MsgIcon::iQuestion,
nullptr,
true
);
if (answer == Misc::MsgButton::bYes)
{
FILE* f = std::fopen(path.c_str(), "w");
if (f != nullptr)
std::fclose(f);
}
}
}
);
}
void LogException(const string_ref errorMessage)
{
using namespace cpl::Misc;
CExclusiveFile exceptionLog;
auto result = cpl::Misc::WaitOnCondition(
1000,
[&]() { return exceptionLog.open(GetExceptionLogFilePath(), exceptionLog.writeMode | exceptionLog.append, true); },
10
);
if (!result)
return;
exceptionLog.newline();
exceptionLog.write(("----------------" + GetDate() + ", " + GetTime() + "----------------").c_str());
exceptionLog.newline();
exceptionLog.write(("- Exception in \"" + programInfo.name + "\" v.\"" + std::to_string(programInfo.version) + "\"").c_str());
exceptionLog.newline();
exceptionLog.write(errorMessage.data(), errorMessage.size());
exceptionLog.newline();
}
void CrashIfUserDoesntDebug(const string_ref errorMessage)
{
using namespace cpl::Misc;
auto ret = MsgBox(errorMessage.string() + newl + newl + "Press yes to break after attaching a debugger. Press no to crash.", programInfo.name + ": Fatal error",
MsgStyle::sYesNo | MsgIcon::iStop);
if (ret == MsgButton::bYes)
{
CPL_BREAKIFDEBUGGED();
}
}
bool IsDebuggerAttached()
{
return cpl::Misc::IsBeingDebugged();
}
}