package async
Source file log_extended.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 304open Core open Async include Log module Console = struct module Ansi = Console.Ansi let with_style ~debug ~info ~error msg = let style, prefix = match Log.Message.level msg with | None -> info, "" | Some `Debug -> debug, "[DEBUG]" | Some `Info -> info, " [INFO]" | Some `Error -> error, "[ERROR]" in String.concat ~sep:" " [ prefix; Log.Message.message msg ] |> Ansi.string_with_attr style ;; let output ?(debug = ([ `Yellow ] :> Ansi.attr list)) ?(info = ([] :> Ansi.attr list)) ?(error = ([ `Red ] :> Ansi.attr list)) writer = Log.Output.create ~flush:(fun () -> return ()) (fun msgs -> Queue.iter msgs ~f:(fun msg -> with_style ~debug ~info ~error msg |> fun styled_msg -> Writer.write writer styled_msg; Writer.newline writer); Deferred.any_unit [ Writer.flushed writer; Writer.consumer_left writer ]) ;; module Blocking = struct let output ?(debug = ([ `Yellow ] :> Ansi.attr list)) ?(info = ([] :> Ansi.attr list)) ?(error = ([ `Red ] :> Ansi.attr list)) outc = Log.Blocking.Output.create (fun msg -> with_style ~debug ~info ~error msg |> fun line -> Out_channel.output_lines outc [ line ]) ;; end end module Syslog = struct let to_syslog msg = let prefix = match Log.Message.level msg with | None -> "" | Some l -> Log.Level.to_string l ^ " " in prefix ^ Log.Message.message msg ;; let to_level msg = match Log.Message.level msg with (* syslog is generally not configured to show `LOG_DEBUG *) | None -> Syslog.Level.INFO | Some `Debug -> Syslog.Level.INFO | Some `Info -> Syslog.Level.INFO | Some `Error -> Syslog.Level.ERR ;; let default_options = [ Syslog.Open_option.PID; Syslog.Open_option.CONS ] let openlog ?id ?(options = default_options) ?facility () = Syslog.openlog ?id ~options ?facility () ;; let output ?id ?options ?facility () = let ready = Lazy_deferred.create (fun () -> (* openlog () shouldn't block by default, but In_thread.run's a cheap cure for paranoia *) In_thread.run (openlog ?id ?options ?facility)) in Log.Output.create ~flush:(fun () -> return ()) (fun msgs -> Lazy_deferred.force_exn ready >>= fun () -> In_thread.run (fun () -> Queue.iter msgs ~f:(fun msg -> let syslog_level = to_level msg in let msg = to_syslog msg in Syslog.syslog ~level:syslog_level (msg ^ "\n")))) ;; module Blocking = struct let output () = openlog (); Log.Blocking.Output.create (fun msg -> let syslog_level = to_level msg in let msg = to_syslog msg in Syslog.syslog ~level:syslog_level (msg ^ "\n")) ;; end end module Command = struct open Core open Async type console_style = | Plain | Color [@@deriving sexp] type console_output = | No | Stdout of console_style | Stderr of console_style [@@deriving sexp] module Parameters = struct type t = { log_level : Level.t ; log_to_console : console_output ; log_to_syslog : bool ; log_to_file : string option } [@@deriving fields ~getters ~iterators:create, sexp] module Flag_name = struct (* This module exists to make it easier to inspect flag names. *) let log_to_file = "log-to-file" let log_to_console = "log-to-console" let log_to_stdout = "log-to-stdout" let log_with_color = "log-with-color" let log_to_syslog = "log-to-syslog" let log_level = "log-level" end let log_to_file_flag t = let default = Option.value t.log_to_file ~default:"<NONE>" in let doc = sprintf "FILENAME Log to a file (default: %s)" default in Command.Param.(flag Flag_name.log_to_file (optional Filename_unix.arg_type) ~doc) ;; let log_to_console_flag t = let default = match t.log_to_console with | No -> false | Stderr _ | Stdout _ -> true in let doc = sprintf !"BOOL Log to console (default: %{Bool})" default in Command.Param.( flag Flag_name.log_to_console (optional_with_default default bool) ~doc) ;; let log_to_syslog_flag t = let doc = sprintf !"BOOL Log to syslog (default: %{Bool})" t.log_to_syslog in Command.Param.( flag Flag_name.log_to_syslog (optional_with_default t.log_to_syslog bool) ~doc) ;; let log_to_stdout_flag t = let default = match t.log_to_console with | No -> false | Stdout _ -> true | Stderr _ -> false in let doc = sprintf !"BOOL Log to stdout when logging to console (default: %{Bool})" default in Command.Param.( flag Flag_name.log_to_stdout (optional_with_default default bool) ~doc) ;; let log_with_color_flag t = let default = match t.log_to_console with | No | Stdout Plain | Stderr Plain -> false | Stdout Color | Stderr Color -> true in let doc = sprintf !"BOOL Log with color when logging to console (default: %{Bool})" default in Command.Param.( flag Flag_name.log_with_color (optional_with_default default bool) ~doc) ;; let log_level_flag t = let doc = sprintf !"LEVEL Set log level to one of [debug | error | info] (default: %{Log.Level})" t.log_level in Command.Param.( flag Flag_name.log_level (optional_with_default t.log_level Level.arg) ~doc) ;; let create_log_to_console ~log_to_console ~log_with_color ~log_to_stdout = match log_to_console, log_with_color, log_to_stdout with | false, true, _ | false, _, true -> failwithf "-%s and -%s require -%s" Flag_name.log_with_color Flag_name.log_to_stdout Flag_name.log_to_console () | true, false, false -> Stderr Plain | true, true, false -> Stderr Color | true, false, true -> Stdout Plain | true, true, true -> Stdout Color | false, false, false -> No ;; (* This tests the only(?) path to turning input into a safe type. *) let%expect_test "console_output" = let print ~log_to_console ~log_with_color ~log_to_stdout = let result = match create_log_to_console ~log_to_console ~log_with_color ~log_to_stdout |> sexp_of_console_output |> Sexp.to_string with | exception e -> Or_error.of_exn ~backtrace:(`This "") e | s -> Or_error.return s in Async.printf !"%{sexp:string Or_error.t}\n" result in print ~log_to_console:false ~log_with_color:false ~log_to_stdout:false; [%expect {| (Ok No) |}]; print ~log_to_console:false ~log_with_color:true ~log_to_stdout:true; [%expect {| (Error ((Failure "-log-with-color and -log-to-stdout require -log-to-console") ())) |}]; print ~log_to_console:true ~log_with_color:true ~log_to_stdout:false; [%expect {| (Ok "(Stderr Color)") |}]; print ~log_to_console:true ~log_with_color:false ~log_to_stdout:false; [%expect {| (Ok "(Stderr Plain)") |}]; print ~log_to_console:true ~log_with_color:true ~log_to_stdout:true; [%expect {| (Ok "(Stdout Color)") |}]; print ~log_to_console:true ~log_with_color:false ~log_to_stdout:true; [%expect {| (Ok "(Stdout Plain)") |}]; Deferred.unit ;; let params t = let%map_open.Command log_to_file = log_to_file_flag t and log_to_console = log_to_console_flag t and log_to_stdout = log_to_stdout_flag t and log_with_color = log_with_color_flag t and log_to_syslog = log_to_syslog_flag t and log_level = log_level_flag t in { log_to_file ; log_to_console = create_log_to_console ~log_to_console ~log_with_color ~log_to_stdout ; log_to_syslog ; log_level } ;; let console_output t = match t.log_to_console with | No -> None | Stderr Plain -> Some (Log.Output.stderr ()) | Stderr Color -> Some (Console.output (Lazy.force Writer.stderr)) | Stdout Plain -> Some (Log.Output.stdout ()) | Stdout Color -> Some (Console.output (Lazy.force Writer.stdout)) ;; let syslog_output t = if t.log_to_syslog then Some (Syslog.output ()) else None let file_output t = match t.log_to_file with | None -> None | Some filename -> Some (Output.file `Text ~filename) ;; let outputs t = List.filter_map [ console_output t; syslog_output t; file_output t ] ~f:Fn.id ;; end let setup_via_params ?(default_output_level = `Info) ~log_to_console_by_default ~log_to_syslog_by_default ?log_to_file_by_default () = let default = Parameters.Fields.create ~log_level:default_output_level ~log_to_console:log_to_console_by_default ~log_to_syslog:log_to_syslog_by_default ~log_to_file:log_to_file_by_default in let%map.Command params = Parameters.params default in Log.Global.set_output (Parameters.outputs params); Log.Global.set_level (Parameters.log_level params) ;; end