Skip to content

Commit

Permalink
Merge pull request #1 from psrenergy/gb/add_possibility-to-define-exc…
Browse files Browse the repository at this point in the history
…eption

Remove the exit(1) and add the possibility of throwing an Exception
  • Loading branch information
guilhermebodin authored Oct 6, 2023
2 parents be6cc08 + 663f467 commit 4458f54
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/logger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 9 additions & 16 deletions src/logs.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/test_logs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ 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)
Expand Down

2 comments on commit 4458f54

@guilhermebodin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/92945

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 4458f548b927610e4c393e4347b78912105a8cd8
git push origin v0.2.0

Please sign in to comment.