-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ?fail_on_error argument to Lwt_log_core.load_rules, also expose new level_of_string function in the interface #306
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,17 @@ let string_of_level = function | |
| Error -> "error" | ||
| Fatal -> "fatal" | ||
|
||
let level_of_string str = | ||
let str = (String.lowercase [@ocaml.warning "-3"]) str in | ||
match str with | ||
| "debug" -> Ok Debug | ||
| "info" -> Ok Info | ||
| "notice" -> Ok Notice | ||
| "warning" -> Ok Warning | ||
| "error" -> Ok Error | ||
| "fatal" -> Ok Fatal | ||
| _ -> Pervasives.Error (Printf.sprintf "invalid log level (%s)" str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say for this function, to have it evaluate to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, right now, shall we use option or introduce a dependency on the result compatibility library? If 4.02 support is a goal, option is the easiest way out, and I agree Error doesn't offer much more than None here, but I'll do whichever you tell me to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I missed it. Ok, I'll go with option then. |
||
|
||
(* +-----------------------------------------------------------------+ | ||
| Patterns and rules | | ||
+-----------------------------------------------------------------+ *) | ||
|
@@ -101,31 +112,30 @@ let split pattern = | |
in | ||
loop 0 | ||
|
||
|
||
let rules = ref [] | ||
|
||
let load_rules' str = | ||
let load_rules' str fail_on_error = | ||
let rec loop = function | ||
| [] -> | ||
[] | ||
| (pattern, level) :: rest -> | ||
let pattern = split pattern in | ||
match (String.lowercase [@ocaml.warning "-3"]) level with | ||
| "debug" -> (pattern, Debug) :: loop rest | ||
| "info" -> (pattern, Info) :: loop rest | ||
| "notice" -> (pattern, Notice) :: loop rest | ||
| "warning" -> (pattern, Warning) :: loop rest | ||
| "error" -> (pattern, Error) :: loop rest | ||
| "fatal" -> (pattern, Fatal) :: loop rest | ||
| level -> log_intern "invalid log level (%s)" level; loop rest | ||
| [] -> [] | ||
| (pattern, level_str) :: rest -> | ||
let pattern = split pattern in | ||
let level = level_of_string level_str in | ||
match level with | ||
| Ok level -> (pattern, level) :: loop rest | ||
| Pervasives.Error msg -> | ||
if fail_on_error then raise (Failure msg) | ||
else log_intern "invalid log level (%s)" level_str; loop rest | ||
in | ||
match Lwt_log_rules.rules (Lexing.from_string str) with | ||
| None -> Printf.eprintf "Invalid contents of the LWT_LOG variable\n%!" | ||
| Some l -> rules := loop l | ||
| None -> | ||
if fail_on_error then raise (Failure "Invalid log rules") | ||
else Printf.eprintf "Invalid contents of the LWT_LOG variable\n%!" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're fixing this up anyway, I think the reference to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just trying to keep the old behaviour completely intact, reference to LWT_LOG does bother me too of course. Shall we just replace the message with "Invalid contents of log rules" or similar? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Up to you. Using the same message for both the exception and the log message is fine. I hope nobody was parsing their log for the string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my thought exactly... ;) |
||
| Some l -> rules := loop l | ||
|
||
|
||
let _ = | ||
match try Some(Sys.getenv "LWT_LOG") with Not_found -> None with | ||
| Some str -> load_rules' str | ||
| Some str -> load_rules' str false | ||
| None -> () | ||
|
||
(* +-----------------------------------------------------------------+ | ||
|
@@ -197,8 +207,8 @@ end | |
|
||
type section = Section.t | ||
|
||
let load_rules str = | ||
load_rules' str; | ||
let load_rules ?(fail_on_error=false) str = | ||
load_rules' str fail_on_error; | ||
Section.recompute_levels () | ||
|
||
let add_rule pattern level = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,9 @@ type section | |
|
||
val string_of_level : level -> string | ||
|
||
val load_rules : string -> unit | ||
val level_of_string : string -> (level, string) result | ||
|
||
val load_rules : ?fail_on_error:bool -> string -> unit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably document There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. I'll add a docstring. |
||
(** Reset the rules set when parsing the [LWT_LOG] environment variable using this | ||
string. *) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right to worry about this, but we can't use
lowercase_ascii
because Lwt still supports 4.02.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, good I kept it intact then.