]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
CA-40149: Add an explicit cache of the local hostname and make it posible to invalida...
authorDavid Scott <dave.scott@eu.citrix.com>
Mon, 12 Apr 2010 16:54:42 +0000 (17:54 +0100)
committerDavid Scott <dave.scott@eu.citrix.com>
Mon, 12 Apr 2010 16:54:42 +0000 (17:54 +0100)
The hostname is included in every log line.

Signed-off-by: David Scott <dave.scott@eu.citrix.com>
log/debug.ml
log/debug.mli

index bdd43e8a9d04dc794ed27a12ba7c35cd06ca98de..092b13e6af3042440261f7ddad897339a0edb8bf 100644 (file)
@@ -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
index 22e00136b810e420c4e09e590b9d3d9a33888e09..2f2b53cff1796b400053e0045e64dedb4c0cfb86 100644 (file)
@@ -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 *)