Skip to content

Commit f6ca8c9

Browse files
committed
Handle the ~exn argument
1 parent 361fbe7 commit f6ca8c9

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

bin/lwt_log_to_logs/main.ml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,32 @@ let rewrite_apply_lwt_log ~state (unit, ident) args =
155155
in
156156
let logf ~ident ~mk_log logs_name =
157157
take_lblopt "section" @@ fun section ->
158-
ignore_lblarg ~cmt:" Use [Printexc.to_string]." "exn"
159-
@@ ignore_lblarg "location" @@ ignore_lblarg "logger" @@ take
158+
take_lblopt "exn" @@ fun exn ->
159+
ignore_lblarg "location" @@ ignore_lblarg "logger" @@ take
160160
@@ fun fmt_arg ->
161161
take_all @@ fun args ->
162-
let args = (Nolabel, fmt_arg) :: args in
163-
let args =
162+
let fmt_arg, args =
164163
(* Log calls that don't end in [_f] use a ["%s"] format string to avoid
165164
any typing and escaping issues. *)
166-
if String.ends_with ~suffix:"_f" ident || format_safe fmt_arg then args
167-
else (Nolabel, Exp.constant (Const.string "%s")) :: args
165+
if String.ends_with ~suffix:"_f" ident || format_safe fmt_arg then
166+
(fmt_arg, args)
167+
else (mk_const_string "%s", (Nolabel, fmt_arg) :: args)
168168
in
169-
Some (mk_log section logs_name args)
169+
let fmt_arg, args =
170+
(* Print the [exn] argument. *)
171+
match exn with
172+
| Some (exn, lbl) ->
173+
if lbl = `Opt then
174+
add_comment state
175+
"Last argument is a [exception option] while [exception] is \
176+
expected.";
177+
( Exp.infix (mk_loc "^^") fmt_arg (mk_const_string "@\n%s"),
178+
args
179+
@ [ (Nolabel, mk_apply_simple [ "Printexc"; "to_string" ] [ exn ]) ]
180+
)
181+
| None -> (fmt_arg, args)
182+
in
183+
Some (mk_log section logs_name ((Nolabel, fmt_arg) :: args))
170184
in
171185
let log_unit ~ident n = logf ~ident ~mk_log n in
172186
let log_lwt ~ident n =

lib/ocamlformat_utils/ast_utils.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,16 @@ let mk_none_ident = mk_longident [ "None" ]
4848
let mk_exp_some x = Exp.construct mk_some_ident (Some x)
4949
let mk_exp_none = Exp.construct mk_none_ident None
5050
let mk_typ_constr ?(params = []) lid = Typ.constr (mk_longident lid) params
51-
let mk_const_int i = Exp.constant (Const.integer i)
5251
let mk_lbl s = Labelled (mk_loc s)
5352
let mk_lblopt s = Optional (mk_loc s)
5453
let mk_pat_some arg = mk_constr_pat ~arg [ "Some" ]
5554
let mk_pat_none = mk_constr_pat [ "None" ]
5655

56+
(* Exp *)
57+
58+
let mk_const_string s = Exp.constant (Const.string s)
59+
let mk_const_int i = Exp.constant (Const.integer i)
60+
5761
(* Construct [let var = lhs in (rhs var)]. *)
5862
let mk_let_var ident lhs rhs =
5963
let pat = Pat.var (mk_loc ident) in

test/lwt_log_to_logs/migrate.t/run.t

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@
107107
108108
(* Other arguments *)
109109
let () =
110-
Logs.info (fun fmt ->
111-
fmt
112-
(* TODO: lwt-log-to-logs: Labelled argument ~exn was dropped. Use [Printexc.to_string]. *)
113-
"exn")
110+
Logs.info (fun fmt -> fmt ("exn" ^^ "@\n%s") (Printexc.to_string Not_found))
114111

115112
let () =
116113
Logs.info (fun fmt ->
@@ -127,9 +124,10 @@
127124
(* Other arguments as opt labels *)
128125
let () =
129126
Logs.info (fun fmt ->
130-
fmt
131-
(* TODO: lwt-log-to-logs: Labelled argument ?exn was dropped. Use [Printexc.to_string]. *)
132-
"exn")
127+
fmt ("exn" ^^ "@\n%s")
128+
(Printexc.to_string
129+
(* TODO: lwt-log-to-logs: Last argument is a [exception option] while [exception] is expected. *)
130+
(Some Not_found)))
133131

134132
let () =
135133
Logs.info (fun fmt ->
@@ -147,27 +145,30 @@
147145
[@@@warning "-5"]
148146

149147
let _
148+
(* TODO: lwt-log-to-logs: Last argument is a [exception option] while [exception] is expected. *)
150149
(* TODO: lwt-log-to-logs: Labelled argument ?logger was dropped. *)
151-
(* TODO: lwt-log-to-logs: Labelled argument ?location was dropped. *)
152-
(* TODO: lwt-log-to-logs: Labelled argument ?exn was dropped. Use [Printexc.to_string]. *)
153-
=
150+
(* TODO: lwt-log-to-logs: Labelled argument ?location was dropped. *) =
154151
fun ?section:x1 ?exn:x2 ?location:x3 ?logger:x4 x5 ->
155-
Logs.info ?src:x1 (fun fmt -> fmt "%s" x5)
152+
Logs.info ?src:x1 (fun fmt ->
153+
fmt ("%s" ^^ "@\n%s") x5 (Printexc.to_string x2))
156154

157155
let _ =
158156
fun ?exn:x1 ?location:x2 ?logger:x3 x4 ->
159157
Logs.info
160158
~src:
159+
(* TODO: lwt-log-to-logs: Last argument is a [exception option] while [exception] is expected. *)
161160
(* TODO: lwt-log-to-logs: Labelled argument ?logger was dropped. *)
162161
(* TODO: lwt-log-to-logs: Labelled argument ?location was dropped. *)
163-
(* TODO: lwt-log-to-logs: Labelled argument ?exn was dropped. Use [Printexc.to_string]. *)
164-
section (fun fmt -> fmt "%s" x4)
162+
section (fun fmt -> fmt ("%s" ^^ "@\n%s") x4 (Printexc.to_string x1))
165163

166-
let _
167-
(* TODO: lwt-log-to-logs: Labelled argument ~exn was dropped. Use [Printexc.to_string]. *)
168-
(* TODO: lwt-log-to-logs: Labelled argument ?logger was dropped. *)
169-
(* TODO: lwt-log-to-logs: Labelled argument ?location was dropped. *) =
170-
fun ?location:x1 ?logger:x2 x3 -> Logs.info (fun fmt -> fmt "%s" x3)
164+
let _ =
165+
fun ?location:x1 ?logger:x2 x3 ->
166+
Logs.info (fun fmt ->
167+
fmt ("%s" ^^ "@\n%s") x3
168+
(Printexc.to_string
169+
(* TODO: lwt-log-to-logs: Labelled argument ?logger was dropped. *)
170+
(* TODO: lwt-log-to-logs: Labelled argument ?location was dropped. *)
171+
Not_found))
171172

172173
let _
173174
(* TODO: lwt-log-to-logs: Labelled argument ~location was dropped. *)

0 commit comments

Comments
 (0)