Skip to content

Commit 361fbe7

Browse files
committed
Translate Lwt_log.file
The channel is not closed in the generated code, so manual intervention is needed. 'set_close_on_exec' is no longer called on the fd.
1 parent b7dcb5e commit 361fbe7

File tree

4 files changed

+270
-38
lines changed

4 files changed

+270
-38
lines changed

bin/lwt_log_to_logs/main.ml

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ let mk_log section cmd args =
2828
let mk_set_level section lvl =
2929
mk_apply_simple [ "Logs"; "Src"; "set_level" ] [ section; lvl ]
3030

31-
let mk_format_reporter ~state ~template ~close_mode channel =
31+
let mk_format_reporter_of_channel channel =
32+
mk_let_var "logs_formatter"
33+
(mk_apply_simple [ "Format"; "formatter_of_out_channel" ] [ channel ])
34+
@@ fun fmt ->
35+
mk_apply_ident
36+
[ "Logs"; "format_reporter" ]
37+
[ (mk_lbl "app", fmt); (mk_lbl "dst", fmt); (Nolabel, mk_unit_val) ]
38+
39+
let mk_channel ~state ~template ~close_mode channel =
3240
if Option.is_some template then
3341
add_comment state
3442
"Lwt_log.channel: The [~template] argument is unsupported. Use \
@@ -39,27 +47,42 @@ let mk_format_reporter ~state ~template ~close_mode channel =
3947
add_comment state
4048
"Lwt_log.channel: The [~close_mode] argument has been dropped. The \
4149
behavior is always [`Keep].");
42-
let fmt_ident = "logs_formatter" in
43-
let fmt_val = mk_exp_var fmt_ident in
4450
add_comment state
4551
"Format.formatter_of_out_channel: Argument is a [Lwt_io.output_channel] \
4652
but a [out_channel] is expected.";
47-
mk_let
48-
(Pat.var (mk_loc fmt_ident))
49-
(mk_apply_simple [ "Format"; "formatter_of_out_channel" ] [ channel ])
50-
(mk_apply_ident
51-
[ "Logs"; "format_reporter" ]
52-
[
53-
(mk_lbl "app", fmt_val); (mk_lbl "dst", fmt_val); (Nolabel, mk_unit_val);
54-
])
53+
mk_format_reporter_of_channel channel
54+
55+
let mk_file ~state ~mode ~perm ~file_name =
56+
let mk_mode append_mode =
57+
Exp.list
58+
[
59+
append_mode;
60+
mk_constr_exp [ "Open_wronly" ];
61+
mk_constr_exp [ "Open_creat" ];
62+
mk_constr_exp [ "Open_text" ];
63+
]
64+
in
65+
let mode =
66+
mk_let_var "append_mode"
67+
(Exp.match_
68+
(value_of_lblopt ~default:(mk_variant_exp "Append") mode)
69+
[
70+
Exp.case (mk_variant_pat "Append") (mk_constr_exp [ "Open_append" ]);
71+
Exp.case (mk_variant_pat "Truncate") (mk_constr_exp [ "Open_trunc" ]);
72+
])
73+
mk_mode
74+
in
75+
let perm = value_of_lblopt ~default:(mk_const_int "0o640") perm in
76+
add_comment state "[file]: Channel is never closed.";
77+
mk_format_reporter_of_channel
78+
(mk_apply_simple [ "open_out_gen" ] [ mode; perm; file_name ])
5579

5680
let mk_reporter report_f =
5781
let report =
5882
let open Mk_function in
5983
mk_function
60-
(return report_f $ (Nolabel, "src") $ (Nolabel, "level")
61-
$ (mk_lbl "over", "over")
62-
$ (Nolabel, "k") $ (Nolabel, "msgf"))
84+
(return report_f $ arg "src" $ arg "level" $ arg ~lbl:`Lbl "over"
85+
$ arg "k" $ arg "msgf")
6386
in
6487
Exp.record [ (mk_longident [ "Logs"; "report" ], None, Some report) ] None
6588

@@ -215,16 +238,19 @@ let rewrite_apply_lwt_log ~state (unit, ident) args =
215238
take_lbl "close_mode" @@ fun close_mode ->
216239
take_lbl "channel" @@ fun channel ->
217240
take @@ fun _unit ->
218-
return
219-
(Some (mk_format_reporter ~state ~template ~close_mode channel))
241+
return (Some (mk_channel ~state ~template ~close_mode channel))
220242
| "syslog" ->
221243
ignore_lblarg "template" @@ take_lblopt "paths"
222244
@@ fun paths ->
223245
take_lbl "facility" @@ fun facility ->
224246
take @@ fun _unit -> return (Some (mk_syslog ~state ~paths ~facility))
225247
| "file" ->
226-
add_comment state (ident ^ " is no longer supported.");
227-
return None
248+
ignore_lblarg "template" @@ take_lblopt "mode"
249+
@@ fun mode ->
250+
take_lblopt "perm" @@ fun perm ->
251+
take_lbl "file_name" @@ fun file_name ->
252+
take @@ fun _unit ->
253+
return (Some (mk_file ~state ~mode ~perm ~file_name))
228254
| _ -> return None)
229255
| _ -> return None
230256

lib/ocamlformat_utils/ast_utils.ml

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ let mk_longident' = function
2929

3030
let mk_longident ident = mk_loc (mk_longident' ident)
3131
let mk_constr_exp ?arg cstr = Exp.construct (mk_longident cstr) arg
32+
33+
let mk_constr_pat ?arg cstr =
34+
Pat.construct (mk_longident cstr) (Option.map (fun a -> ([], a)) arg)
35+
36+
let mk_variant_exp ?arg cstr = Exp.variant (mk_loc (mk_loc cstr)) arg
37+
let mk_variant_pat ?arg cstr = Pat.variant (mk_loc (mk_loc cstr)) arg
3238
let same_longident a b = Longident.flatten a = b
3339
let mk_exp_ident ident = Exp.ident (mk_longident ident)
3440
let mk_exp_var s = mk_exp_ident [ s ]
@@ -42,6 +48,11 @@ let mk_none_ident = mk_longident [ "None" ]
4248
let mk_exp_some x = Exp.construct mk_some_ident (Some x)
4349
let mk_exp_none = Exp.construct mk_none_ident None
4450
let mk_typ_constr ?(params = []) lid = Typ.constr (mk_longident lid) params
51+
let mk_const_int i = Exp.constant (Const.integer i)
52+
let mk_lbl s = Labelled (mk_loc s)
53+
let mk_lblopt s = Optional (mk_loc s)
54+
let mk_pat_some arg = mk_constr_pat ~arg [ "Some" ]
55+
let mk_pat_none = mk_constr_pat [ "None" ]
4556

4657
(* Construct [let var = lhs in (rhs var)]. *)
4758
let mk_let_var ident lhs rhs =
@@ -56,32 +67,41 @@ module Mk_function : sig
5667
let open Mk_function in
5768
mk_function
5869
(return (fun a b -> Exp.tuple [ a; b ])
59-
$ (Nolabel, "a") $ (Nolabel, "b"))
70+
$ arg "a" $ arg "b")
6071
in
6172
]} *)
6273

6374
type 'a t
75+
type arg
6476

65-
val ( $ ) : (expression -> 'a) t -> arg_label * string -> 'a t
77+
val arg : ?lbl:[ `Lbl | `Opt of expression option ] -> string -> arg
78+
val ( $ ) : (expression -> 'a) t -> arg -> 'a t
6679
val return : 'a -> 'a t
6780
val mk_function : ?typ:type_constraint -> expression t -> expression
6881
end = struct
6982
type 'a t = expr_function_param list * 'a
83+
type arg = expr_function_param * expression
7084

71-
let ( $ ) (params, body) (lbl, ident) =
72-
let exp = mk_exp_var ident and pat = Pat.var (mk_loc ident) in
73-
let params = mk_function_param ~lbl pat :: params in
74-
(params, body exp)
75-
85+
let ( $ ) (params, body) (param, exp) = (param :: params, body exp)
7686
let return f = ([], f)
7787

88+
let arg ?lbl name =
89+
let lbl, def =
90+
match lbl with
91+
| Some `Lbl -> (Some (mk_lbl name), None)
92+
| Some (`Opt def) -> (Some (mk_lblopt name), def)
93+
| None -> (None, None)
94+
in
95+
let exp = mk_exp_var name and pat = Pat.var (mk_loc name) in
96+
(mk_function_param ?lbl ?def pat, exp)
97+
7898
let mk_function ?typ (params, body) =
7999
Exp.function_ (List.rev params) typ (Pfunction_body body)
80100
end
81101

82-
let mk_fun ?(arg_lbl = Nolabel) ?(arg_name = "x") f =
102+
let mk_fun ?(arg_name = "x") f =
83103
let open Mk_function in
84-
mk_function (return f $ (arg_lbl, arg_name))
104+
mk_function (return f $ arg arg_name)
85105

86106
let is_unit_val = function
87107
| { pexp_desc = Pexp_construct (ident, None); _ } ->
@@ -98,13 +118,24 @@ let mk_binding_op ?(loc = !default_loc) ?(is_pun = false) op pat ?(args = [])
98118
?(typ = None) exp =
99119
Exp.binding_op op pat args typ exp is_pun loc
100120

101-
let mk_lbl s = Labelled (mk_loc s)
102-
let mk_lblopt s = Optional (mk_loc s)
103121
let mk_apply_ident ident args = Exp.apply (mk_exp_ident ident) args
104122

105123
let mk_apply_simple f_ident args =
106124
mk_apply_ident f_ident (List.map (fun x -> (Nolabel, x)) args)
107125

126+
(** Generate an expression that read the value of a optional argument obtained
127+
with [Unpack_apply.take_lblopt]. *)
128+
let value_of_lblopt ~default arg =
129+
match arg with
130+
| Some (exp, `Lbl) -> exp
131+
| Some (exp, `Opt) ->
132+
Exp.match_ exp
133+
[
134+
Exp.case (mk_pat_some (Pat.var (mk_loc "x"))) (mk_exp_var "x");
135+
Exp.case mk_pat_none default;
136+
]
137+
| None -> default
138+
108139
(** Flatten a pipelines composed of [|>] and [@@] into a [Pexp_apply] node. *)
109140
let rec flatten_apply exp =
110141
let flatten callee arg =

test/lwt_log_to_logs/migrate.t/foo.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ let () =
9090
let _ = Lwt_log.file in
9191
()
9292

93+
let () =
94+
let _ = Lwt_log.file ~template:"" ~file_name:"" () in
95+
let _ = Lwt_log.file ~mode:`Truncate ~file_name:"" () in
96+
let _ = let mode = `Append in Lwt_log.file ~mode ~file_name:"" () in
97+
let _ = let mode = Some `Append in Lwt_log.file ?mode ~file_name:"" () in
98+
let _ = Lwt_log.file ~perm:1 ~file_name:"" () in
99+
let _ = let perm = 1 in Lwt_log.file ~perm ~file_name:"" () in
100+
let _ = let perm = Some 1 in Lwt_log.file ?perm ~file_name:"" () in
101+
let _ = Lwt_log.file ~file_name:"" () in
102+
()
103+
93104
let _open_files () =
94105
(* Extracted from ocsigenserver's [src/server/ocsigen_messages.ml]. *)
95106
let open Lwt.Infix in

0 commit comments

Comments
 (0)