-
Notifications
You must be signed in to change notification settings - Fork 18
/
logger_macro.ml
313 lines (292 loc) · 9.66 KB
/
logger_macro.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
302
303
304
305
306
307
308
309
310
311
312
313
open Camlp4.PreCast
(*
This file defines several macros related to the Logger module.
It is important to keep in mind that macros can't always be used like functions, therefor it's useful to know how these macros are expanded.
The general structure is the following (in which level can be any of Debug|Info|Notice|Warning|Error|Fatal):
Logger.level ?exn section msg
Logger.level_ ?exn msg
Logger.level_f ?exn section format_string and args
Logger.level_f_ ?exn format_string and args
Logger.ign_level ?exn section msg
Logger.ign_level_ ?exn msg
Logger.ign_level_f ?exn section format_string and args
Logger.ign_level_f_ ?exn format_string and args
The macros ending with _ grab a variable named section from the enviroment.
The macros starting with ign_ return unit instead of unit Lwt.t.
Macros containing _f wrap the 'format_string and args' part into a closure which can at a later time be evaluated to calculate a string.
Some concrete examples:
Logger.debug section "log this"
=> Logger.log section Logger.Debug "log this"
Logger.debug_ "log this"
=> Logger.log section Logger.Debug "log this"
Logger.debug_f section "format%s string" "bla"
=> Logger.log_ section Logger.Debug (fun () -> Printf.sprintf "format%s string" "bla")
Logger.debug_f_ "format%s string" "bla"
=> Logger.log_ section Logger.Debug (fun () -> Printf.sprintf "format%s string" "bla")
!!!! Important note !!!
Most of the time it's good when the arguments for a format string are captured in a closure, because when the arguments themselves are expensive to compute this cost might be avoided.
The catch however is that when the computation of the arguments depends on a variable that might be changed at a later time or for example a call to Unix.gettimeofday this will also be evaluated at a later time, possibly resulting in another log message than intended.
Bad examples (don't do this):
Logger.debug_f_ "Event bla happened at time %f" (Unix.gettimeofday ())
Logger.debug_f_ "bla bla %s" (string_of !counter)
*)
let rec apply e = function
| [] -> e
| x :: l -> let _loc = Ast.loc_of_expr x in apply <:expr< $e$ $x$ >> l
let split e =
let rec aux acc = function
| <:expr@_loc< Logger.log_f $section$ $level$ >> ->
`Log_f(section, level, acc)
| <:expr@_loc< Logger.ign_log_f $section$ $level$ >> ->
`Ign_log_f(section, level, acc)
| <:expr@_loc< Logger.$lid:func$ ~exn $arg$ >> ->
let ign =
String.length func >= 4 && "ign_" = String.sub func 0 4 in
let func =
if ign
then
String.sub func 4 (String.length func - 4)
else
func in
let implicit_section = func.[String.length func - 1] = '_' in
let pos = String.length func - 1 - (if implicit_section then 1 else 0) in
let is_f = func.[pos] = 'f' in
if not is_f
then
let level = String.sub func 0 (pos + 1) in
if level = "log"
then
`No_match
else
let level' = String.capitalize level in
if implicit_section
then
`Log_e_l_ (arg::acc, level', ign)
else
`Log_e_l(acc, level', arg, ign)
else
let level = String.sub func 0 (pos - 1) in
let level' = String.capitalize level in
if implicit_section
then
`Log_e_f_l_ (arg::acc, level', ign)
else
`Log_e_f_l (acc, level', arg, ign)
| <:expr@_loc< Logger.$lid:func$ $arg$ >> ->
let ign =
String.length func >= 4 && "ign_" = String.sub func 0 4 in
let func =
if ign
then
String.sub func 4 (String.length func - 4)
else
func in
let implicit_section = func.[String.length func - 1] = '_' in
let pos = String.length func - 1 - (if implicit_section then 1 else 0) in
let is_f = func.[pos] = 'f' in
if not is_f
then
let level = String.sub func 0 (pos + 1) in
if level = "log"
then
`No_match
else
let level' = String.capitalize level in
if implicit_section
then
`Log_l_(arg::acc, level', ign)
else
`Log_l (acc, level', arg, ign)
else
let level = String.sub func 0 (pos - 1) in
let level' = String.capitalize level in
if implicit_section
then
`Log_f_l_ (arg::acc, level', ign)
else
`Log_f_l (acc, level', arg, ign)
| <:expr@_loc< $a$ $b$ >> -> begin
match b with
| b ->
aux (b :: acc) a
end
| _ ->
`No_match
in
aux [] e
let make_loc _loc =
<:expr<
($str:Loc.file_name _loc$,
$int:string_of_int (Loc.start_line _loc)$,
$int:string_of_int (Loc.start_off _loc - Loc.start_bol _loc)$)
>>
let map =
object
inherit Ast.map as super
method! expr e =
let _loc = Ast.loc_of_expr e in
match split e with
| `Log_f(section, level, args) ->
let args = List.map super#expr args in
<:expr<
Logger.log_
$section$
$level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
| `Ign_log_f(section, level, args) ->
let args = List.map super#expr args in
<:expr<
Logger.ign_log_
$section$
$level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
| `Log_l_(args, level, ign) ->
let args = List.map super#expr args in
if ign
then
apply
<:expr<
Logger.ign_log
section
Lwt_log.$uid:level$
>> args
else
apply
<:expr<
Logger.log
section
Lwt_log.$uid:level$
>> args
| `Log_e_l_(args, level, ign) ->
let args = List.map super#expr args in
if ign
then
apply
<:expr<
Logger.ign_log
~exn
section
Lwt_log.$uid:level$
>> args
else
apply
<:expr<
Logger.log
~exn
section
Lwt_log.$uid:level$
>> args
| `Log_l(args, level, section, ign) ->
let args = List.map super#expr args in
if ign
then
apply
<:expr<
Logger.ign_log
$section$
Lwt_log.$uid:level$
>> args
else
apply
<:expr<
Logger.log
$section$
Lwt_log.$uid:level$
>> args
| `Log_e_l(args, level, section, ign) ->
let args = List.map super#expr args in
if ign
then
apply <:expr<
Logger.ign_log
~exn
$section$
Lwt_log.$uid:level$
>> args
else
apply <:expr<
Logger.log
~exn
$section$
Lwt_log.$uid:level$
>> args
| `Log_f_l_(args, level, ign) ->
let args = List.map super#expr args in
if ign
then
<:expr<
Logger.ign_log_
section
Lwt_log.$uid:level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
else
<:expr<
Logger.log_
section
Lwt_log.$uid:level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
| `Log_e_f_l_(args, level, ign) ->
let args = List.map super#expr args in
if ign
then
<:expr<
Logger.ign_log_
~exn
section
Lwt_log.$uid:level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
else
<:expr<
Logger.log_
~exn
section
Lwt_log.$uid:level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
| `Log_f_l(args, level, section, ign) ->
let args = List.map super#expr args in
if ign
then
<:expr<
Logger.ign_log_
$section$
Lwt_log.$uid:level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
else
<:expr<
Logger.log_
$section$
Lwt_log.$uid:level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
| `Log_e_f_l(args, level, section, ign) ->
let args = List.map super#expr args in
if ign
then
<:expr<
Logger.ign_log_
~exn
$section$
Lwt_log.$uid:level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
else
<:expr<
Logger.log_
~exn
$section$
Lwt_log.$uid:level$
(fun () -> $apply <:expr< Printf.sprintf >> args$)
>>
| `No_match ->
super#expr e
end
let () =
AstFilters.register_str_item_filter map#str_item;
AstFilters.register_topphrase_filter map#str_item;