From: David Scott Date: Mon, 12 Apr 2010 16:54:42 +0000 (+0100) Subject: CA-40149: Add an explicit cache of the local hostname and make it posible to invalida... X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=c123f14b51c1da682e2cd87fd6ab7c163c58b2d4;p=xcp%2Fxen-api-libs.git CA-40149: Add an explicit cache of the local hostname and make it posible to invalidate the cache. The hostname is included in every log line. Signed-off-by: David Scott --- diff --git a/log/debug.ml b/log/debug.ml index bdd43e8..092b13e 100644 --- a/log/debug.ml +++ b/log/debug.ml @@ -22,11 +22,6 @@ let thread_tasks_m = Mutex.create () let get_thread_id () = try Thread.id (Thread.self ()) with _ -> -1 -(* Theses functions need to be defined later in the code. *) -let get_hostname = - let f () = "Debug.get_hostname not set" in - ref f - let associate_thread_with_task task = let id = get_thread_id () in if id <> -1 @@ -81,8 +76,18 @@ module type BRAND = sig val name: string end +let hostname_cache = ref None +let hostname_m = Mutex.create () +let get_hostname () = + match Mutex.execute hostname_m (fun () -> !hostname_cache) with + | Some h -> h + | None -> + let h = Unix.gethostname () in + Mutex.execute hostname_m (fun () -> hostname_cache := Some h); + h +let invalidate_hostname_cache () = Mutex.execute hostname_m (fun () -> hostname_cache := None) + module Debugger = functor(Brand: BRAND) -> struct - let hostname = Unix.gethostname () let _ = Mutex.execute dkmutex (fun () -> debug_keys := StringSet.add Brand.name !debug_keys) @@ -102,7 +107,7 @@ module Debugger = functor(Brand: BRAND) -> struct let output (f:string -> ?extra:string -> ('a, unit, string, 'b) format4 -> 'a) fmt = let extra = Printf.sprintf "%s|%s|%s|%s" - hostname + (get_hostname ()) (get_thread_name ()) (get_task ()) Brand.name @@ -112,7 +117,7 @@ module Debugger = functor(Brand: BRAND) -> struct let output_and_return ?raw (f:string -> ?raw:bool -> ?extra:string -> ('a, unit, string, 'b) format4 -> 'a) fmt = let extra = Printf.sprintf "%s|%s|%s|%s" - hostname + (get_hostname ()) (get_thread_name ()) (get_task ()) Brand.name diff --git a/log/debug.mli b/log/debug.mli index 22e0013..2f2b53c 100644 --- a/log/debug.mli +++ b/log/debug.mli @@ -14,6 +14,9 @@ (** Debug utilities *) +(** Throw away the cached hostname. The next log line will re-query the hostname *) +val invalidate_hostname_cache: unit -> unit + (** {2 Associate a task to the current actions} *) (** Associate a task name to the current thread *)