Hi all !

Following #361, I am investigating the implemenation of a syslog support in 
dtools.

I have come with an implementation which I think is nice. Its extends the Log 
module by adding the possibility to register new logging method and implements 
a syslog method.

The syslog method is only compiled if syslog is detected at configure.

I would like to have your feedback on the proposed patch (attached) before 
commiting, in particular gim since he's dtools' father :-)

Romain
Index: configure.ac
===================================================================
--- configure.ac	(révision 7305)
+++ configure.ac	(copie de travail)
@@ -1,7 +1,8 @@
-AC_INIT(ocaml-dtools, 0.1.6, [email protected])
+AC_INIT(ocaml-dtools, 0.2.0, [email protected])
 VERSION=$PACKAGE_VERSION
 AC_MSG_RESULT(configuring $PACKAGE_STRING)
 
+REQUIRES="str unix thread"
 OCAMLFIND_LDCONF=""
 AC_ARG_ENABLE([ldconf], AC_HELP_STRING([--disable-ldconf],[don't modify the dynamic loader configuration file (default is enable)]),[ac_enable_ldconf=$enableval],[ac_enable_ldconf=$enableval],[ac_enable_ldconf=yes])
 if test "$ac_enable_ldconf" = no ; then
@@ -139,6 +140,30 @@
 #	AC_MSG_ERROR(Cannot find ps2pdf.)
 #fi
 
+#
+# Syslog
+#
+
+AC_ARG_WITH([syslog-dir],
+   AC_HELP_STRING(
+      [--with-syslog-dir=path],
+      [look for ocaml-syslog library in "path" (autodetected by default)]))
+AC_ARG_ENABLE([syslog],
+              AC_HELP_STRING([--disable-syslog],["don't use ocaml-syslog"]))
+
+if test "x$enable_syslog" != "xno" ; then
+AC_MSG_CHECKING(for ocaml-syslog)
+    if ! $OCAMLFIND query syslog > /dev/null 2>&1 ; then
+        SYSLOG_FILES=""
+        AC_MSG_RESULT(not found)
+    else
+        SYSLOG_FILES="dtools_syslog.ml"
+        AC_MSG_RESULT(ok)
+        REQUIRES="$REQUIRES syslog"
+        INC="$INC `$OCAMLFIND query syslog`"
+    fi
+fi
+
 # substitutions to perform
 AC_SUBST(OCAMLC)
 AC_SUBST(OCAMLOPT)
@@ -157,6 +182,9 @@
 AC_SUBST(OCAMLCP)	# TODO
 AC_SUBST(CAMLLIBPATH)
 AC_SUBST(BEST)
+AC_SUBST(REQUIRES)
+AC_SUBST(SYSLOG_FILES)
+AC_SUBST(INC)
 
 AC_SUBST(LATEX)		# TODO
 AC_SUBST(DVIPS)		# TODO
Index: src/Makefile.in
===================================================================
--- src/Makefile.in	(révision 7308)
+++ src/Makefile.in	(copie de travail)
@@ -19,10 +19,11 @@
 BEST = @BEST@
 
 THREADS = "yes"
-SOURCES = dtools.ml dtools.mli
+SOURCES = dtools.ml dtools.mli @SYSLOG_FILES@
 RESULT = dtools
 LIBINSTALL_FILES = $(wildcard *.cma *.cmxa *.cmx *.mli *.cmi *.a)
-OCAMLLDFLAGS =
+OCAMLLDFLAGS = -linkall
+INCDIRS = @INC@
 NO_CUSTOM = yes
 OCAMLFLAGS = @OCAMLFLAGS@
 
Index: src/dtools.mli
===================================================================
--- src/dtools.mli	(révision 7308)
+++ src/dtools.mli	(copie de travail)
@@ -1,14 +1,14 @@
 
   (**************************************************************************)
   (*  ocaml-dtools                                                          *)
-  (*  Copyright (C) 2003-2006  The Savonet Team                             *)
+  (*  Copyright (C) 2003-2010  The Savonet Team                             *)
   (**************************************************************************)
   (*  This program is free software; you can redistribute it and/or modify  *)
   (*  it under the terms of the GNU General Public License as published by  *)
   (*  the Free Software Foundation; either version 2 of the License, or     *)
   (*  any later version.                                                    *)
   (**************************************************************************)
-  (*  Contact: [email protected]                                                 *)
+  (*  Contact: [email protected]                           *)
   (**************************************************************************)
 
 (* $Id$ *)
@@ -255,6 +255,22 @@
        Type for loggers.
     *)
 
+  type custom_log =
+    {
+      timestamp : bool ;
+      exec      : string -> unit
+    }
+
+  val add_custom_log : string -> custom_log -> unit
+    (**
+      Add a custom logging functions. 
+    *)
+
+  val rm_custom_log : string -> unit
+    (**
+      Remove a custom logging functions.    
+    *)
+
   val make : Conf.path -> t
     (**
       Make a logger labeled according to the given path.
Index: src/META.in
===================================================================
--- src/META.in	(révision 7308)
+++ src/META.in	(copie de travail)
@@ -1,6 +1,6 @@
 name="dtools"
 version="@PACKAGE_VERSION@"
 description="OCaml deamon tools library"
-requires="str unix threads"
+requires="@REQUIRES@"
 archive(byte) = "dtools.cma"
 archive(native) = "dtools.cmxa"
Index: src/dtools.ml
===================================================================
--- src/dtools.ml	(révision 7309)
+++ src/dtools.ml	(copie de travail)
@@ -1,14 +1,14 @@
 
   (**************************************************************************)
   (*  ocaml-dtools                                                          *)
-  (*  Copyright (C) 2003-2006  The Savonet Team                             *)
+  (*  Copyright (C) 2003-2010  The Savonet Team                             *)
   (**************************************************************************)
   (*  This program is free software; you can redistribute it and/or modify  *)
   (*  it under the terms of the GNU General Public License as published by  *)
   (*  the Free Software Foundation; either version 2 of the License, or     *)
   (*  any later version.                                                    *)
   (**************************************************************************)
-  (*  Contact: [email protected]                                                 *)
+  (*  Contact: [email protected]                           *)
   (**************************************************************************)
 
 (* $Id$ *)
@@ -641,11 +641,23 @@
 	f: 'a. int -> ('a, unit, string, unit) format4 -> 'a;
       >
 
+  type custom_log = 
+    { 
+      timestamp : bool ;
+      exec      : string -> unit 
+    }
+
   let log_ch = ref None
 
   (* Mutex to avoid interlacing logs *)
   let log_mutex = Mutex.create ()
 
+  (* Custom logging methods. *)
+  let custom_log : (string, custom_log) Hashtbl.t = Hashtbl.create 0
+
+  let add_custom_log name f = Hashtbl.replace custom_log name f
+  let rm_custom_log name = Hashtbl.remove custom_log name
+
   let conf =
     Conf.void "log configuration"
 
@@ -698,22 +710,27 @@
   let print (time, str) =
     let to_stdout = conf_stdout#get in
     let to_file = !log_ch <> None in
+    let timestamp = timestamp time in
+    let message = 
+        Printf.sprintf "%s %s" timestamp str
+    in
     begin match to_stdout || to_file with
     | true ->
-        let timestamp = timestamp time in
 	let do_stdout () =
-	  Printf.printf "%s %s\n%!" timestamp str;
+	  Printf.printf "%s\n%!" message;
 	in
 	let do_file () =
 	  begin match !log_ch with
 	  | None -> ()
-	  | Some ch -> Printf.fprintf ch "%s %s\n%!" timestamp str;
+	  | Some ch -> Printf.fprintf ch "%s\n%!" message;
 	  end
 	in
 	if to_stdout then do_stdout ();
 	if to_file then do_file ();
     | false -> ()
-    end
+    end ;
+    let f _ x = x.exec (if x.timestamp then message else str) in
+    Hashtbl.iter f custom_log
 
   let proceed entry =
     mutexify (fun () ->
Index: src/dtools_syslog.ml
===================================================================
--- src/dtools_syslog.ml	(révision 0)
+++ src/dtools_syslog.ml	(révision 0)
@@ -0,0 +1,56 @@
+  (**************************************************************************)
+  (*  ocaml-dtools                                                          *)
+  (*  Copyright (C) 2003-2010  The Savonet Team                             *)
+  (**************************************************************************)
+  (*  This program is free software; you can redistribute it and/or modify  *)
+  (*  it under the terms of the GNU General Public License as published by  *)
+  (*  the Free Software Foundation; either version 2 of the License, or     *)
+  (*  any later version.                                                    *)
+  (**************************************************************************)
+  (*  Contact: [email protected]                           *)
+  (**************************************************************************)
+
+ (* $Id$ *)
+
+ (* Syslog logging. *)
+
+open Dtools
+
+let conf_syslog = 
+  Conf.bool ~p:(Log.conf#plug "syslog") ~d:false
+    "Enable syslog logging."
+let conf_program = 
+  Conf.string ~p:(conf_syslog#plug "program") 
+    ~d:(Filename.basename Sys.executable_name)
+    "Name of the program."
+let conf_facility = 
+  Conf.string ~p:(conf_syslog#plug "facility") ~d:"DAEMON"
+    "Logging facility."
+
+let logging = ref None
+
+let () = 
+  let start () = 
+    if conf_syslog#get then
+      let facility = 
+        Syslog.facility_of_string conf_facility#get 
+      in
+      let program = 
+        Printf.sprintf "%s[%d]" conf_program#get (Unix.getpid ())
+      in
+      let log = Syslog.openlog ~facility program in
+      logging := Some log ;
+      let exec s = Syslog.syslog log `LOG_INFO s in
+      Log.add_custom_log 
+         program { Log.
+                    timestamp = false ;
+                    exec      = exec }
+  in
+  let stop () = 
+    match !logging with
+      | Some x -> Syslog.closelog x
+      |_ -> ()
+  in
+  ignore (Dtools.Init.at_start ~before:[Log.start] start) ;
+  ignore (Dtools.Init.at_stop ~after:[Log.stop] stop)
+

Modification de propriétés sur src/dtools_syslog.ml
___________________________________________________________________
Ajouté : svn:keywords
   + Author Date Id Revision
Ajouté : svn:eol-style
   + native

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
Savonet-devl mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/savonet-devl

Répondre à