mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-07 11:45:36 -05:00
info-level logging for each request
This commit is contained in:
parent
aabfa173c4
commit
cbc5761904
4 changed files with 33 additions and 10 deletions
|
|
@ -4,3 +4,4 @@ let info _ = ()
|
||||||
let debug _ = ()
|
let debug _ = ()
|
||||||
let error _ = ()
|
let error _ = ()
|
||||||
let enable ~debug:_ () = ()
|
let enable ~debug:_ () = ()
|
||||||
|
let dummy = true
|
||||||
|
|
|
||||||
|
|
@ -14,3 +14,5 @@ let setup ~debug () =
|
||||||
Logs.Debug
|
Logs.Debug
|
||||||
else
|
else
|
||||||
Logs.Info))
|
Logs.Info))
|
||||||
|
|
||||||
|
let dummy = false
|
||||||
|
|
|
||||||
|
|
@ -8,3 +8,5 @@ val setup : debug:bool -> unit -> unit
|
||||||
(** Setup and enable logging. This should only ever be used in executables,
|
(** Setup and enable logging. This should only ever be used in executables,
|
||||||
not libraries.
|
not libraries.
|
||||||
@param debug if true, set logging to debug (otherwise info) *)
|
@param debug if true, set logging to debug (otherwise info) *)
|
||||||
|
|
||||||
|
val dummy : bool
|
||||||
|
|
|
||||||
|
|
@ -844,6 +844,11 @@ let create_from ?(buf_size = 16 * 1_024) ?(middlewares = []) ~backend () : t =
|
||||||
|
|
||||||
let is_ipv6_str addr : bool = String.contains addr ':'
|
let is_ipv6_str addr : bool = String.contains addr ':'
|
||||||
|
|
||||||
|
let str_of_sockaddr = function
|
||||||
|
| Unix.ADDR_UNIX f -> f
|
||||||
|
| Unix.ADDR_INET (inet, port) ->
|
||||||
|
Printf.sprintf "%s:%d" (Unix.string_of_inet_addr inet) port
|
||||||
|
|
||||||
module Unix_tcp_server_ = struct
|
module Unix_tcp_server_ = struct
|
||||||
type t = {
|
type t = {
|
||||||
addr: string;
|
addr: string;
|
||||||
|
|
@ -858,11 +863,6 @@ module Unix_tcp_server_ = struct
|
||||||
mutable running: bool; (* TODO: use an atomic? *)
|
mutable running: bool; (* TODO: use an atomic? *)
|
||||||
}
|
}
|
||||||
|
|
||||||
let str_of_sockaddr = function
|
|
||||||
| Unix.ADDR_UNIX f -> f
|
|
||||||
| Unix.ADDR_INET (inet, port) ->
|
|
||||||
Printf.sprintf "%s:%d" (Unix.string_of_inet_addr inet) port
|
|
||||||
|
|
||||||
let to_tcp_server (self : t) : IO.TCP_server.builder =
|
let to_tcp_server (self : t) : IO.TCP_server.builder =
|
||||||
{
|
{
|
||||||
IO.TCP_server.serve =
|
IO.TCP_server.serve =
|
||||||
|
|
@ -1044,6 +1044,16 @@ let client_handle_for (self : t) ~client_addr ic oc : unit =
|
||||||
|
|
||||||
if Request.close_after_req req then continue := false;
|
if Request.close_after_req req then continue := false;
|
||||||
|
|
||||||
|
(* how to log the response to this query *)
|
||||||
|
let log_response (resp : Response.t) =
|
||||||
|
if not Log.dummy then
|
||||||
|
Log.info (fun k ->
|
||||||
|
let elapsed = B.get_time_s () -. req.start_time in
|
||||||
|
k "response to=%s code=%d time=%.3fs"
|
||||||
|
(str_of_sockaddr client_addr)
|
||||||
|
resp.code elapsed)
|
||||||
|
in
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(* is there a handler for this path? *)
|
(* is there a handler for this path? *)
|
||||||
let base_handler =
|
let base_handler =
|
||||||
|
|
@ -1081,6 +1091,7 @@ let client_handle_for (self : t) ~client_addr ic oc : unit =
|
||||||
try
|
try
|
||||||
if Headers.get "connection" r.Response.headers = Some "close" then
|
if Headers.get "connection" r.Response.headers = Some "close" then
|
||||||
continue := false;
|
continue := false;
|
||||||
|
log_response r;
|
||||||
Response.output_ ~buf:buf_res oc r
|
Response.output_ ~buf:buf_res oc r
|
||||||
with Sys_error _ -> continue := false
|
with Sys_error _ -> continue := false
|
||||||
in
|
in
|
||||||
|
|
@ -1088,15 +1099,22 @@ let client_handle_for (self : t) ~client_addr ic oc : unit =
|
||||||
(* call handler *)
|
(* call handler *)
|
||||||
try handler oc req ~resp with Sys_error _ -> continue := false
|
try handler oc req ~resp with Sys_error _ -> continue := false
|
||||||
with
|
with
|
||||||
| Sys_error _ -> continue := false
|
| Sys_error _ ->
|
||||||
(* connection broken somehow *)
|
(* connection broken somehow *)
|
||||||
|
Log.debug (fun k -> k "connection broken");
|
||||||
|
continue := false
|
||||||
| Bad_req (code, s) ->
|
| Bad_req (code, s) ->
|
||||||
continue := false;
|
continue := false;
|
||||||
Response.output_ ~buf:buf_res oc @@ Response.make_raw ~code s
|
let resp = Response.make_raw ~code s in
|
||||||
|
log_response resp;
|
||||||
|
Response.output_ ~buf:buf_res oc resp
|
||||||
| e ->
|
| e ->
|
||||||
continue := false;
|
continue := false;
|
||||||
Response.output_ ~buf:buf_res oc
|
let resp =
|
||||||
@@ Response.fail ~code:500 "server error: %s" (Printexc.to_string e))
|
Response.fail ~code:500 "server error: %s" (Printexc.to_string e)
|
||||||
|
in
|
||||||
|
log_response resp;
|
||||||
|
Response.output_ ~buf:buf_res oc resp)
|
||||||
done
|
done
|
||||||
|
|
||||||
let client_handler (self : t) : IO.TCP_server.conn_handler =
|
let client_handler (self : t) : IO.TCP_server.conn_handler =
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue