|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
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"
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. | |
Request logging middleware — text and JSON structured formats.
Uses the csilk reflection engine for JSON serialization of request log entries.
| 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 |
| #define REQ_LOG_MAP | ( | X | ) |
| 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.
| c | The request context. |