Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
logger.c File Reference

Request logging middleware — text and JSON structured formats. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "csilk/core/internal.h"
#include "csilk/csilk.h"
#include "csilk/reflection/reflect.h"
Include dependency graph for logger.c:

Data Structures

struct  csilk_req_log_t
 Request log entry for JSON structured logging. More...
 

Macros

#define REQ_LOG_MAP(X)
 

Functions

void csilk_logger_handler (csilk_ctx_t *c)
 Request logging middleware — logs method, path, status, and duration.
 

Detailed Description

Request logging middleware — text and JSON structured formats.

Uses the csilk reflection engine for JSON serialization of request log entries.


Data Structure Documentation

◆ csilk_req_log_t

struct csilk_req_log_t

Request log entry for JSON structured logging.

This struct captures the essential fields of an HTTP request for log output. It is registered with the CSILK reflection engine (via CSILK_REGISTER_REFLECT) so it can be automatically marshalled to JSON for structured log entries.

Data Fields
double duration

Request duration in seconds

char method[8]

HTTP method

char path[256]

Request path

char remote_addr[46]

Client IP address

int32_t status

Response status code

Macro Definition Documentation

◆ REQ_LOG_MAP

#define REQ_LOG_MAP (   X)
Value:
X(csilk_req_log_t, method, CSILK_TYPE_STRING, 8, 0, false, NULL) \
X(csilk_req_log_t, path, CSILK_TYPE_STRING, 256, 0, false, NULL) \
X(csilk_req_log_t, status, CSILK_TYPE_INT32, sizeof(int32_t), 0, false, NULL) \
X(csilk_req_log_t, duration, CSILK_TYPE_DOUBLE, sizeof(double), 0, false, NULL) \
X(csilk_req_log_t, remote_addr, CSILK_TYPE_STRING, 46, 0, false, NULL)
Request log entry for JSON structured logging.
Definition logger.c:35
@ CSILK_TYPE_INT32
Definition reflect.h:34
@ CSILK_TYPE_STRING
Definition reflect.h:42
@ CSILK_TYPE_DOUBLE
Definition reflect.h:40

Function Documentation

◆ csilk_logger_handler()

void csilk_logger_handler ( csilk_ctx_t *  c)

Request logging middleware — logs method, path, status, and duration.

Logging middleware handler. Logs request method, path, and processing time.

Records the start time of the request using a high-resolution monotonic clock (CLOCK_MONOTONIC), proceeds to the next handler via csilk_next(), and then calculates the total elapsed time. It automatically selects between plain-text logging and structured JSON logging based on the global logger configuration.

In JSON mode, the request details are marshalled into a csilk_req_log_t struct and logged via the reflection-based JSON logger. In text mode, a simple "[HTTP] METHOD PATH STATUS DURATION" line is emitted.

If a request ID is set on the context, it is propagated to the logger's thread-local state for correlating log entries.

Parameters
cThe request context.
Note
This middleware should be one of the first in the pipeline so that the timing covers the entire request lifecycle.
Warning
csilk_next() is called BEFORE the log output; the response status must therefore be set by downstream handlers before returning.