From a8190b3b20459231786736c07bbe6711af4a096a Mon Sep 17 00:00:00 2001 From: guilhermebodin Date: Fri, 6 Oct 2023 13:07:18 -0300 Subject: [PATCH 1/3] remove the exit(1) and add the possibility of throwing an Exception --- Project.toml | 2 +- src/logger.jl | 2 +- src/logs.jl | 25 +++++++++---------------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/Project.toml b/Project.toml index ed7383d..564bc2d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "LoggingPolyglot" uuid = "211639cc-9b11-4cfd-abc6-8f7477829344" -version = "0.1.0" +version = "0.2.0" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/logger.jl b/src/logger.jl index 810415e..dae7c09 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -225,7 +225,7 @@ end function print_colored(io::IO, str::String; color::Symbol = :normal, reverse::Bool = false) if color == :normal && reverse == false - print(io, str) # MD Studio doesn't support colors and even printstyled with color = :normal and reverse = false doesn't work + print(io, str) else printstyled(io, str; color = color, reverse = reverse) end diff --git a/src/logs.jl b/src/logs.jl index 6fda222..3e8fa6f 100644 --- a/src/logs.jl +++ b/src/logs.jl @@ -1,15 +1,3 @@ -# Additional level -macro fatal_error(msg) - quote - @logmsg FatalErrorLevel $(esc(msg)) - if isinteractive() - error("Fatal Error") - else - exit(1) - end - end -end - # Direct logs function debug(msg::String; level::Int = -1000) @assert Logging.Debug <= Logging.LogLevel(level) < Logging.Info @@ -28,8 +16,9 @@ function non_fatal_error(msg::String) @error msg return nothing end -function fatal_error(msg::String) - @fatal_error msg +function fatal_error(msg::String; exception::Exception = ErrorException("Fatal error")) + @logmsg FatalErrorLevel msg + throw(exception) return nothing end @@ -100,8 +89,12 @@ function non_fatal_error(code::Int, replacements...) non_fatal_error(msg) return nothing end -function fatal_error(code::Int, replacements...) +function fatal_error( + code::Int, + replacements...; + exception::Exception = ErrorException("Fatal error"), +) msg = prepare_msg(code, replacements...) - fatal_error(msg) + fatal_error(msg; exception = exception) return nothing end From 31dc236d470b2f2d80d575b4dac7c5611d9cb082 Mon Sep 17 00:00:00 2001 From: guilhermebodin Date: Fri, 6 Oct 2023 13:10:23 -0300 Subject: [PATCH 2/3] add tests --- test/test_logs.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_logs.jl b/test/test_logs.jl index dc928dd..1a643ef 100644 --- a/test/test_logs.jl +++ b/test/test_logs.jl @@ -47,6 +47,24 @@ function test_direct_log_error() return nothing end +function test_direct_log_fatal_error() + log_error_path = "error.log" + polyglot_logger = LoggingPolyglot.create_polyglot_logger(log_error_path) + @test_throws ErrorException LoggingPolyglot.fatal_error("test message") + LoggingPolyglot.close_polyglot_logger(polyglot_logger) + rm(log_error_path; force = true) + return nothing +end + +function test_direct_log_fatal_error_other_exception() + log_error_path = "error.log" + polyglot_logger = LoggingPolyglot.create_polyglot_logger(log_error_path) + @test_throws DimensionMismatch LoggingPolyglot.fatal_error("dim mismatch"; exception = DimensionMismatch("")) + LoggingPolyglot.close_polyglot_logger(polyglot_logger) + rm(log_error_path; force = true) + return nothing +end + function test_different_logs_same_file() log_path = "test.log" polyglot_logger = LoggingPolyglot.create_polyglot_logger(log_path) From 663f4675b649d818f9714917d8419d8c1846d356 Mon Sep 17 00:00:00 2001 From: guilhermebodin Date: Fri, 6 Oct 2023 13:14:37 -0300 Subject: [PATCH 3/3] format logs tests --- test/test_logs.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test_logs.jl b/test/test_logs.jl index 1a643ef..06facd6 100644 --- a/test/test_logs.jl +++ b/test/test_logs.jl @@ -59,7 +59,10 @@ end function test_direct_log_fatal_error_other_exception() log_error_path = "error.log" polyglot_logger = LoggingPolyglot.create_polyglot_logger(log_error_path) - @test_throws DimensionMismatch LoggingPolyglot.fatal_error("dim mismatch"; exception = DimensionMismatch("")) + @test_throws DimensionMismatch LoggingPolyglot.fatal_error( + "dim mismatch"; + exception = DimensionMismatch(""), + ) LoggingPolyglot.close_polyglot_logger(polyglot_logger) rm(log_error_path; force = true) return nothing