xref: /PHP-8.2/sapi/fpm/fpm/zlog.c (revision 325ca31d)
1 	/* (c) 2004-2007 Andrei Nigmatulin */
2 
3 #include "fpm_config.h"
4 
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <time.h>
8 #include <string.h>
9 #include <stdarg.h>
10 #include <sys/time.h>
11 #include <errno.h>
12 
13 #include "php_syslog.h"
14 
15 #include "zlog.h"
16 #include "fpm.h"
17 #include "zend_portability.h"
18 
19 /* buffer is used for fmt result and it should never be over 2048 */
20 #define MAX_BUF_LENGTH 2048
21 
22 /* maximal length for wrapping prefix */
23 #define MAX_WRAPPING_PREFIX_LENGTH 512
24 
25 #define EXTRA_SPACE_FOR_PREFIX 128
26 
27 static int zlog_fd = -1;
28 static int zlog_level = ZLOG_NOTICE;
29 static int zlog_limit = ZLOG_DEFAULT_LIMIT;
30 static zlog_bool zlog_buffering = ZLOG_DEFAULT_BUFFERING;
31 static int launched = 0;
32 static void (*external_logger)(int, char *, size_t) = NULL;
33 
34 static const char *level_names[] = {
35 	[ZLOG_DEBUG]   = "DEBUG",
36 	[ZLOG_NOTICE]  = "NOTICE",
37 	[ZLOG_WARNING] = "WARNING",
38 	[ZLOG_ERROR]   = "ERROR",
39 	[ZLOG_ALERT]   = "ALERT",
40 };
41 
42 #ifdef HAVE_SYSLOG_H
43 const int syslog_priorities[] = {
44 	[ZLOG_DEBUG]   = LOG_DEBUG,
45 	[ZLOG_NOTICE]  = LOG_NOTICE,
46 	[ZLOG_WARNING] = LOG_WARNING,
47 	[ZLOG_ERROR]   = LOG_ERR,
48 	[ZLOG_ALERT]   = LOG_ALERT,
49 };
50 #endif
51 
zlog_set_external_logger(void (* logger)(int,char *,size_t))52 void zlog_set_external_logger(void (*logger)(int, char *, size_t)) /* {{{ */
53 {
54 	external_logger = logger;
55 }
56 /* }}} */
57 
zlog_get_level_name(int log_level)58 const char *zlog_get_level_name(int log_level) /* {{{ */
59 {
60 	if (log_level < 0) {
61 		log_level = zlog_level;
62 	} else if (log_level < ZLOG_DEBUG || log_level > ZLOG_ALERT) {
63 		return "unknown value";
64 	}
65 
66 	return level_names[log_level];
67 }
68 /* }}} */
69 
zlog_set_launched(void)70 void zlog_set_launched(void) /* {{{ */
71 {
72 	launched = 1;
73 }
74 /* }}} */
75 
zlog_print_time(struct timeval * tv,char * timebuf,size_t timebuf_len)76 size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len) /* {{{ */
77 {
78 	struct tm t;
79 	size_t len;
80 
81 	len = strftime(timebuf, timebuf_len, "[%d-%b-%Y %H:%M:%S",
82 			localtime_r((const time_t *) &tv->tv_sec, &t));
83 	if (zlog_level == ZLOG_DEBUG) {
84 		len += snprintf(timebuf + len, timebuf_len - len, ".%06d", (int) tv->tv_usec);
85 	}
86 	len += snprintf(timebuf + len, timebuf_len - len, "] ");
87 	return len;
88 }
89 /* }}} */
90 
zlog_set_fd(int new_fd)91 int zlog_set_fd(int new_fd) /* {{{ */
92 {
93 	int old_fd = zlog_fd;
94 
95 	zlog_fd = new_fd;
96 	return old_fd;
97 }
98 /* }}} */
99 
zlog_set_level(int new_value)100 int zlog_set_level(int new_value) /* {{{ */
101 {
102 	int old_value = zlog_level;
103 
104 	if (new_value < ZLOG_DEBUG || new_value > ZLOG_ALERT) return old_value;
105 
106 	zlog_level = new_value;
107 	return old_value;
108 }
109 /* }}} */
110 
zlog_set_limit(int new_value)111 int zlog_set_limit(int new_value) /* {{{ */
112 {
113 	int old_value = zlog_limit;
114 
115 	zlog_limit = new_value;
116 	return old_value;
117 }
118 /* }}} */
119 
zlog_set_buffering(zlog_bool buffering)120 int zlog_set_buffering(zlog_bool buffering) /* {{{ */
121 {
122 	int old_value = zlog_buffering;
123 
124 	zlog_buffering = buffering;
125 	return old_value;
126 }
127 /* }}} */
128 
zlog_truncate_buf(char * buf,size_t buf_size,size_t space_left)129 static inline size_t zlog_truncate_buf(char *buf, size_t buf_size, size_t space_left) /* {{{ */
130 {
131 	memcpy(buf + buf_size - sizeof("...") + 1 - space_left, "...", sizeof("...") - 1);
132 	return buf_size - space_left;
133 }
134 /* }}} */
135 
zlog_external(int flags,char * buf,size_t buf_size,const char * fmt,va_list args)136 static inline void zlog_external(
137 		int flags, char *buf, size_t buf_size, const char *fmt, va_list args) /* {{{ */
138 {
139 	va_list ap;
140 	size_t len;
141 
142 	va_copy(ap, args);
143 	len = vsnprintf(buf, buf_size, fmt, ap);
144 	va_end(ap);
145 
146 	if (len >= buf_size) {
147 		len = zlog_truncate_buf(buf, buf_size, 0);
148 	}
149 	external_logger(flags & ZLOG_LEVEL_MASK, buf, len);
150 }
151 /* }}} */
152 
zlog_buf_prefix(const char * function,int line,int flags,char * buf,size_t buf_size,int use_syslog)153 static size_t zlog_buf_prefix(
154 		const char *function, int line, int flags,
155 		char *buf, size_t buf_size, int use_syslog) /* {{{ */
156 {
157 	struct timeval tv;
158 	size_t len = 0;
159 
160 #ifdef HAVE_SYSLOG_H
161 	if (use_syslog /* && !fpm_globals.is_child */) {
162 		if (zlog_level == ZLOG_DEBUG) {
163 			len += snprintf(buf, buf_size, "[%s] %s(), line %d: ",
164 					level_names[flags & ZLOG_LEVEL_MASK], function, line);
165 		} else {
166 			len += snprintf(buf, buf_size, "[%s] ", level_names[flags & ZLOG_LEVEL_MASK]);
167 		}
168 	} else
169 #endif
170 	{
171 		if (!fpm_globals.is_child) {
172 			gettimeofday(&tv, 0);
173 			len = zlog_print_time(&tv, buf, buf_size);
174 		}
175 		if (zlog_level == ZLOG_DEBUG) {
176 			if (!fpm_globals.is_child) {
177 				len += snprintf(buf + len, buf_size - len, "%s: pid %d, %s(), line %d: ",
178 						level_names[flags & ZLOG_LEVEL_MASK], getpid(), function, line);
179 			} else {
180 				len += snprintf(buf + len, buf_size - len, "%s: %s(), line %d: ",
181 						level_names[flags & ZLOG_LEVEL_MASK], function, line);
182 			}
183 		} else {
184 			len += snprintf(buf + len, buf_size - len, "%s: ",
185 					level_names[flags & ZLOG_LEVEL_MASK]);
186 		}
187 	}
188 
189 	return len;
190 }
191 /* }}} */
192 
vzlog(const char * function,int line,int flags,const char * fmt,va_list args)193 void vzlog(const char *function, int line, int flags, const char *fmt, va_list args) /* {{{ */
194 {
195 	char buf[MAX_BUF_LENGTH];
196 	size_t buf_size = MAX_BUF_LENGTH;
197 	size_t len = 0;
198 	int truncated = 0;
199 	int saved_errno;
200 
201 	if (external_logger) {
202 		zlog_external(flags, buf, buf_size, fmt, args);
203 	}
204 
205 	if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
206 		return;
207 	}
208 
209 	saved_errno = errno;
210 	len = zlog_buf_prefix(function, line, flags, buf, buf_size, zlog_fd == ZLOG_SYSLOG);
211 
212 	if (len > buf_size - 1) {
213 		truncated = 1;
214 	} else {
215 		len += vsnprintf(buf + len, buf_size - len, fmt, args);
216 		if (len >= buf_size) {
217 			truncated = 1;
218 		}
219 	}
220 
221 	if (!truncated) {
222 		if (flags & ZLOG_HAVE_ERRNO) {
223 			len += snprintf(buf + len, buf_size - len,
224 					": %s (%d)", strerror(saved_errno), saved_errno);
225 			if (len >= zlog_limit) {
226 				truncated = 1;
227 			}
228 		}
229 	}
230 
231 	if (truncated) {
232 		len = zlog_truncate_buf(buf, zlog_limit < buf_size ? zlog_limit : buf_size, 1);
233 	}
234 
235 #ifdef HAVE_SYSLOG_H
236 	if (zlog_fd == ZLOG_SYSLOG) {
237 		buf[len] = '\0';
238 		php_syslog(syslog_priorities[zlog_level], "%s", buf);
239 		buf[len++] = '\n';
240 	} else
241 #endif
242 	{
243 		buf[len++] = '\n';
244 		zend_quiet_write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
245 	}
246 
247 	if (zlog_fd != STDERR_FILENO && zlog_fd != -1 &&
248 			!launched && (flags & ZLOG_LEVEL_MASK) >= ZLOG_NOTICE) {
249 		zend_quiet_write(STDERR_FILENO, buf, len);
250 	}
251 }
252 /* }}} */
253 
zlog_ex(const char * function,int line,int flags,const char * fmt,...)254 void zlog_ex(const char *function, int line, int flags, const char *fmt, ...) /* {{{ */
255 {
256 	va_list args;
257 	va_start(args, fmt);
258 	vzlog(function, line, flags, fmt, args);
259 	va_end(args);
260 }
261 /* }}} */
262 
263 /* predefine stream init that is used by zlog_msg_ex */
264 static inline void zlog_stream_init_internal(
265 		struct zlog_stream *stream, int flags, size_t capacity, int fd);
266 
zlog_msg_ex(const char * function,int line,int flags,const char * prefix,const char * msg)267 void zlog_msg_ex(const char *function, int line, int flags,
268 		const char *prefix, const char *msg) /* {{{ */
269 {
270 	struct zlog_stream stream;
271 	size_t prefix_len = strlen(prefix);
272 	size_t msg_len = strlen(msg);
273 
274 	zlog_stream_init_internal(&stream, flags, msg_len + prefix_len, 0);
275 	zlog_stream_prefix_ex(&stream, function, line);
276 	zlog_stream_str(&stream, prefix, prefix_len);
277 	zlog_stream_str(&stream, msg, msg_len);
278 	zlog_stream_finish(&stream);
279 	zlog_stream_destroy(&stream);
280 }
281 /* }}} */
282 
283 /* STREAM OPS */
284 
zlog_stream_buf_alloc_ex(struct zlog_stream * stream,size_t needed)285 static zlog_bool zlog_stream_buf_alloc_ex(struct zlog_stream *stream, size_t needed)  /* {{{ */
286 {
287 	char *buf;
288 	size_t size = stream->buf.size ?: stream->buf_init_size;
289 	size = MIN(zlog_limit, MAX((stream->buf.data ? (size << 1) : size), needed));
290 	buf = realloc(stream->buf.data, size);
291 
292 	if (buf == NULL) {
293 		return 0;
294 	}
295 
296 	stream->buf.data = buf;
297 	stream->buf.size = size;
298 
299 	return 1;
300 }
301 /* }}} */
302 
zlog_stream_buf_alloc(struct zlog_stream * stream)303 inline static zlog_bool zlog_stream_buf_alloc(struct zlog_stream *stream)  /* {{{ */
304 {
305 	/* if there is enough space in the buffer, we do not need to reallocate */
306 	if (stream->buf.data && stream->buf.size >= MIN(zlog_limit, stream->buf_init_size)) {
307 		return 1;
308 	}
309 	return zlog_stream_buf_alloc_ex(stream, 0);
310 }
311 /* }}} */
312 
zlog_stream_direct_write_ex(struct zlog_stream * stream,const char * buf,size_t len,const char * append,size_t append_len)313 static inline ssize_t zlog_stream_direct_write_ex(
314 		struct zlog_stream *stream, const char *buf, size_t len,
315 		const char *append, size_t append_len) /* {{{ */
316 {
317 	if (stream->use_fd) {
318 		zend_quiet_write(stream->fd, buf, len);
319 		if (append_len > 0) {
320 			zend_quiet_write(stream->fd, append, append_len);
321 		}
322 	}
323 
324 	if (stream->use_stderr) {
325 		zend_quiet_write(STDERR_FILENO, buf, len);
326 		if (append_len > 0) {
327 			zend_quiet_write(STDERR_FILENO, append, append_len);
328 		}
329 	}
330 
331 	return len;
332 }
333 /* }}} */
334 
zlog_stream_direct_write(struct zlog_stream * stream,const char * buf,size_t len)335 static ssize_t zlog_stream_direct_write(
336 		struct zlog_stream *stream, const char *buf, size_t len) /* {{{ */
337 {
338 	return zlog_stream_direct_write_ex(stream, buf, len, NULL, 0);
339 }
340 /* }}} */
341 
zlog_stream_unbuffered_write(struct zlog_stream * stream,const char * buf,size_t len)342 static inline ssize_t zlog_stream_unbuffered_write(
343 		struct zlog_stream *stream, const char *buf, size_t len) /* {{{ */
344 {
345 	const char *append = NULL;
346 	size_t append_len = 0, required_len, reserved_len;
347 	ssize_t written;
348 
349 	if (stream->len == 0) {
350 		stream->len = zlog_stream_prefix_ex(stream, stream->function, stream->line);
351 	}
352 
353 	/* msg_suffix_len and msg_quote are used only for wrapping */
354 	reserved_len = stream->len + stream->msg_suffix_len + stream->msg_quote;
355 	required_len = reserved_len + len;
356 	if (required_len >= zlog_limit) {
357 		if (stream->wrap) {
358 			size_t available_len;
359 			if (required_len == zlog_limit) {
360 				append = NULL;
361 				append_len = 0;
362 			} else {
363 				append = "\n";
364 				append_len = 1;
365 			}
366 			available_len = zlog_limit - reserved_len - 1;
367 			zlog_stream_direct_write(stream, buf, available_len);
368 			if (append != NULL) {
369 				if (stream->msg_quote) {
370 					zlog_stream_direct_write(stream, "\"", 1);
371 				}
372 				if (stream->msg_suffix) {
373 					zlog_stream_direct_write(stream, stream->msg_suffix, stream->msg_suffix_len);
374 				}
375 				zlog_stream_direct_write(stream, append, append_len);
376 			}
377 			stream->len = 0;
378 			written = zlog_stream_unbuffered_write(
379 					stream, buf + available_len, len - available_len);
380 			if (written > 0) {
381 				return available_len + written;
382 			}
383 
384 			return written;
385 		}
386 		/* this would be used in case of an option for disabling wrapping in direct write */
387 		stream->full = 1;
388 		if (required_len == zlog_limit) {
389 			append = NULL;
390 		} else {
391 			append = "...";
392 			append_len = sizeof("...") - 1;
393 			len = zlog_limit - stream->len - append_len;
394 		}
395 	}
396 
397 	written = zlog_stream_direct_write_ex(stream, buf, len, append, append_len);
398 	if (written > 0) {
399 		/* currently written will be always len as the write is blocking
400 		 * - this should be address if we change to non-blocking write */
401 		stream->len += written;
402 	}
403 
404 	return written;
405 }
406 /* }}} */
407 
zlog_stream_buf_copy_cstr(struct zlog_stream * stream,const char * str,size_t str_len)408 static inline ssize_t zlog_stream_buf_copy_cstr(
409 		struct zlog_stream *stream, const char *str, size_t str_len) /* {{{ */
410 {
411 	if (stream->buf.size - stream->len <= str_len &&
412 			!zlog_stream_buf_alloc_ex(stream, str_len + stream->len)) {
413 		return -1;
414 	}
415 
416 	memcpy(stream->buf.data + stream->len, str, str_len);
417 	stream->len += str_len;
418 
419 	return str_len;
420 }
421 /* }}} */
422 
zlog_stream_buf_copy_char(struct zlog_stream * stream,char c)423 static inline ssize_t zlog_stream_buf_copy_char(struct zlog_stream *stream, char c) /* {{{ */
424 {
425 	if (stream->buf.size - stream->len < 1 && !zlog_stream_buf_alloc_ex(stream, 1)) {
426 		return -1;
427 	}
428 
429 	stream->buf.data[stream->len++] = c;
430 
431 	return 1;
432 }
433 /* }}} */
434 
zlog_stream_buf_flush(struct zlog_stream * stream)435 static ssize_t zlog_stream_buf_flush(struct zlog_stream *stream) /* {{{ */
436 {
437 	ssize_t written;
438 
439 #ifdef HAVE_SYSLOG_H
440 	if (stream->use_syslog) {
441 		zlog_stream_buf_copy_char(stream, '\0');
442 		php_syslog(syslog_priorities[zlog_level], "%s", stream->buf.data);
443 		--stream->len;
444 	}
445 #endif
446 
447 	if (external_logger != NULL) {
448 		external_logger(stream->flags & ZLOG_LEVEL_MASK,
449 				stream->buf.data + stream->prefix_len, stream->len - stream->prefix_len);
450 	}
451 	zlog_stream_buf_copy_char(stream, '\n');
452 	written = zlog_stream_direct_write(stream, stream->buf.data, stream->len);
453 	stream->len = 0;
454 
455 	return written;
456 }
457 /* }}} */
458 
zlog_stream_buf_append(struct zlog_stream * stream,const char * str,size_t str_len)459 static ssize_t zlog_stream_buf_append(
460 		struct zlog_stream *stream, const char *str, size_t str_len)  /* {{{ */
461 {
462 	int over_limit = 0;
463 	size_t available_len, required_len, reserved_len;
464 
465 	if (stream->len == 0) {
466 		stream->len = zlog_stream_prefix_ex(stream, stream->function, stream->line);
467 	}
468 
469 	/* msg_suffix_len and msg_quote are used only for wrapping */
470 	reserved_len = stream->len + stream->msg_suffix_len + stream->msg_quote;
471 	required_len = reserved_len + str_len;
472 	if (required_len >= zlog_limit) {
473 		over_limit = 1;
474 		available_len = zlog_limit - reserved_len - 1;
475 	} else {
476 		available_len = str_len;
477 	}
478 
479 	if (zlog_stream_buf_copy_cstr(stream, str, available_len) < 0) {
480 		return -1;
481 	}
482 
483 	if (!over_limit) {
484 		return available_len;
485 	}
486 
487 	if (stream->wrap) {
488 		if (stream->msg_quote) {
489 			zlog_stream_buf_copy_char(stream, '"');
490 		}
491 		if (stream->msg_suffix != NULL) {
492 			zlog_stream_buf_copy_cstr(stream, stream->msg_suffix, stream->msg_suffix_len);
493 		}
494 		zlog_stream_buf_flush(stream);
495 		zlog_stream_prefix_ex(stream, stream->function, stream->line);
496 		return available_len + zlog_stream_buf_append(
497 				stream, str + available_len, str_len - available_len);
498 	}
499 
500 	stream->len = zlog_truncate_buf(stream->buf.data, stream->len, 0);
501 	stream->full = 1;
502 	return available_len;
503 }
504 /* }}} */
505 
zlog_stream_init_internal(struct zlog_stream * stream,int flags,size_t capacity,int fd)506 static inline void zlog_stream_init_internal(
507 		struct zlog_stream *stream, int flags, size_t capacity, int fd) /* {{{ */
508 {
509 	if (fd == 0) {
510 		fd = zlog_fd;
511 	}
512 
513 	memset(stream, 0, sizeof(struct zlog_stream));
514 	stream->flags = flags;
515 	stream->use_syslog = fd == ZLOG_SYSLOG;
516 	stream->use_fd = fd > 0;
517 	stream->use_buffer = zlog_buffering || external_logger != NULL || stream->use_syslog;
518 	stream->buf_init_size = capacity;
519 	stream->use_stderr = fd < 0 ||
520 			(
521 				fd != STDERR_FILENO && fd != STDOUT_FILENO && !launched &&
522 				(flags & ZLOG_LEVEL_MASK) >= ZLOG_NOTICE
523 			);
524 	stream->prefix_buffer = (flags & ZLOG_LEVEL_MASK) >= zlog_level &&
525 			(stream->use_fd || stream->use_stderr || stream->use_syslog);
526 	stream->fd = fd > -1 ? fd : STDERR_FILENO;
527 }
528 /* }}} */
529 
zlog_stream_init(struct zlog_stream * stream,int flags)530 void zlog_stream_init(struct zlog_stream *stream, int flags) /* {{{ */
531 {
532 	zlog_stream_init_internal(stream, flags, 1024, 0);
533 }
534 /* }}} */
535 
zlog_stream_init_ex(struct zlog_stream * stream,int flags,int fd)536 void zlog_stream_init_ex(struct zlog_stream *stream, int flags, int fd) /* {{{ */
537 {
538 	zlog_stream_init_internal(stream, flags, 1024, fd);
539 	stream->wrap = 1;
540 }
541 /* }}} */
542 
zlog_stream_set_decorating(struct zlog_stream * stream,zlog_bool decorate)543 void zlog_stream_set_decorating(struct zlog_stream *stream, zlog_bool decorate) /* {{{ */
544 {
545 	if (decorate) {
546 		stream->decorate = 1;
547 	} else {
548 		stream->decorate = 0;
549 		stream->msg_quote = 0;
550 		stream->prefix_buffer = 0;
551 	}
552 }
553 /* }}} */
554 
zlog_stream_set_wrapping(struct zlog_stream * stream,zlog_bool wrap)555 void zlog_stream_set_wrapping(struct zlog_stream *stream, zlog_bool wrap) /* {{{ */
556 {
557 	stream->wrap = wrap ? 1 : 0;
558 }
559 /* }}} */
560 
zlog_stream_set_is_stdout(struct zlog_stream * stream,zlog_bool is_stdout)561 void zlog_stream_set_is_stdout(struct zlog_stream *stream, zlog_bool is_stdout) /* {{{ */
562 {
563 	stream->is_stdout = is_stdout ? 1 : 0;
564 }
565 /* }}} */
566 
zlog_stream_set_child_pid(struct zlog_stream * stream,int child_pid)567 void zlog_stream_set_child_pid(struct zlog_stream *stream, int child_pid) /* {{{ */
568 {
569 	stream->child_pid = child_pid;
570 }
571 /* }}} */
572 
zlog_stream_set_msg_quoting(struct zlog_stream * stream,zlog_bool quote)573 void zlog_stream_set_msg_quoting(struct zlog_stream *stream, zlog_bool quote) /* {{{ */
574 {
575 	stream->msg_quote = quote && stream->decorate ? 1 : 0;
576 }
577 /* }}} */
578 
zlog_stream_set_msg_prefix(struct zlog_stream * stream,const char * fmt,...)579 zlog_bool zlog_stream_set_msg_prefix(struct zlog_stream *stream, const char *fmt, ...) /* {{{ */
580 {
581 	char buf[MAX_WRAPPING_PREFIX_LENGTH];
582 	size_t len;
583 	va_list args;
584 
585 	if (!stream->decorate) {
586 		return ZLOG_TRUE;
587 	}
588 
589 	va_start(args, fmt);
590 	len = vsnprintf(buf, MAX_WRAPPING_PREFIX_LENGTH - 1, fmt, args);
591 	va_end(args);
592 
593 	if (stream->msg_prefix_len < len) {
594 		stream->msg_prefix = stream->msg_prefix_len ? realloc(stream->msg_prefix, len + 1) : malloc(len + 1);
595 		if (stream->msg_prefix == NULL) {
596 			return ZLOG_FALSE;
597 		}
598 	}
599 	memcpy(stream->msg_prefix, buf, len);
600 	stream->msg_prefix[len] = 0;
601 	stream->msg_prefix_len = len;
602 
603 	return len;
604 }
605 /* }}} */
606 
zlog_stream_set_msg_suffix(struct zlog_stream * stream,const char * suffix,const char * final_suffix)607 zlog_bool zlog_stream_set_msg_suffix(
608 		struct zlog_stream *stream, const char *suffix, const char *final_suffix)  /* {{{ */
609 {
610 	size_t len;
611 	if (!stream->wrap || !stream->decorate) {
612 		return ZLOG_TRUE;
613 	}
614 
615 	if (suffix != NULL && final_suffix != NULL) {
616 		stream->msg_suffix_len = strlen(suffix);
617 		stream->msg_final_suffix_len = strlen(final_suffix);
618 		len = stream->msg_suffix_len + stream->msg_final_suffix_len + 2;
619 		if (stream->msg_suffix != NULL) {
620 			free(stream->msg_suffix);
621 		}
622 		stream->msg_suffix = malloc(len);
623 		if (stream->msg_suffix == NULL) {
624 			return ZLOG_FALSE;
625 		}
626 		stream->msg_final_suffix = stream->msg_suffix + stream->msg_suffix_len + 1;
627 		memcpy(stream->msg_suffix, suffix, stream->msg_suffix_len + 1);
628 		memcpy(stream->msg_final_suffix, final_suffix, stream->msg_final_suffix_len + 1);
629 		return ZLOG_TRUE;
630 	}
631 	if (suffix != NULL) {
632 		stream->msg_suffix_len = strlen(suffix);
633 		len = stream->msg_suffix_len + 1;
634 		if (stream->msg_suffix != NULL) {
635 			free(stream->msg_suffix);
636 		}
637 		stream->msg_suffix = malloc(len);
638 		if (stream->msg_suffix == NULL) {
639 			return ZLOG_FALSE;
640 		}
641 		memcpy(stream->msg_suffix, suffix, len);
642 		return ZLOG_TRUE;
643 	}
644 	if (final_suffix != NULL) {
645 		stream->msg_final_suffix_len = strlen(final_suffix);
646 		len = stream->msg_final_suffix_len + 1;
647 		if (stream->msg_final_suffix != NULL) {
648 			free(stream->msg_final_suffix);
649 		}
650 		stream->msg_final_suffix = malloc(len);
651 		if (stream->msg_final_suffix == NULL) {
652 			return ZLOG_FALSE;
653 		}
654 		memcpy(stream->msg_final_suffix, final_suffix, len);
655 		return ZLOG_TRUE;
656 	}
657 
658 	return ZLOG_TRUE;
659 }
660 /* }}} */
661 
zlog_stream_prefix_ex(struct zlog_stream * stream,const char * function,int line)662 ssize_t zlog_stream_prefix_ex(struct zlog_stream *stream, const char *function, int line) /* {{{ */
663 {
664 	size_t len;
665 
666 	if (!stream->prefix_buffer) {
667 		return 0;
668 	}
669 	if (stream->wrap && stream->function == NULL) {
670 		stream->function = function;
671 		stream->line = line;
672 	}
673 
674 	if (stream->use_buffer) {
675 		if (!zlog_stream_buf_alloc(stream)) {
676 			return -1;
677 		}
678 		len = zlog_buf_prefix(
679 				function, line, stream->flags,
680 				stream->buf.data, stream->buf.size, stream->use_syslog);
681 		stream->len = stream->prefix_len = len;
682 		if (stream->msg_prefix != NULL) {
683 			zlog_stream_buf_copy_cstr(stream, stream->msg_prefix, stream->msg_prefix_len);
684 		}
685 		if (stream->msg_quote) {
686 			zlog_stream_buf_copy_char(stream, '"');
687 		}
688 		return stream->len;
689 	} else {
690 		char sbuf[1024];
691 		ssize_t written;
692 		len = zlog_buf_prefix(function, line, stream->flags, sbuf, 1024, stream->use_syslog);
693 		written = zlog_stream_direct_write(stream, sbuf, len);
694 		if (stream->msg_prefix != NULL) {
695 			written += zlog_stream_direct_write(
696 					stream, stream->msg_prefix, stream->msg_prefix_len);
697 		}
698 		if (stream->msg_quote) {
699 			written += zlog_stream_direct_write(stream, "\"", 1);
700 		}
701 		return written;
702 	}
703 }
704 /* }}} */
705 
zlog_stream_vformat(struct zlog_stream * stream,const char * fmt,va_list args)706 ssize_t zlog_stream_vformat(struct zlog_stream *stream, const char *fmt, va_list args) /* {{{ */
707 {
708 	char sbuf[1024];
709 	size_t len;
710 
711 	len = vsnprintf(sbuf, 1024, fmt, args);
712 
713 	return zlog_stream_str(stream, sbuf, len);
714 }
715 /* }}} */
716 
zlog_stream_format(struct zlog_stream * stream,const char * fmt,...)717 ssize_t zlog_stream_format(struct zlog_stream *stream, const char *fmt, ...) /* {{{ */
718 {
719 	ssize_t len;
720 
721 	va_list args;
722 	va_start(args, fmt);
723 	len = zlog_stream_vformat(stream, fmt, args);
724 	va_end(args);
725 
726 	return len;
727 }
728 /* }}} */
729 
zlog_stream_str(struct zlog_stream * stream,const char * str,size_t str_len)730 ssize_t zlog_stream_str(struct zlog_stream *stream, const char *str, size_t str_len) /* {{{ */
731 {
732 	/* do not write anything if the stream is full or str is empty */
733 	if (str_len == 0 || stream->full) {
734 		return 0;
735 	}
736 
737 	/* reset stream if it is finished */
738 	if (stream->finished) {
739 		stream->finished = 0;
740 		stream->len = 0;
741 		stream->full = 0;
742 	}
743 
744 	if (stream->use_buffer) {
745 		return zlog_stream_buf_append(stream, str, str_len);
746 	}
747 
748 	return zlog_stream_unbuffered_write(stream, str, str_len);
749 }
750 /* }}} */
751 
zlog_stream_finish_buffer_suffix(struct zlog_stream * stream)752 static inline void zlog_stream_finish_buffer_suffix(struct zlog_stream *stream) /* {{{ */
753 {
754 	if (stream->msg_quote) {
755 		zlog_stream_buf_copy_char(stream, '"');
756 	}
757 	if (stream->msg_suffix != NULL) {
758 		zlog_stream_buf_copy_cstr(stream, stream->msg_suffix, stream->msg_suffix_len);
759 	}
760 	if (stream->msg_final_suffix != NULL) {
761 		if (stream->len + stream->msg_final_suffix_len >= zlog_limit) {
762 			zlog_bool quoting = stream->msg_quote;
763 			size_t final_suffix_wrap = stream->len + stream->msg_final_suffix_len + 1 - zlog_limit;
764 			zlog_stream_buf_copy_cstr(
765 					stream, stream->msg_final_suffix,
766 					stream->msg_final_suffix_len - final_suffix_wrap);
767 			zlog_stream_buf_copy_char(stream, '\n');
768 			zlog_stream_buf_flush(stream);
769 			stream->msg_quote = 0;
770 			zlog_stream_prefix_ex(stream, stream->function, stream->line);
771 			stream->msg_quote = quoting;
772 			zlog_stream_buf_copy_cstr(
773 					stream,
774 					stream->msg_final_suffix + (stream->msg_final_suffix_len - final_suffix_wrap),
775 					final_suffix_wrap);
776 			zlog_stream_buf_copy_char(stream, '\n');
777 		} else {
778 			zlog_stream_buf_copy_cstr(
779 					stream, stream->msg_final_suffix, stream->msg_final_suffix_len);
780 		}
781 	}
782 }
783 /* }}} */
784 
zlog_stream_finish_direct_suffix(struct zlog_stream * stream)785 static inline void zlog_stream_finish_direct_suffix(struct zlog_stream *stream) /* {{{ */
786 {
787 	if (stream->msg_quote) {
788 		zlog_stream_direct_write(stream, "\"", 1);
789 		++stream->len;
790 	}
791 	if (stream->msg_suffix != NULL) {
792 		/* we should always have space for wrap suffix so we don't have to check it */
793 		zlog_stream_direct_write(stream, stream->msg_suffix, stream->msg_suffix_len);
794 		stream->len += stream->msg_suffix_len;
795 	}
796 	if (stream->msg_final_suffix != NULL) {
797 		if (stream->len + stream->msg_final_suffix_len >= zlog_limit) {
798 			zlog_bool quoting = stream->msg_quote;
799 			size_t final_suffix_wrap = stream->len + stream->msg_final_suffix_len + 1 - zlog_limit;
800 			zlog_stream_direct_write_ex(
801 					stream, stream->msg_final_suffix,
802 					stream->msg_final_suffix_len - final_suffix_wrap, "\n", 1);
803 			stream->msg_quote = 0;
804 			zlog_stream_prefix_ex(stream, stream->function, stream->line);
805 			stream->msg_quote = quoting;
806 			zlog_stream_direct_write_ex(
807 					stream,
808 					stream->msg_final_suffix + (stream->msg_final_suffix_len - final_suffix_wrap),
809 					final_suffix_wrap, "\n", 1);
810 		} else {
811 			zlog_stream_direct_write_ex(
812 					stream, stream->msg_final_suffix, stream->msg_final_suffix_len, "\n", 1);
813 		}
814 	} else {
815 		zlog_stream_direct_write(stream, "\n", 1);
816 	}
817 }
818 /* }}} */
819 
zlog_stream_finish(struct zlog_stream * stream)820 zlog_bool zlog_stream_finish(struct zlog_stream *stream) /* {{{ */
821 {
822 	if (stream->finished || stream->len == 0) {
823 		return ZLOG_TRUE;
824 	}
825 
826 	if (stream->use_buffer) {
827 		if (stream->decorate) {
828 			zlog_stream_finish_buffer_suffix(stream);
829 		}
830 		zlog_stream_buf_flush(stream);
831 	} else {
832 		if (stream->decorate) {
833 			zlog_stream_finish_direct_suffix(stream);
834 		} else {
835 			zlog_stream_direct_write(stream, "\n", 1);
836 		}
837 	}
838 	stream->finished = 1;
839 
840 	return ZLOG_TRUE;
841 }
842 /* }}} */
843 
zlog_stream_destroy(struct zlog_stream * stream)844 void zlog_stream_destroy(struct zlog_stream *stream) /* {{{ */
845 {
846 	if (stream->buf.data != NULL) {
847 		free(stream->buf.data);
848 	}
849 	if (stream->msg_prefix != NULL) {
850 		free(stream->msg_prefix);
851 	}
852 	if (stream->msg_suffix != NULL) {
853 		free(stream->msg_suffix);
854 	} else if (stream->msg_final_suffix != NULL) {
855 		free(stream->msg_final_suffix);
856 	}
857 }
858 /* }}} */
859 
zlog_stream_close(struct zlog_stream * stream)860 zlog_bool zlog_stream_close(struct zlog_stream *stream) /* {{{ */
861 {
862 	zlog_bool finished = zlog_stream_finish(stream);
863 	zlog_stream_destroy(stream);
864 
865 	return finished;
866 }
867 /* }}} */
868