Skip to content

RFC: hide some of the logging macro expansion inside a function #25911

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
45 changes: 22 additions & 23 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,35 +294,34 @@ function logmsg_code(_module, file, line, level, message, exs...)
# Note that it may be necessary to set `id` and `group` manually during bootstrap
id !== nothing || (id = Expr(:quote, log_record_id(_module, level, exs)))
group !== nothing || (group = Expr(:quote, Symbol(splitext(basename(file))[1])))
quote
level = $level
std_level = convert(LogLevel, level)
return quote
std_level = convert(LogLevel, $level)
if std_level >= _min_enabled_level[]
logstate = current_logstate()
if std_level >= logstate.min_enabled_level
logger = logstate.logger
_module = $_module
id = $id
group = $group
# Second chance at an early bail-out, based on arbitrary
# logger-specific logic.
if shouldlog(logger, level, _module, group, id)
# Bind log record generation into a closure, allowing us to
# defer creation of the records until after filtering.
create_msg = function cm(logger, level, _module, group, id, file, line)
msg = $(esc(message))
handle_message(logger, level, msg, _module, group, id, file, line; $(kwargs...))
end
file = $file
line = $line
dispatch_message(logger, level, _module, group, id, file, line, create_msg)
end
maybe_log(std_level, $level, $_module, $id, $group, $(esc(message)), $file, $line, $(kwargs...))
end
end
end

# This is in a separate function for performance
function maybe_log(std_level, level, _module, id, group, msg, file, line, kwargs...)
Copy link
Member Author

Choose a reason for hiding this comment

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

Should be ;kwargs...

logstate = current_logstate()
if std_level >= logstate.min_enabled_level
logger = logstate.logger
# Second chance at an early bail-out, based on arbitrary
# logger-specific logic.
if shouldlog(logger, level, _module, group, id)
# Bind log record generation into a closure, allowing us to
# defer creation of the records until after filtering.
create_msg = function cm(logger, level, _module, group, id, file, line)
handle_message(logger, level, msg, _module, group, id, file, line; kwargs...)
end
dispatch_message(logger, level, _module, group, id, file, line, create_msg)
end
nothing
end
return nothing
end


# Call the log message creation function, and dispatch the result to `logger`.
# TODO: Consider some @nospecialize annotations here
# TODO: The `logger` is loaded from global state and inherently non-inferrable,
Expand Down