annotate Engine/ErrorHandling.h @ 2516:56b6d74ce716

.
author Ritor1
date Thu, 02 Oct 2014 22:01:06 +0600
parents 68cdef6879a0
children c7264ab7132f
rev   line source
2499
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
1 #define _CRT_NON_CONFORMING_SWPRINTFS
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
2 #pragma once
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
3
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
4 #define Error(format, ...) Error_impl_(__FILE__, __FUNCTION__, __LINE__, format, __VA_ARGS__)
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
5 #define Assert(condition, ...) Assert_impl_(__FILE__, __FUNCTION__, __LINE__, condition, #condition, __VA_ARGS__)
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
6
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
7
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
8
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
9 #include <stdarg.h>
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
10 #include <stdio.h>
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
11 inline __declspec(noreturn) void Error_impl_(const char *filename, const char *functionname, int line,
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
12 const char *format, ...)
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
13 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
14 va_list va;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
15 va_start(va, format);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
16 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
17 char header[4096];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
18 sprintf(header, "Error in %s: %u\n\t%s\n\n", filename, line, functionname);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
19
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
20 char msg_body[8192];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
21 vsprintf(msg_body, format, va);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
22
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
23 wchar_t msg[sizeof(header) + sizeof(msg_body)];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
24 swprintf(msg, 8192, L"%S %S", header, msg_body);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
25
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
26 extern void MsgBox(const wchar_t *, const wchar_t *);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
27 MsgBox(msg, L"Error");
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
28 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
29 va_end(va);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
30
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
31 #ifndef NODEBUG
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
32 __debugbreak();
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
33 #endif
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
34 exit(0);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
35 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
36
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
37
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
38 inline void Assert_impl_(const char *filename, const char *functionname, int line,
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
39 bool condition, const char *condition_string, const char *format = nullptr, ...)
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
40 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
41 if (condition)
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
42 return;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
43
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
44 va_list va;
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
45 va_start(va, format);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
46 {
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
47 char header[4096];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
48 sprintf(header, "Assertion in %s: %u\n\t%s:\n%s\n\n", filename, line, functionname, condition_string);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
49
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
50 char msg_body[8192];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
51 vsprintf(msg_body, format, va);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
52
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
53 wchar_t msg[sizeof(header) + sizeof(msg_body)];
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
54 if (format)
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
55 swprintf(msg, (sizeof(header) + sizeof(msg_body)), L"%S %S", header, msg_body);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
56 else
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
57 swprintf(msg, (sizeof(header) + sizeof(msg_body)), L"%S", header);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
58
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
59 extern void MsgBox(const wchar_t *, const wchar_t *);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
60 MsgBox(msg, L"Assertion");
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
61 }
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
62 va_end(va);
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
63
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
64 __debugbreak();
68cdef6879a0 engine folder
Ritor1
parents:
diff changeset
65 }