package vlt

  1. Overview
  2. Docs

Source file logger.ml

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
(*
 * This file is part of Bolt.
 * Copyright (C) 2009-2012 Xavier Clerc.
 *
 * Bolt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bolt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *)


let register_logger normalized_name level filter pass layout mode output (output_name, output_rotate) =
  let layout = lazy (try Layout.get layout with Not_found -> Layout.default) in
  let output = lazy
      (try
        (Output.get output) output_name output_rotate layout
      with Not_found -> Output.void output_name output_rotate layout) in
  let infos = { Tree.name = normalized_name;
                level; filter; pass; layout; mode; output; } in
  Tree.register_logger infos

let register name level filter pass layout mode output output_params =
  let normalized_name = Name.of_string name in
  let filter = lazy (try Filter.get filter with Not_found -> Filter.all) in
  let pass = lazy (try Filter.get pass with Not_found -> Filter.all) in
  register_logger normalized_name level filter pass layout mode output output_params

let log_event name level file line column properties error msg =
  let orig_event =
    Event.make
      name
      level
      ~file:file
      ~line:line
      ~column:column
      ~properties:properties
      ~error:error
      msg in
  let loggers = Tree.get_loggers name in
  try
    List.iter
      (fun (nam, lst) ->
	let event = Event.with_logger nam orig_event in
	List.iter
          (fun logger ->
            try
              let _, _, layout = Lazy.force logger.Tree.layout in
              let mode = logger.Tree.mode in
              let output = Lazy.force logger.Tree.output in
              let filter = Lazy.force logger.Tree.filter in
	      let pass = Lazy.force logger.Tree.pass in
              if (level <= logger.Tree.level) && (filter event) then
		mode#deliver output (layout event);
	      if not (pass event) then
		raise Exit
            with Exit -> raise Exit | _ -> ())
          lst)
      loggers
  with
    Exit -> ()

let check_level name level =
(*
  Printf.fprintf stderr "check_level: \"%s\" %s\n" name (Level.to_string level);
*)
  let name = Name.of_string name in
  let loggers = Tree.get_loggers name in
  try
    List.iter
      (fun (_, lst) ->
	if List.exists (fun logger -> level <= logger.Tree.level) lst then
	  raise Exit
      ) loggers;
    false
  with
    Exit -> true

let logf name level ?(file="") ?(line=(-1)) ?(column=(-1)) ?(properties=[]) ?(error=None) fmt =
  let f msg =
    let name = Name.of_string name in
    log_event name level file line column properties error msg
  in
  Printf.ksprintf f fmt

let log name level ?(file="") ?(line=(-1)) ?(column=(-1)) ?(properties=[]) ?(error=None) msg =
  let name = Name.of_string name in
  log_event name level file line column properties error msg

let prepare name =
  let name = Name.of_string name in
  Tree.make_node name

let configuration =
  let get_from_env () =
    try
      (Sys.getenv "BOLT_CONFIG"), ConfigurationNew.load
    with _ ->
      (Sys.getenv "BOLT_FILE"), ConfigurationOld.load in
  try
    let file, func = get_from_env () in
    Some (func file)
  with
  | Not_found -> None
  | Configuration.Exception (line, err) ->
      let msg =
        Printf.sprintf "syntax error in configuration file at line %d:\n  %s"
          line
          err in
      Utils.verbose msg;
      None
  | _ ->
      Utils.verbose "unable to load configuration";
      None

let () =
  try
    let plugins_env = Sys.getenv "BOLT_PLUGINS" in
    let plugins_list = Utils.split "," plugins_env in
    let plugins_list = List.map Utils.trim plugins_list in
    List.iter Dynlink.loadfile_private plugins_list
  with _ -> ()

let () =
  let used_signals = Array.make Signal.max_int false in
  let read_lines file =
    try
      let ch = open_in file in
      let res = ref [] in
      (try
        while true do
          res := (input_line ch) :: !res
        done
      with End_of_file -> ());
      List.rev !res
    with _ -> [] in
  let rec make_filter = function
    | Configuration.Identifier s
    | Configuration.String s ->
        lazy (try Filter.get s with Not_found -> Filter.all)
    | Configuration.Integer _
    | Configuration.Float _ ->
        lazy Filter.all
    | Configuration.And (v1, v2) ->
        let f1 = make_filter v1 in
        let f2 = make_filter v2 in
        lazy (fun e -> ((Lazy.force f1) e) && ((Lazy.force f2) e))
    | Configuration.Or (v1, v2) ->
        let f1 = make_filter v1 in
        let f2 = make_filter v2 in
        lazy (fun e -> ((Lazy.force f1) e) || ((Lazy.force f2) e)) in
  match configuration with
  | Some conf ->
    List.iter
      (fun section ->
        let assoc x = List.assoc x section.Configuration.elements in
        let assoc_string x =
          try
            match List.assoc x section.Configuration.elements with
            | Configuration.Identifier s
            | Configuration.String s -> Some s
            | _ -> None
          with _ -> None in
        let level =
          try
            match assoc_string "level" with
            | Some s -> Level.of_string s
            | None -> Level.FATAL
          with _ -> Level.FATAL in
        let filter =
          try
            match assoc "filter" with
            | Configuration.Identifier s
            | Configuration.String s ->
                lazy (try Filter.get s with Not_found -> Filter.all)
            | f -> make_filter f
          with _ -> lazy Filter.all in
        let pass =
          try
            match assoc "pass" with
            | Configuration.Identifier s
            | Configuration.String s ->
                lazy (try Filter.get s with Not_found -> Filter.all)
            | f -> make_filter f
          with _ -> lazy Filter.all in
        let layout =
          try
            match assoc "layout" with
            | Configuration.Identifier "pattern"
            | Configuration.String "pattern" ->
                let header =
                  try
                    match assoc_string "pattern-header-file" with
                    | Some s -> read_lines s
                    | None -> []
                  with _ -> [] in
                let footer =
                  try
                    match assoc_string "pattern-footer-file" with
                    | Some s -> read_lines s
                    | _ -> []
                  with _ -> [] in
                let pattern =
                  match assoc_string "pattern" with
                  | Some s -> s
                  | None -> "" in
                Layout.register_unnamed (Layout.pattern header footer pattern)
            | Configuration.Identifier "csv"
            | Configuration.String "csv" ->
                let sep =
                  match assoc_string "csv-separator" with
                  | Some s -> s
                  | None -> ";" in
                let list =
                  match assoc_string "csv-elements" with
                  | Some s -> s
                  | None -> "" in
                let elems = Utils.split " \t" list in
                Layout.register_unnamed (Layout.csv sep elems)
            | Configuration.Identifier s
            | Configuration.String s ->
                s
            | _ ->
                "default"
          with _ -> "default" in
        let mode =
          try
            match assoc "mode" with
            | Configuration.Identifier "direct"
            | Configuration.String "direct" ->
                Mode.direct ()
            | Configuration.Identifier "memory"
            | Configuration.String "memory" ->
                Mode.memory ()
            | Configuration.Identifier "retained"
            | Configuration.String "retained" ->
                Mode.retained
                  (match assoc_string "retention" with
                  | Some s -> s
                  | None -> "")
            | _ ->
                Mode.direct ()
          with _ -> Mode.direct () in
        let output =
          match assoc_string "output" with
          | Some s -> s
          | None -> "file" in
        let name =
          match assoc_string "name" with
          | Some s -> s
          | None -> "<stderr>" in
        let seconds =
          try
            match assoc "rotate" with
            | Configuration.Integer i ->
                Some (float_of_int i)
            | Configuration.Float f ->
                Some f
            | Configuration.Identifier s
            | Configuration.String s ->
                Some (float_of_string s)
            | _ ->
                None
          with _ -> None in
        let signal =
          try
            match assoc_string "signal" with
            | Some s ->
                let s = Signal.of_string s in
                used_signals.(Signal.to_int s) <- true;
                Some s
            | None -> None
          with _ -> None in
        let rotate = { Output.seconds_elapsed = seconds;
                       Output.signal_caught = signal; } in
        register_logger
          section.Configuration.name
          level
          filter
	  pass
          layout
          mode
          output
          (name, rotate))
      conf;
      Array.iteri
        (fun i v ->
          let handler _ =
            Output.signals.(i) <- true in
          let s = Signal.to_sys (Signal.of_int i) in
          if v then Sys.set_signal s (Sys.Signal_handle handler))
        used_signals
  | None ->
      ()