Skip to content

Commit

Permalink
Merge pull request #2 from psrenergy/feature/abstract
Browse files Browse the repository at this point in the history
Add Abstract Types
  • Loading branch information
guilhermebodin authored Oct 9, 2023
2 parents 75467eb + 9da7b90 commit f42692b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 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.2.0"
version = "0.2.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
6 changes: 3 additions & 3 deletions src/constants.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const FatalErrorLevel = Logging.LogLevel(3000)

function set_language(lang::String)
function set_language(lang::AbstractString)
POLYGLOT_LANG[1] = lang
return lang
end
Expand Down Expand Up @@ -57,7 +57,7 @@ function set_dict(dict::Dict)
return dict
end

function set_dict(toml_dict_path::String)
function set_dict(toml_dict_path::AbstractString)
dict = toml_file_to_dict(toml_dict_path)
if !is_valid_dict(dict)
error("The dictionary of codes and language is invalid.")
Expand All @@ -70,7 +70,7 @@ function get_dict()
return POLYGLOT_LOG_DICT[1]
end

function toml_file_to_dict(toml_dict_path::String)
function toml_file_to_dict(toml_dict_path::AbstractString)
@assert isfile(toml_dict_path)
toml_dict = TOML.parsefile(toml_dict_path)
new_dict = Dict{Int, Dict{String, String}}()
Expand Down
19 changes: 12 additions & 7 deletions src/logger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ function close_polyglot_logger(logger::TeeLogger)
end

"""
remove_log_file_path_on_logger_creation(log_file_path::String)
remove_log_file_path_on_logger_creation(log_file_path::AbstractString)
* `log_file_path`: Remove log file in path log_file_path
"""
function remove_log_file_path_on_logger_creation(log_file_path::String)
function remove_log_file_path_on_logger_creation(log_file_path::AbstractString)
try
if global_logger() isa TeeLogger
close_polyglot_logger(global_logger())
Expand Down Expand Up @@ -69,7 +69,7 @@ function get_tag_brackets(level::LogLevel, brackets_dict::Dict)
end
end

function treat_empty_tag(level_to_print::String, close_bracket::String)
function treat_empty_tag(level_to_print::AbstractString, close_bracket::AbstractString)
if level_to_print == "" && close_bracket == ""
return ""
else
Expand All @@ -79,7 +79,7 @@ end

"""
create_polyglot_logger(
log_file_path::String;
log_file_path::AbstractString;
min_level_console::Logging.LogLevel,
min_level_file::Logging.LogLevel,
brackets,
Expand Down Expand Up @@ -129,7 +129,7 @@ end
)
"""
function create_polyglot_logger(
log_file_path::String;
log_file_path::AbstractString;
min_level_console::Logging.LogLevel = Logging.Info,
min_level_file::Logging.LogLevel = Logging.Debug,
append_log::Bool = false,
Expand Down Expand Up @@ -209,7 +209,7 @@ end

function print_colored(
io::IO,
str::String,
str::AbstractString,
level::Logging.LogLevel,
color_dict::Dict{String, Symbol},
reverse_dict::Dict{String, Bool},
Expand All @@ -223,7 +223,12 @@ function print_colored(
return nothing
end

function print_colored(io::IO, str::String; color::Symbol = :normal, reverse::Bool = false)
function print_colored(
io::IO,
str::AbstractString;
color::Symbol = :normal,
reverse::Bool = false,
)
if color == :normal && reverse == false
print(io, str)
else
Expand Down
29 changes: 16 additions & 13 deletions src/logs.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
# Direct logs
function debug(msg::String; level::Int = -1000)
function debug(msg::AbstractString; level::Integer = -1000)
@assert Logging.Debug <= Logging.LogLevel(level) < Logging.Info
@logmsg Logging.LogLevel(level) msg
return nothing
end
function info(msg::String)
function info(msg::AbstractString)
@info msg
return nothing
end
function warn(msg::String)
function warn(msg::AbstractString)
@warn msg
return nothing
end
function non_fatal_error(msg::String)
function non_fatal_error(msg::AbstractString)
@error msg
return nothing
end
function fatal_error(msg::String; exception::Exception = ErrorException("Fatal error"))
function fatal_error(
msg::AbstractString;
exception::Exception = ErrorException("Fatal error"),
)
@logmsg FatalErrorLevel msg
throw(exception)
return nothing
end

# logs via code and language
function get_raw_message(dict::Dict, code::Int, lang::String)
function get_raw_message(dict::Dict, code::Integer, lang::AbstractString)
if haskey(dict, code) # Code could be an Int
langs_dict = dict[code]
if haskey(langs_dict, lang)
Expand All @@ -43,15 +46,15 @@ function get_raw_message(dict::Dict, code::Int, lang::String)
end
end

function prepare_msg(code::Int, replacements...)
function prepare_msg(code::Integer, replacements...)
dict = get_dict()
lang = get_language()
raw_message = get_raw_message(dict, code, lang)
treated_message = treat_message(raw_message, replacements...)
return treated_message
end

function treat_message(raw_message::String, replacements...)
function treat_message(raw_message::AbstractString, replacements...)
splitted_message = split(raw_message, "@@@")
num_correct_replacements = length(splitted_message) - 1
# If the is no @@@ in the message we can return the raw_message
Expand All @@ -69,28 +72,28 @@ function treat_message(raw_message::String, replacements...)
return treated_message
end

function debug(code::Int, replacements...; level::Int = -1000)
function debug(code::Integer, replacements...; level::Integer = -1000)
msg = prepare_msg(code, replacements...)
debug(msg; level)
return nothing
end
function info(code::Int, replacements...)
function info(code::Integer, replacements...)
msg = prepare_msg(code, replacements...)
info(msg)
return nothing
end
function warn(code::Int, replacements...)
function warn(code::Integer, replacements...)
msg = prepare_msg(code, replacements...)
warn(msg)
return nothing
end
function non_fatal_error(code::Int, replacements...)
function non_fatal_error(code::Integer, replacements...)
msg = prepare_msg(code, replacements...)
non_fatal_error(msg)
return nothing
end
function fatal_error(
code::Int,
code::Integer,
replacements...;
exception::Exception = ErrorException("Fatal error"),
)
Expand Down

2 comments on commit f42692b

@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/93072

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.1 -m "<description of version>" f42692bdacd8c15ed6bc9c2dc03d69482b947bac
git push origin v0.2.1

Please sign in to comment.