/* * Calculate the total size of the logbuf and all chained buffers. */ ssize_t logbuf_size(logbuf_t *lb) { ssize_t sz; sz = lb->sz; if (lb->next) { sz += logbuf_size(lb->next); } return sz; }
void log_content_submit(log_content_ctx_t *ctx, logbuf_t *lb, int direction) { logbuf_t *head; time_t epoch; struct tm *utc; char *header; if (!ctx->open) { log_err_printf("log_content_submit called on closed ctx\n"); return; } if (!(header = direction ? ctx->header_out : ctx->header_in)) goto out; /* prepend size tag and newline */ head = logbuf_new_printf(lb->fd, lb, " (%zu):\n", logbuf_size(lb)); if (!head) { log_err_printf("Failed to allocate memory\n"); logbuf_free(lb); return; } lb = head; /* prepend header */ head = logbuf_new_copy(header, strlen(header), lb->fd, lb); if (!head) { log_err_printf("Failed to allocate memory\n"); logbuf_free(lb); return; } lb = head; /* prepend timestamp */ head = logbuf_new_alloc(32, lb->fd, lb); if (!head) { log_err_printf("Failed to allocate memory\n"); logbuf_free(lb); return; } lb = head; time(&epoch); utc = gmtime(&epoch); lb->sz = strftime((char*)lb->buf, lb->sz, "%Y-%m-%d %H:%M:%S UTC ", utc); out: lb->fd = ctx->fd; logger_submit(content_log, lb); }
static logbuf_t * log_content_file_prepcb(void *fh, unsigned long prepflags, logbuf_t *lb) { log_content_ctx_t *ctx = fh; int is_request = !!(prepflags & PREPFLAG_REQUEST); logbuf_t *head; time_t epoch; struct tm *utc; char *header; if (!(header = is_request ? ctx->u.file.header_req : ctx->u.file.header_resp)) goto out; /* prepend size tag and newline */ head = logbuf_new_printf(lb->fh, lb, " (%zu):\n", logbuf_size(lb)); if (!head) { log_err_printf("Failed to allocate memory\n"); logbuf_free(lb); return NULL; } lb = head; /* prepend header */ head = logbuf_new_copy(header, strlen(header), lb->fh, lb); if (!head) { log_err_printf("Failed to allocate memory\n"); logbuf_free(lb); return NULL; } lb = head; /* prepend timestamp */ head = logbuf_new_alloc(32, lb->fh, lb); if (!head) { log_err_printf("Failed to allocate memory\n"); logbuf_free(lb); return NULL; } lb = head; time(&epoch); utc = gmtime(&epoch); lb->sz = strftime((char*)lb->buf, lb->sz, "%Y-%m-%d %H:%M:%S UTC ", utc); out: return lb; }