Skip to content
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 Abstract Types #2

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
return dict
end

function set_dict(toml_dict_path::String)
function set_dict(toml_dict_path::AbstractString)

Check warning on line 60 in src/constants.jl

View check run for this annotation

Codecov / codecov/patch

src/constants.jl#L60

Added line #L60 was not covered by tests
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 @@
return POLYGLOT_LOG_DICT[1]
end

function toml_file_to_dict(toml_dict_path::String)
function toml_file_to_dict(toml_dict_path::AbstractString)

Check warning on line 73 in src/constants.jl

View check run for this annotation

Codecov / codecov/patch

src/constants.jl#L73

Added line #L73 was not covered by tests
@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 @@
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 @@
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...)

Check warning on line 85 in src/logs.jl

View check run for this annotation

Codecov / codecov/patch

src/logs.jl#L85

Added line #L85 was not covered by tests
msg = prepare_msg(code, replacements...)
warn(msg)
return nothing
end
function non_fatal_error(code::Int, replacements...)
function non_fatal_error(code::Integer, replacements...)

Check warning on line 90 in src/logs.jl

View check run for this annotation

Codecov / codecov/patch

src/logs.jl#L90

Added line #L90 was not covered by tests
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
Loading