From 7668522147126e620258e6d22c16a2b0fa56feb4 Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Fri, 16 Apr 2004 08:18:07 +0000 Subject: [PATCH] - rewritten http_getter logger interface --- helm/ocaml/getter/.depend | 28 ++++++++----------- helm/ocaml/getter/Makefile | 2 +- helm/ocaml/getter/http_getter.ml | 7 ++--- helm/ocaml/getter/http_getter_cache.ml | 28 +++++++++++-------- helm/ocaml/getter/http_getter_env.ml | 4 +++ ...tter_debugger.ml => http_getter_logger.ml} | 16 ++++++----- ...er_debugger.mli => http_getter_logger.mli} | 21 +++++++++----- helm/ocaml/getter/http_getter_misc.ml | 13 ++++----- 8 files changed, 65 insertions(+), 54 deletions(-) rename helm/ocaml/getter/{http_getter_debugger.ml => http_getter_logger.ml} (85%) rename helm/ocaml/getter/{http_getter_debugger.mli => http_getter_logger.mli} (70%) diff --git a/helm/ocaml/getter/.depend b/helm/ocaml/getter/.depend index 58fb042ac..59d5b4c8e 100644 --- a/helm/ocaml/getter/.depend +++ b/helm/ocaml/getter/.depend @@ -4,10 +4,10 @@ http_getter_cache.cmi: http_getter_types.cmo http_getter.cmi: http_getter_types.cmo clientHTTP.cmo: clientHTTP.cmi clientHTTP.cmx: clientHTTP.cmi -http_getter_debugger.cmo: http_getter_debugger.cmi -http_getter_debugger.cmx: http_getter_debugger.cmi -http_getter_misc.cmo: http_getter_debugger.cmi http_getter_misc.cmi -http_getter_misc.cmx: http_getter_debugger.cmx http_getter_misc.cmi +http_getter_logger.cmo: http_getter_logger.cmi +http_getter_logger.cmx: http_getter_logger.cmi +http_getter_misc.cmo: http_getter_misc.cmi +http_getter_misc.cmx: http_getter_misc.cmi http_getter_const.cmo: http_getter_const.cmi http_getter_const.cmx: http_getter_const.cmi http_getter_env.cmo: http_getter_const.cmi http_getter_misc.cmi \ @@ -20,17 +20,13 @@ http_getter_common.cmx: http_getter_env.cmx http_getter_misc.cmx \ http_getter_types.cmx http_getter_common.cmi http_getter_map.cmo: http_getter_map.cmi http_getter_map.cmx: http_getter_map.cmi -http_getter_cache.cmo: http_getter_common.cmi http_getter_debugger.cmi \ - http_getter_env.cmi http_getter_misc.cmi http_getter_types.cmo \ - http_getter_cache.cmi -http_getter_cache.cmx: http_getter_common.cmx http_getter_debugger.cmx \ - http_getter_env.cmx http_getter_misc.cmx http_getter_types.cmx \ - http_getter_cache.cmi +http_getter_cache.cmo: http_getter_common.cmi http_getter_env.cmi \ + http_getter_misc.cmi http_getter_types.cmo http_getter_cache.cmi +http_getter_cache.cmx: http_getter_common.cmx http_getter_env.cmx \ + http_getter_misc.cmx http_getter_types.cmx http_getter_cache.cmi http_getter.cmo: clientHTTP.cmi http_getter_cache.cmi http_getter_common.cmi \ - http_getter_const.cmi http_getter_debugger.cmi http_getter_env.cmi \ - http_getter_map.cmi http_getter_misc.cmi http_getter_types.cmo \ - http_getter.cmi + http_getter_const.cmi http_getter_env.cmi http_getter_map.cmi \ + http_getter_misc.cmi http_getter_types.cmo http_getter.cmi http_getter.cmx: clientHTTP.cmx http_getter_cache.cmx http_getter_common.cmx \ - http_getter_const.cmx http_getter_debugger.cmx http_getter_env.cmx \ - http_getter_map.cmx http_getter_misc.cmx http_getter_types.cmx \ - http_getter.cmi + http_getter_const.cmx http_getter_env.cmx http_getter_map.cmx \ + http_getter_misc.cmx http_getter_types.cmx http_getter.cmi diff --git a/helm/ocaml/getter/Makefile b/helm/ocaml/getter/Makefile index ac3abcce4..cc2519cfc 100644 --- a/helm/ocaml/getter/Makefile +++ b/helm/ocaml/getter/Makefile @@ -7,7 +7,7 @@ REQUIRES = \ INTERFACE_FILES = \ clientHTTP.mli \ - http_getter_debugger.mli \ + http_getter_logger.mli \ http_getter_misc.mli \ http_getter_const.mli \ http_getter_env.mli \ diff --git a/helm/ocaml/getter/http_getter.ml b/helm/ocaml/getter/http_getter.ml index 1e85f70b4..0f20792d7 100644 --- a/helm/ocaml/getter/http_getter.ml +++ b/helm/ocaml/getter/http_getter.ml @@ -30,7 +30,6 @@ open Printf open Http_getter_common open Http_getter_misc -open Http_getter_debugger open Http_getter_types exception Not_implemented of string @@ -81,7 +80,7 @@ let map_of_uri = function | uri -> raise (Unresolvable_URI uri) let update_from_server logger server_url = (* use global maps *) - debug_print ("Updating information from " ^ server_url); + Http_getter_logger.log ("Updating information from " ^ server_url); let xml_url_of_uri = function (* TODO missing sanity checks on server_url, e.g. it can contains $1 *) | uri when (Pcre.pmatch ~rex:heading_cic_RE uri) -> @@ -108,7 +107,7 @@ let update_from_server logger server_url = (* use global maps *) http_get (server_url ^ "/" ^ (Lazy.force Http_getter_env.xsl_index))) in if (xml_index = None && rdf_index = None && xsl_index = None) then - debug_print (sprintf "Warning: useless server %s" server_url); + Http_getter_logger.log (sprintf "Warning: useless server %s" server_url); (match xml_index with | Some xml_index -> logger (`T "- Updating XML db ..."); @@ -171,7 +170,7 @@ let update_from_server logger server_url = (* use global maps *) logger (`T "All done"); logger `BR | None -> ()); - debug_print "done with this server" + Http_getter_logger.log "done with this server" let update_from_all_servers logger () = (* use global maps *) clear_maps (); diff --git a/helm/ocaml/getter/http_getter_cache.ml b/helm/ocaml/getter/http_getter_cache.ml index 75730ac21..3eebf4b3f 100644 --- a/helm/ocaml/getter/http_getter_cache.ml +++ b/helm/ocaml/getter/http_getter_cache.ml @@ -32,11 +32,10 @@ clients, uwobo (java implementation, not yet tested with the OCaml one) starts looping sending output to one of the client *) -open Http_getter_common;; -open Http_getter_debugger;; -open Http_getter_misc;; -open Http_getter_types;; -open Printf;; +open Http_getter_common +open Http_getter_misc +open Http_getter_types +open Printf (* expose ThreadSafe.threadSafe methods *) class threadSafe = @@ -115,7 +114,7 @@ let respond_xml let fill_cache () = threadSafe#doWriter (lazy( if not (is_in_cache basename) then begin (* cache MISS *) - debug_print "Cache MISS :-("; + Http_getter_logger.log ~level:2 "Cache MISS :-("; mkdir ~parents:true (Filename.dirname downloadname); match (resource_type, Lazy.force Http_getter_env.cache_mode) with | `Normal, `Normal | `Gzipped, `Gzipped -> @@ -148,7 +147,7 @@ let respond_xml res )); end else begin - debug_print "Cache HIT :-)"; + Http_getter_logger.log ~level:2 "Cache HIT :-)"; None end )) in @@ -160,10 +159,12 @@ let respond_xml (* resource in cache is already in the required format *) (match enc with | `Normal -> - debug_print "No format mangling required (encoding = normal)"; + Http_getter_logger.log ~level:2 + "No format mangling required (encoding = normal)"; return_file ~via_http ~fname:basename ~contype ~patch_fun outchan | `Gzipped -> - debug_print "No format mangling required (encoding = gzipped)"; + Http_getter_logger.log ~level:2 + "No format mangling required (encoding = gzipped)"; return_file ~via_http ~fname:(basename ^ ".gz") ~contype ~contenc:"x-gzip" ~patch_fun ~gunzip:true @@ -171,7 +172,8 @@ let respond_xml | `Normal, `Gzipped | `Gzipped, `Normal -> (match tmp_short_circuit with | None -> (* no short circuit possible, use cache *) - debug_print "No short circuit available, use cache"; + Http_getter_logger.log ~level:2 + "No short circuit available, use cache"; let tmp = tempfile () in finally (fun () -> Sys.remove tmp) (lazy ( (match enc with @@ -189,12 +191,14 @@ let respond_xml outchan) )) | Some (fname, `Normal) -> (* short circuit available, use it! *) - debug_print "Using short circuit (encoding = normal)"; + Http_getter_logger.log ~level:2 + "Using short circuit (encoding = normal)"; finally (fun () -> Sys.remove fname) (lazy ( return_file ~via_http ~fname ~contype ~patch_fun outchan )) | Some (fname, `Gzipped) -> (* short circuit available, use it! *) - debug_print "Using short circuit (encoding = gzipped)"; + Http_getter_logger.log ~level:2 + "Using short circuit (encoding = gzipped)"; finally (fun () -> Sys.remove fname) (lazy ( return_file ~via_http ~fname ~contype ~contenc:"x-gzip" ~patch_fun ~gunzip:true outchan diff --git a/helm/ocaml/getter/http_getter_env.ml b/helm/ocaml/getter/http_getter_env.ml index fa3216dcc..89fc79c88 100644 --- a/helm/ocaml/getter/http_getter_env.ml +++ b/helm/ocaml/getter/http_getter_env.ml @@ -122,6 +122,8 @@ dtd_base_urls:\t%s cache_mode:\t%s servers: \t%s +log_file:\t%s +log_level:\t%d " version (Lazy.force cic_dbm) (Lazy.force nuprl_dbm) (Lazy.force rdf_dbm) (Lazy.force xsl_dbm) (Lazy.force xml_index) @@ -136,6 +138,8 @@ servers: (String.concat "\n\t" (* (position * server) list *) (List.map (fun (pos, server) -> sprintf "%3d: %s" pos server) (servers ()))) + (match Http_getter_logger.get_log_file () with None -> "None" | Some f -> f) + (Http_getter_logger.get_log_level ()) let add_server ?position url = let new_servers = diff --git a/helm/ocaml/getter/http_getter_debugger.ml b/helm/ocaml/getter/http_getter_logger.ml similarity index 85% rename from helm/ocaml/getter/http_getter_debugger.ml rename to helm/ocaml/getter/http_getter_logger.ml index 3f9afd78c..c639f6c90 100644 --- a/helm/ocaml/getter/http_getter_debugger.ml +++ b/helm/ocaml/getter/http_getter_logger.ml @@ -26,13 +26,15 @@ * http://helm.cs.unibo.it/ *) -let debug = ref true +let log_level = ref 1 +let get_log_level () = !log_level +let set_log_level l = log_level := l (* invariant: if logfile is set, then logchan is set too *) let logfile = ref None let logchan = ref None -let set_logfile f = +let set_log_file f = (match !logchan with None -> () | Some oc -> close_out oc); match f with | Some f -> @@ -42,13 +44,13 @@ let set_logfile f = logfile := None; logchan := None -let get_logfile () = !logfile +let get_log_file () = !logfile -let close_logfile () = set_logfile None +let close_log_file () = set_log_file None -let debug_print s = - let msg = "[HTTP-Getter] " ^ s in - if !debug then +let log ?(level = 1) s = + if level <= !log_level then + let msg = "[HTTP-Getter] " ^ s in match (!logfile, !logchan) with | None, _ -> prerr_endline msg | Some fname, Some oc -> diff --git a/helm/ocaml/getter/http_getter_debugger.mli b/helm/ocaml/getter/http_getter_logger.mli similarity index 70% rename from helm/ocaml/getter/http_getter_debugger.mli rename to helm/ocaml/getter/http_getter_logger.mli index 96f32183f..d39fe739d 100644 --- a/helm/ocaml/getter/http_getter_debugger.mli +++ b/helm/ocaml/getter/http_getter_logger.mli @@ -28,15 +28,22 @@ (** {2 Debugger and logger} *) - (** enable/disable debugging messages *) -val debug: bool ref + (** log level + * 0 -> logging disabled + * 1 -> standard logging + * >=2 -> verbose logging + * default is 1 *) +val get_log_level: unit -> int +val set_log_level: int -> unit - (** output a debugging message *) -val debug_print: string -> unit + (** log a message through the logger with a given log level + * level defaults to 1, higher level denotes more verbose messages which are + * ignored with the default log_level *) +val log: ?level: int -> string -> unit (** if set to Some fname, fname will be used as a logfile, otherwise stderr * will be used *) -val get_logfile: unit -> string option -val set_logfile: string option -> unit -val close_logfile: unit -> unit +val get_log_file: unit -> string option +val set_log_file: string option -> unit +val close_log_file: unit -> unit diff --git a/helm/ocaml/getter/http_getter_misc.ml b/helm/ocaml/getter/http_getter_misc.ml index 6db793369..897442e63 100644 --- a/helm/ocaml/getter/http_getter_misc.ml +++ b/helm/ocaml/getter/http_getter_misc.ml @@ -28,8 +28,6 @@ open Printf -open Http_getter_debugger - let trailing_dot_gz_RE = Pcre.regexp "\\.gz$" (* for g{,un}zip *) let url_RE = Pcre.regexp "^([\\w.-]+)(:(\\d+))?(/.*)?$" let http_scheme_RE = Pcre.regexp ~flags:[`CASELESS] "^http://" @@ -78,7 +76,7 @@ let cp src dst = close_in ic; close_out oc let wget ?output url = - debug_print + Http_getter_logger.log (sprintf "wgetting %s (output: %s)" url (match output with None -> "default" | Some f -> f)); match url with @@ -103,7 +101,8 @@ let wget ?output url = let gzip ?(keep = false) ?output fname = let output = match output with None -> fname ^ ".gz" | Some fname -> fname in - debug_print (sprintf "gzipping %s (keep: %b, output: %s)" fname keep output); + Http_getter_logger.log ~level:3 + (sprintf "gzipping %s (keep: %b, output: %s)" fname keep output); let (ic, oc) = (open_in fname, Gzip.open_out output) in let buf = String.create bufsiz in (try @@ -128,8 +127,8 @@ let gunzip ?(keep = false) ?output fname = "Http_getter_misc.gunzip: unable to determine output file name" | Some fname -> fname in - debug_print (sprintf "gunzipping %s (keep: %b, output: %s)" - fname keep output); + Http_getter_logger.log ~level:3 + (sprintf "gunzipping %s (keep: %b, output: %s)" fname keep output); let (ic, oc) = (Gzip.open_in fname, open_out output) in let buf = String.create bufsiz in (try @@ -198,7 +197,7 @@ let http_get url = try Some (Http_client.http_get url) with e -> - debug_print (sprintf + Http_getter_logger.log (sprintf "Warning: Http_client failed on url %s with exception: %s" url (Printexc.to_string e)); None -- 2.39.2