-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_writer.c
217 lines (184 loc) · 5.29 KB
/
error_writer.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 - 2022 TheVice
*
*/
#include "error_writer.h"
#include "host_fxr.h"
#include "host_policy.h"
#include "buffer.h"
#include "common.h"
#include "file_system.h"
#if defined(_WIN32)
#include "text_encoding.h"
#include <wchar.h>
#endif
#if defined(_WIN32)
extern uint8_t file_open_wchar_t(const wchar_t* path, const wchar_t* mode, void** output);
#endif
static void* host_fx_resolver_error_file_writer = NULL;
static void* host_policy_error_file_writer = NULL;
error_writer_type set_error_writer(
const void* library_object, error_writer_type writer,
setter_type setter_function, const type_of_element* path,
uint8_t writer_number)
{
error_writer_type error_writer_pointer = NULL;
if (!path)
{
#if defined(_MSC_VER) && (_MSC_VER < 1910)
#pragma warning(suppress: 4054)
error_writer_pointer = setter_function(library_object, NULL);
#else
error_writer_pointer = setter_function(library_object, NULL);
#endif
}
void* file_stream = writer_number ? host_policy_error_file_writer : host_fx_resolver_error_file_writer;
if (file_stream && !file_close(file_stream))
{
return NULL;
}
file_stream = NULL;
if (writer_number)
{
host_policy_error_file_writer = file_stream;
}
else
{
host_fx_resolver_error_file_writer = file_stream;
}
if (path)
{
#if defined(_WIN32)
if (!file_open_wchar_t(path, L"ab", &file_stream))
{
return NULL;
}
#else
if (!file_open(path, (const uint8_t*)"ab", &file_stream))
{
return NULL;
}
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1910)
#pragma warning(suppress: 4054)
error_writer_pointer = setter_function(library_object, writer);
#else
error_writer_pointer = setter_function(library_object, writer);
#endif
if (writer_number)
{
host_policy_error_file_writer = file_stream;
}
else
{
host_fx_resolver_error_file_writer = file_stream;
}
}
return error_writer_pointer;
}
#if defined(_WIN32)
static uint8_t host_fx_resolver_error_writer_win32_content[BUFFER_SIZE_OF];
static uint8_t is_host_fx_resolver_error_writer_win32_content_initialized = 0;
static uint8_t host_policy_error_writer_win32_content[BUFFER_SIZE_OF];
static uint8_t is_host_policy_error_writer_win32_content_initialized = 0;
#define ERROR_WRITER_WIN32(MESSAGE, CONTENT, IS_CONTENT_INITIALIZED, ERROR_FILE_WRITER) \
\
if (IS_CONTENT_INITIALIZED) \
{ \
if (!buffer_resize((void*)(CONTENT), 0)) \
{ \
return; \
} \
} \
else \
{ \
(IS_CONTENT_INITIALIZED) = buffer_init((void*)(CONTENT), BUFFER_SIZE_OF); \
} \
\
if (ERROR_FILE_WRITER) \
{ \
if (!text_encoding_UTF16LE_to_UTF8((MESSAGE), \
(MESSAGE) + wcslen(MESSAGE), (void*)(CONTENT)) || \
!buffer_push_back((void*)(CONTENT), '\n')) \
{ \
return; \
} \
\
if (!file_write_with_several_steps((void*)(CONTENT), (ERROR_FILE_WRITER))) \
{ \
return; \
} \
\
file_flush(ERROR_FILE_WRITER); \
}
void host_fx_resolver_error_writer(const type_of_element* message)
{
ERROR_WRITER_WIN32(
message,
host_fx_resolver_error_writer_win32_content,
is_host_fx_resolver_error_writer_win32_content_initialized,
host_fx_resolver_error_file_writer);
}
void host_policy_error_writer(const type_of_element* message)
{
ERROR_WRITER_WIN32(
message,
host_policy_error_writer_win32_content,
is_host_policy_error_writer_win32_content_initialized,
host_policy_error_file_writer);
}
#else
#define ERROR_WRITER_POSIX(MESSAGE, ERROR_FILE_WRITER) \
if (ERROR_FILE_WRITER) \
{ \
static const uint8_t n = '\n'; \
\
if (!file_write(MESSAGE, sizeof(type_of_element), common_count_bytes_until(MESSAGE, 0), \
ERROR_FILE_WRITER) || \
!file_write(&n, sizeof(type_of_element), 1, ERROR_FILE_WRITER)) \
{ \
return; \
} \
\
file_flush(ERROR_FILE_WRITER); \
}
void host_fx_resolver_error_writer(const type_of_element* message)
{
ERROR_WRITER_POSIX(
message,
host_fx_resolver_error_file_writer);
}
void host_policy_error_writer(const type_of_element* message)
{
ERROR_WRITER_POSIX(
message,
host_policy_error_file_writer);
}
#endif
void error_writer_release_buffers(void* host_fxr_object, void* host_policy_object)
{
if (host_fx_resolver_error_file_writer)
{
host_fxr_set_error_writer(host_fxr_object, NULL);
file_close(host_fx_resolver_error_file_writer);
host_fx_resolver_error_file_writer = NULL;
}
if (host_policy_error_file_writer)
{
core_host_set_error_writer(host_policy_object, NULL);
file_close(host_policy_error_file_writer);
host_policy_error_file_writer = NULL;
}
#if defined(_WIN32)
if (is_host_fx_resolver_error_writer_win32_content_initialized)
{
buffer_release(&host_fx_resolver_error_writer_win32_content);
}
if (is_host_policy_error_writer_win32_content_initialized)
{
buffer_release(&host_policy_error_writer_win32_content);
}
#endif
}