php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
log.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Alexander Borisov
3 *
4 * Author: Alexander Borisov <borisov@lexbor.com>
5 */
6
7#include "lexbor/core/print.h"
9#include "lexbor/css/log.h"
10
11
12typedef struct {
13 const char *msg;
14 size_t length;
15}
17
18
19static const lxb_css_log_type_str_t lxb_css_log_types_map[] = {
20 {"Info", 4},
21 {"Warning", 7},
22 {"Error", 5},
23 {"Syntax error", 12}
24};
25
26
29{
30 return lexbor_calloc(1, sizeof(lxb_css_log_t));
31}
32
35{
37
38 if (log == NULL) {
40 }
41
42 status = lexbor_array_obj_init(&log->messages, 64,
43 sizeof(lxb_css_log_message_t));
44 if (status != LXB_STATUS_OK) {
45 return status;
46 }
47
48 if (mraw != NULL) {
49 log->mraw = mraw;
50 log->self_mraw = false;
51 return LXB_STATUS_OK;
52 }
53
54 log->self_mraw = true;
55
56 log->mraw = lexbor_mraw_create();
57
58 return lexbor_mraw_init(log->mraw, 4096);
59}
60
61void
63{
64 if (log != NULL) {
65 lexbor_array_obj_clean(&log->messages);
66
67 if (log->self_mraw) {
69 }
70 }
71}
72
75{
76 if (log == NULL) {
77 return NULL;
78 }
79
80 (void) lexbor_array_obj_destroy(&log->messages, false);
81
82 if (log->self_mraw) {
83 (void) lexbor_mraw_destroy(log->mraw, true);
84 }
85
86 if (self_destroy) {
88 }
89
90 return log;
91}
92
95 const lxb_char_t *str, size_t length)
96{
98
99 msg = lexbor_array_obj_push(&log->messages);
100 if (msg == NULL) {
101 return NULL;
102 }
103
104 if (lexbor_str_init(&msg->text, log->mraw, length) == NULL) {
105 lexbor_array_obj_pop(&log->messages);
106 return NULL;
107 }
108
109 memcpy(msg->text.data, str, length);
110 msg->text.length = length;
111
112 msg->text.data[length] = '\0';
113
114 msg->type = type;
115
116 return msg;
117}
118
121{
123
124 msg = lexbor_array_obj_push(&log->messages);
125 if (msg == NULL) {
126 return NULL;
127 }
128
129 if (lexbor_str_init(&msg->text, log->mraw, length) == NULL) {
130 lexbor_array_obj_pop(&log->messages);
131 return NULL;
132 }
133
134 msg->type = type;
135
136 return msg;
137}
138
141 const char *format, ...)
142{
143 size_t psize;
145 va_list va;
146
147 va_start(va, format);
148 psize = lexbor_vprintf_size(format, va);
149 va_end(va);
150
151 if (psize == LXB_PRINT_ERROR) {
152 return NULL;
153 }
154
156 if (msg == NULL) {
157 return NULL;
158 }
159
160 va_start(va, format);
161 (void) lexbor_vsprintf(msg->text.data, psize, format, va);
162 va_end(va);
163
164 msg->text.length = psize;
165
166 return msg;
167}
168
171 const char *module_name, const char *description)
172{
173 static const char unexpected[] = "%s. Not supported: %s";
174
176 module_name, description);
177}
178
179const lxb_char_t *
181{
182 if (out_length != NULL) {
183 *out_length = lxb_css_log_types_map[type].length;
184 }
185
186 return (const lxb_char_t *) lxb_css_log_types_map[type].msg;
187}
188
191 const lxb_char_t *indent, size_t indent_length)
192{
193 size_t i;
196
197 if (log->messages.length == 0) {
198 return LXB_STATUS_OK;
199 }
200
201 i = 0;
202
203 do {
204 msg = lexbor_array_obj_get(&log->messages, i);
205
206 if (indent != NULL) {
207 lexbor_serialize_write(cb, indent, indent_length, ctx, status);
208 }
209
211 if (status != LXB_STATUS_OK) {
212 return status;
213 }
214
215 i++;
216
217 if (i == log->messages.length) {
218 break;
219 }
220
221 lexbor_serialize_write(cb, "\n", 1, ctx, status);
222 }
223 while (true);
224
225 return LXB_STATUS_OK;
226}
227
230 const lxb_char_t *indent, size_t indent_length)
231{
232 size_t length = 0;
234 lexbor_str_t str;
235
237 indent, indent_length);
238 if (status != LXB_STATUS_OK) {
239 goto failed;
240 }
241
242 /* + 1 == '\0' */
243 str.data = lexbor_malloc(length + 1);
244 if (str.data == NULL) {
245 goto failed;
246 }
247
248 str.length = 0;
249
251 indent, indent_length);
252 if (status != LXB_STATUS_OK) {
253 lexbor_free(str.data);
254 goto failed;
255 }
256
257 str.data[str.length] = '\0';
258
259 if (out_length != NULL) {
260 *out_length = str.length;
261 }
262
263 return str.data;
264
265failed:
266
267 if (out_length != NULL) {
268 *out_length = 0;
269 }
270
271 return NULL;
272}
273
274
277 lexbor_serialize_cb_f cb, void *ctx)
278{
279 size_t length;
281 const lxb_char_t *type_name;
282
283 type_name = lxb_css_log_type_by_id(msg->type, &length);
284
285 lexbor_serialize_write(cb, type_name, length, ctx, status);
286 lexbor_serialize_write(cb, ". ", 2, ctx, status);
287 lexbor_serialize_write(cb, msg->text.data, msg->text.length, ctx, status);
288
289 return LXB_STATUS_OK;
290}
291
294 size_t *out_length)
295{
296 size_t length = 0;
298 lexbor_str_t str;
299
301 &length);
302 if (status != LXB_STATUS_OK) {
303 goto failed;
304 }
305
306 /* + 1 == '\0' */
307 str.data = lexbor_malloc(length + 1);
308 if (str.data == NULL) {
309 goto failed;
310 }
311
312 str.length = 0;
313
315 if (status != LXB_STATUS_OK) {
316 lexbor_free(str.data);
317 goto failed;
318 }
319
320 str.data[str.length] = '\0';
321
322 if (out_length != NULL) {
323 *out_length = str.length;
324 }
325
326 return str.data;
327
328failed:
329
330 if (out_length != NULL) {
331 *out_length = 0;
332 }
333
334 return NULL;
335}
void * lexbor_array_obj_pop(lexbor_array_obj_t *array)
Definition array_obj.c:147
void * lexbor_array_obj_push(lexbor_array_obj_t *array)
Definition array_obj.c:93
void lexbor_array_obj_clean(lexbor_array_obj_t *array)
Definition array_obj.c:42
lxb_status_t lexbor_array_obj_init(lexbor_array_obj_t *array, size_t size, size_t struct_size)
Definition array_obj.c:17
lexbor_array_obj_t * lexbor_array_obj_destroy(lexbor_array_obj_t *array, bool self_destroy)
Definition array_obj.c:50
lxb_inline void * lexbor_array_obj_get(const lexbor_array_obj_t *array, size_t idx)
Definition array_obj.h:70
char * cb
Definition assert.c:26
log(float $num, float $base=M_E)
lxb_status_t(* lexbor_serialize_cb_f)(const lxb_char_t *data, size_t len, void *ctx)
Definition base.h:82
@ LXB_STATUS_ERROR_OBJECT_IS_NULL
Definition base.h:52
@ LXB_STATUS_OK
Definition base.h:49
#define lexbor_serialize_write(cb, data, length, ctx, status)
Definition serialize.h:14
DNS_STATUS status
Definition dns_win32.c:49
size_t lexbor_vsprintf(lxb_char_t *dst, size_t size, const char *format, va_list va)
Definition print.c:100
size_t lexbor_vprintf_size(const char *format, va_list va)
Definition print.c:37
zend_ffi_type * type
Definition ffi.c:3812
memcpy(ptr1, ptr2, size)
#define NULL
Definition gdcache.h:45
LXB_API void * lexbor_free(void *dst)
Definition memory.c:33
LXB_API void * lexbor_malloc(size_t size)
Definition memory.c:15
LXB_API void * lexbor_calloc(size_t num, size_t size)
Definition memory.c:27
lxb_css_log_t * lxb_css_log_destroy(lxb_css_log_t *log, bool self_destroy)
Definition log.c:74
void lxb_css_log_clean(lxb_css_log_t *log)
Definition log.c:62
lxb_char_t * lxb_css_log_serialize_char(lxb_css_log_t *log, size_t *out_length, const lxb_char_t *indent, size_t indent_length)
Definition log.c:229
lxb_css_log_message_t * lxb_css_log_append(lxb_css_log_t *log, lxb_css_log_type_t type, const lxb_char_t *str, size_t length)
Definition log.c:94
lxb_css_log_message_t * lxb_css_log_format(lxb_css_log_t *log, lxb_css_log_type_t type, const char *format,...)
Definition log.c:140
lxb_status_t lxb_css_log_init(lxb_css_log_t *log, lexbor_mraw_t *mraw)
Definition log.c:34
const lxb_char_t * lxb_css_log_type_by_id(lxb_css_log_type_t type, size_t *out_length)
Definition log.c:180
lxb_css_log_message_t * lxb_css_log_push(lxb_css_log_t *log, lxb_css_log_type_t type, size_t length)
Definition log.c:120
lxb_status_t lxb_css_log_serialize(lxb_css_log_t *log, lexbor_serialize_cb_f cb, void *ctx, const lxb_char_t *indent, size_t indent_length)
Definition log.c:190
lxb_css_log_message_t * lxb_css_log_not_supported(lxb_css_log_t *log, const char *module_name, const char *description)
Definition log.c:170
lxb_char_t * lxb_css_log_message_serialize_char(lxb_css_log_message_t *msg, size_t *out_length)
Definition log.c:293
lxb_css_log_t * lxb_css_log_create(void)
Definition log.c:28
lxb_status_t lxb_css_log_message_serialize(lxb_css_log_message_t *msg, lexbor_serialize_cb_f cb, void *ctx)
Definition log.c:276
lxb_css_log_type_t
Definition log.h:20
@ LXB_CSS_LOG_SYNTAX_ERROR
Definition log.h:24
lexbor_mraw_t * lexbor_mraw_create(void)
Definition mraw.c:32
void lexbor_mraw_clean(lexbor_mraw_t *mraw)
Definition mraw.c:76
lxb_status_t lexbor_mraw_init(lexbor_mraw_t *mraw, size_t chunk_size)
Definition mraw.c:38
lexbor_mraw_t * lexbor_mraw_destroy(lexbor_mraw_t *mraw, bool destroy_self)
Definition mraw.c:87
char * msg
Definition phpdbg.h:289
#define LXB_PRINT_ERROR
Definition print.h:19
lxb_status_t lexbor_serialize_length_cb(const lxb_char_t *data, size_t length, void *ctx)
Definition serialize.c:12
lxb_status_t lexbor_serialize_copy_cb(const lxb_char_t *data, size_t length, void *ctx)
Definition serialize.c:19
lxb_char_t * lexbor_str_init(lexbor_str_t *str, lexbor_mraw_t *mraw, size_t size)
Definition str.c:22
lxb_char_t * data
Definition str.h:47
size_t length
Definition str.h:48
const char * msg
Definition log.c:13
unsigned int lxb_status_t
Definition types.h:28
unsigned char lxb_char_t
Definition types.h:27
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)