Skip to content

Commit a26eb25

Browse files
committed
Implement wrapping to the unbuffered write
1 parent cd8adbe commit a26eb25

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

sapi/fpm/fpm/zlog.c

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,18 @@ inline static zlog_bool zlog_stream_buf_alloc(struct zlog_stream *stream) /* {{
293293
/* TODO: consider better handling of errors and do not use zend_quiet_write */
294294
static inline ssize_t zlog_stream_direct_write_ex(
295295
struct zlog_stream *stream, const char *buf, size_t len,
296-
int finished, const char *append, size_t append_len) /* {{{ */
296+
const char *append, size_t append_len) /* {{{ */
297297
{
298298
if (stream->use_fd) {
299299
zend_quiet_write(stream->fd, buf, len);
300-
if (finished) {
300+
if (append_len > 0) {
301301
zend_quiet_write(stream->fd, append, append_len);
302302
}
303303
}
304304

305305
if (stream->use_stderr) {
306306
zend_quiet_write(STDERR_FILENO, buf, len);
307-
if (finished) {
307+
if (append_len > 0) {
308308
zend_quiet_write(STDERR_FILENO, append, append_len);
309309
}
310310
}
@@ -315,7 +315,7 @@ static inline ssize_t zlog_stream_direct_write_ex(
315315

316316
static ssize_t zlog_stream_direct_write(struct zlog_stream *stream, const char *buf, size_t len) /* {{{ */
317317
{
318-
return zlog_stream_direct_write_ex(stream, buf, len, 0, NULL, 0);
318+
return zlog_stream_direct_write_ex(stream, buf, len, NULL, 0);
319319
}
320320
/* }}} */
321321

@@ -326,14 +326,36 @@ static inline ssize_t zlog_stream_unbuffered_write(struct zlog_stream *stream, c
326326
size_t append_len;
327327

328328
if (stream->len + len >= zlog_limit) {
329-
/* TODO: handle wrapping */
329+
if (stream->wrap) {
330+
if (stream->wrap_prefix != NULL) {
331+
zlog_stream_direct_write_ex(
332+
stream, stream->wrap_prefix, stream->wrap_prefix_len, NULL, 0);
333+
}
334+
len = zlog_limit - stream->len;
335+
if (stream->len + len == zlog_limit) {
336+
append = NULL;
337+
append_len = 0;
338+
} else {
339+
append = "\n";
340+
append_len = 1;
341+
}
342+
if (stream->wrap_suffix) {
343+
zlog_stream_direct_write(stream, buf, len);
344+
zlog_stream_direct_write_ex(
345+
stream, stream->wrap_suffix, stream->wrap_suffix_len, append, append_len);
346+
} else {
347+
zlog_stream_direct_write_ex(stream, buf, len, append, append_len);
348+
}
349+
/* TODO: use loop to speed it up */
350+
return len + zlog_stream_unbuffered_write(stream, buf + len, len);
351+
}
330352
stream->finished = finished = 1;
331353
append = (stream->len + len == zlog_limit) ? "\n" : "...\n";
332354
append_len = sizeof(append) - 1;
333355
len = zlog_limit - stream->len - append_len;
334356
}
335357

336-
return zlog_stream_direct_write_ex(stream, buf, len, finished, append, append_len);
358+
return zlog_stream_direct_write_ex(stream, buf, len, append, append_len);
337359
}
338360
/* }}} */
339361

@@ -373,10 +395,13 @@ static ssize_t zlog_stream_buf_append(struct zlog_stream *stream, const char *st
373395
if (stream->wrap) {
374396
if (stream->wrap_suffix != NULL) {
375397
zlog_stream_buf_copy(stream, stream->wrap_suffix, stream->wrap_suffix_len);
398+
zlog_stream_buf_copy(stream, "\n", 1);
376399
}
400+
/* TODO: replace with proper write as it is in the finish (syslog and external logging) */
377401
zlog_stream_direct_write(stream, stream->buf, stream->len);
378402
stream->len = 0;
379403
zlog_stream_prefix_ex(stream, stream->function, stream->line);
404+
/* TODO: use loop to speed it up */
380405
return available_len + zlog_stream_buf_append(stream, str + available_len, str_len - available_len);
381406
}
382407

@@ -578,7 +603,12 @@ zlog_bool zlog_stream_finish(struct zlog_stream *stream) /* {{{ */
578603
stream->buf[stream->len++] = '\n';
579604
zlog_stream_direct_write(stream, stream->buf, stream->len);
580605
} else if (!stream->finished) {
581-
zlog_stream_direct_write(stream, "\n", 1);
606+
if (stream->wrap_final_suffix != NULL) {
607+
zlog_stream_direct_write_ex(
608+
stream, stream->wrap_final_suffix, stream->wrap_final_suffix_len, "\n", 1);
609+
} else {
610+
zlog_stream_direct_write(stream, "\n", 1);
611+
}
582612
}
583613

584614
stream->finished = 1;

0 commit comments

Comments
 (0)