File: dtools_syslog.ml

package info (click to toggle)
ocaml-dtools 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 688 kB
  • sloc: sh: 3,262; ml: 986; makefile: 73
file content (56 lines) | stat: -rw-r--r-- 2,001 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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: savonet-devl@lists.sourceforge.net                           *)
  (**************************************************************************)

 (* $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)