20#define MAX_BUF_LENGTH 2048
23#define MAX_WRAPPING_PREFIX_LENGTH 512
25#define EXTRA_SPACE_FOR_PREFIX 128
27static int zlog_fd = -1;
28static bool zlog_fd_is_stderr =
false;
32static int launched = 0;
33static void (*external_logger)(int,
char *, size_t) =
NULL;
35static const char *level_names[] = {
44const int syslog_priorities[] = {
55 external_logger = logger;
62 log_level = zlog_level;
63 }
else if (log_level < ZLOG_DEBUG || log_level >
ZLOG_ALERT) {
64 return "unknown value";
67 return level_names[log_level];
82 len =
strftime(timebuf, timebuf_len,
"[%d-%b-%Y %H:%M:%S",
83 localtime_r((
const time_t *) &
tv->tv_sec, &t));
97 zlog_fd_is_stderr = is_stderr;
105 int old_value = zlog_level;
107 if (new_value < ZLOG_DEBUG || new_value >
ZLOG_ALERT)
return old_value;
109 zlog_level = new_value;
116 int old_value = zlog_limit;
118 zlog_limit = new_value;
125 int old_value = zlog_buffering;
127 zlog_buffering = buffering;
132static inline size_t zlog_truncate_buf(
char *
buf,
size_t buf_size,
size_t space_left)
134 memcpy(
buf + buf_size -
sizeof(
"...") + 1 - space_left,
"...",
sizeof(
"...") - 1);
135 return buf_size - space_left;
139static inline void zlog_external(
140 int flags,
char *
buf,
size_t buf_size,
const char *fmt, va_list
args)
149 if (
len >= buf_size) {
150 len = zlog_truncate_buf(
buf, buf_size, 0);
157static size_t zlog_buf_prefix(
159 char *
buf,
size_t buf_size,
int use_syslog)
206 if (external_logger) {
217 if (
len > buf_size - 1) {
221 if (
len >= buf_size) {
229 ": %s (%d)", strerror(saved_errno), saved_errno);
230 if (
len >= zlog_limit) {
237 len = zlog_truncate_buf(
buf, zlog_limit < buf_size ? zlog_limit : buf_size, 1);
252 if (!zlog_fd_is_stderr && zlog_fd != -1 &&
269static inline void zlog_stream_init_internal(
314 return zlog_stream_buf_alloc_ex(stream, 0);
318static inline ssize_t zlog_stream_direct_write_ex(
320 const char *append,
size_t append_len)
324 if (append_len > 0) {
331 if (append_len > 0) {
340static ssize_t zlog_stream_direct_write(
343 return zlog_stream_direct_write_ex(stream,
buf,
len,
NULL, 0);
347static inline ssize_t zlog_stream_unbuffered_write(
350 const char *append =
NULL;
351 size_t append_len = 0, required_len, reserved_len;
354 if (stream->
len == 0) {
360 required_len = reserved_len +
len;
361 if (required_len >= zlog_limit) {
363 size_t available_len;
364 if (required_len == zlog_limit) {
371 available_len = zlog_limit - reserved_len - 1;
372 zlog_stream_direct_write(stream,
buf, available_len);
373 if (append !=
NULL) {
375 zlog_stream_direct_write(stream,
"\"", 1);
380 zlog_stream_direct_write(stream, append, append_len);
383 written = zlog_stream_unbuffered_write(
384 stream,
buf + available_len,
len - available_len);
386 return available_len + written;
393 if (required_len == zlog_limit) {
397 append_len =
sizeof(
"...") - 1;
398 len = zlog_limit - stream->
len - append_len;
402 written = zlog_stream_direct_write_ex(stream,
buf,
len, append, append_len);
406 stream->
len += written;
413static inline ssize_t zlog_stream_buf_copy_cstr(
414 struct zlog_stream *stream,
const char *str,
size_t str_len)
417 if (stream->
buf.
size - stream->
len <= str_len &&
418 !zlog_stream_buf_alloc_ex(stream, str_len + stream->
len)) {
423 stream->
len += str_len;
429static inline ssize_t zlog_stream_buf_copy_char(
struct zlog_stream *stream,
char c)
432 if (stream->
buf.
size - stream->
len < 1 && !zlog_stream_buf_alloc_ex(stream, 1)) {
442static ssize_t zlog_stream_buf_flush(
struct zlog_stream *stream)
448 zlog_stream_buf_copy_char(stream,
'\0');
454 if (external_logger !=
NULL) {
458 zlog_stream_buf_copy_char(stream,
'\n');
459 written = zlog_stream_direct_write(stream, stream->
buf.
data, stream->
len);
466static ssize_t zlog_stream_buf_append(
467 struct zlog_stream *stream,
const char *str,
size_t str_len)
470 size_t available_len, required_len, reserved_len;
472 if (stream->
len == 0) {
478 required_len = reserved_len + str_len;
479 if (required_len >= zlog_limit) {
481 available_len = zlog_limit - reserved_len - 1;
483 available_len = str_len;
486 if (zlog_stream_buf_copy_cstr(stream, str, available_len) < 0) {
491 return available_len;
496 zlog_stream_buf_copy_char(stream,
'"');
501 zlog_stream_buf_flush(stream);
503 return available_len + zlog_stream_buf_append(
504 stream, str + available_len, str_len - available_len);
507 stream->
len = zlog_truncate_buf(stream->
buf.
data, stream->
len, 0);
509 return available_len;
513static inline void zlog_stream_init_internal(
539 zlog_stream_init_internal(stream,
flags, 1024, 0);
545 zlog_stream_init_internal(stream,
flags, 1024,
fd);
615 struct zlog_stream *stream,
const char *suffix,
const char *final_suffix)
622 if (suffix !=
NULL && final_suffix !=
NULL) {
638 if (suffix !=
NULL) {
651 if (final_suffix !=
NULL) {
682 if (!zlog_stream_buf_alloc(stream)) {
685 len = zlog_buf_prefix(
692 if (!zlog_stream_buf_alloc_ex(stream,
len)) {
704 zlog_stream_buf_copy_char(stream,
'"');
711 written = zlog_stream_direct_write(stream, sbuf,
MIN(
len,
sizeof(sbuf)));
713 written += zlog_stream_direct_write(
717 written += zlog_stream_direct_write(stream,
"\"", 1);
751 if (str_len == 0 || stream->
full) {
763 return zlog_stream_buf_append(stream, str, str_len);
766 return zlog_stream_unbuffered_write(stream, str, str_len);
770static inline void zlog_stream_finish_buffer_suffix(
struct zlog_stream *stream)
773 zlog_stream_buf_copy_char(stream,
'"');
782 zlog_stream_buf_copy_cstr(
785 zlog_stream_buf_copy_char(stream,
'\n');
786 zlog_stream_buf_flush(stream);
790 zlog_stream_buf_copy_cstr(
794 zlog_stream_buf_copy_char(stream,
'\n');
796 zlog_stream_buf_copy_cstr(
803static inline void zlog_stream_finish_direct_suffix(
struct zlog_stream *stream)
806 zlog_stream_direct_write(stream,
"\"", 1);
818 zlog_stream_direct_write_ex(
824 zlog_stream_direct_write_ex(
827 final_suffix_wrap,
"\n", 1);
829 zlog_stream_direct_write_ex(
833 zlog_stream_direct_write(stream,
"\n", 1);
846 zlog_stream_finish_buffer_suffix(stream);
848 zlog_stream_buf_flush(stream);
851 zlog_stream_finish_direct_suffix(stream);
853 zlog_stream_direct_write(stream,
"\n", 1);
gettimeofday(bool $as_float=false)
memset(ptr, 0, type->size)
zend_ffi_ctype_name_buf buf
struct fpm_globals_s fpm_globals
strftime(string $format, ?int $timestamp=null)
PHPAPI void php_syslog(int priority, const char *format,...)
unsigned int prefix_buffer
struct zlog_stream_buffer buf
size_t msg_final_suffix_len
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
#define zend_quiet_write(...)
#define EXPECTED(condition)
function(EX_VAR(opline->result.var))
zlog_bool zlog_stream_finish(struct zlog_stream *stream)
void zlog_set_launched(void)
ssize_t zlog_stream_str(struct zlog_stream *stream, const char *str, size_t str_len)
void zlog_stream_init_ex(struct zlog_stream *stream, int flags, int fd)
#define MAX_WRAPPING_PREFIX_LENGTH
void zlog_msg_ex(const char *function, int line, int flags, const char *prefix, const char *msg)
int zlog_set_limit(int new_value)
void zlog_stream_init(struct zlog_stream *stream, int flags)
zlog_bool zlog_stream_set_msg_prefix(struct zlog_stream *stream, const char *fmt,...)
void zlog_stream_set_child_pid(struct zlog_stream *stream, int child_pid)
int zlog_set_level(int new_value)
zlog_bool zlog_stream_close(struct zlog_stream *stream)
void zlog_stream_set_msg_quoting(struct zlog_stream *stream, zlog_bool quote)
ssize_t zlog_stream_prefix_ex(struct zlog_stream *stream, const char *function, int line)
zlog_bool zlog_stream_set_msg_suffix(struct zlog_stream *stream, const char *suffix, const char *final_suffix)
void zlog_stream_set_decorating(struct zlog_stream *stream, zlog_bool decorate)
void zlog_stream_set_is_stdout(struct zlog_stream *stream, zlog_bool is_stdout)
void zlog_ex(const char *function, int line, int flags, const char *fmt,...)
void zlog_stream_destroy(struct zlog_stream *stream)
const char * zlog_get_level_name(int log_level)
ssize_t zlog_stream_vformat(struct zlog_stream *stream, const char *fmt, va_list args)
void zlog_set_external_logger(void(*logger)(int, char *, size_t))
void vzlog(const char *function, int line, int flags, const char *fmt, va_list args)
ssize_t zlog_stream_format(struct zlog_stream *stream, const char *fmt,...)
void zlog_stream_set_wrapping(struct zlog_stream *stream, zlog_bool wrap)
size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len)
int zlog_set_fd(int new_fd, zlog_bool is_stderr)
int zlog_set_buffering(zlog_bool buffering)
#define ZLOG_DEFAULT_LIMIT
#define ZLOG_DEFAULT_BUFFERING