Skip to content

Commit

Permalink
Remove root macros/logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nefrathenrici committed Jan 3, 2025
1 parent 70aa316 commit f802277
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 126 deletions.
82 changes: 3 additions & 79 deletions src/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import Logging, LoggingExtras

export MPILogger, MPIFileLogger

const default_min_level = Logging.Info

"""
MPILogger(iostream = stdout)
Add a rank prefix before log messages.
Takes in `stdout` as a default IOStream.
"""
function MPILogger(iostream = stdout; ctx = context())
function MPILogger(ctx::AbstractCommsContext, iostream = stdout)
pid = mypid(ctx)

function format_log(io, log)
Expand All @@ -27,10 +25,9 @@ end
Log MPI ranks to different files within the `log_dir`.
"""
function MPIFileLogger(
function MPIFileLogger(ctx::AbstractCommsContext,
log_dir::AbstractString;
min_level::Logging.LogLevel = default_min_level,
ctx = context(),
min_level::Logging.LogLevel = Logging.Info,
)
rank = mypid(ctx)
!isdir(log_dir) && mkdir(log_dir)
Expand All @@ -40,76 +37,3 @@ function MPIFileLogger(
always_flush = true,
)
end

"""
MPIRootLogger(logger = ConsoleLogger())
Only log messages on the root process.
"""
function MPIRootLogger(
logger = Logging.ConsoleLogger(stdout, default_min_level);
ctx = context(),
)
is_root(_) = iamroot(ctx)
return LoggingExtras.EarlyFilteredLogger(is_root, logger)
end

"""
@root_logmsg level message args...
Log a message on the root process. Works similar to @logmsg but only emits
on pid 1.
Helper function for `@root_info`, `@root_debug`, `@root_warn`, `@root_error`
"""
macro root_logmsg(level, msg, args...)
return quote
if iamroot(context())
Logging.@logmsg($(esc(level)), $(esc(msg)), $(map(esc, args)...))
end
end
end

"""
@root_info msg args...
Log an Info message on the root process.
"""
macro root_info(msg, args...)
return quote
@root_logmsg(Logging.Info, $(esc(msg)), $(map(esc, args)...))
end
end

"""
@root_warn msg args...
Log a Warn message on the root process.
"""
macro root_warn(msg, args...)
return quote
@root_logmsg(Logging.Warn, $(esc(msg)), $(map(esc, args)...))
end
end

"""
@root_error msg args...
Log an Error message on the root process.
"""
macro root_error(msg, args...)
return quote
@root_logmsg(Logging.Error, $(esc(msg)), $(map(esc, args)...))
end
end

"""
@root_debug msg args...
Log a Debug message on the root process.
"""
macro root_debug(msg, args...)
return quote
@root_logmsg(Logging.Debug, $(esc(msg)), $(map(esc, args)...))
end
end
52 changes: 5 additions & 47 deletions test/logging.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@

import ClimaComms, MPI
using Logging, Test

ctx = ClimaComms.context()
ClimaComms.init(ctx)
mypid = (ClimaComms.mypid(ctx))
(mypid, nprocs) = ClimaComms.init(ctx)

@testset "MPIFileLogger" begin
log_dir = mktempdir()
logger = ClimaComms.MPIFileLogger(log_dir)
logger = ClimaComms.MPIFileLogger(ctx, log_dir)
with_logger(logger) do
test_str = "Test message from rank $mypid"
@info test_str
Expand All @@ -19,7 +17,7 @@ end

@testset "MPILogger" begin
io = IOBuffer()
logger = ClimaComms.MPILogger(io)
logger = ClimaComms.MPILogger(ctx, io)
with_logger(logger) do
@info "smoke test"
end
Expand All @@ -28,7 +26,7 @@ end

# Test with file IOStream
test_filename, io = mktemp()
logger = ClimaComms.MPILogger(io)
logger = ClimaComms.MPILogger(ctx, io)
with_logger(logger) do
test_str = "Test message from rank $mypid"
@info test_str
Expand All @@ -37,46 +35,6 @@ end

log_content = read(test_filename, String)
@test occursin(test_str, log_content)
end
end

@testset "MPIRootLogger" begin
test_filename, io = mktemp()
logger = Logging.ConsoleLogger(io)
logger = ClimaComms.MPIRootLogger(logger)
with_logger(logger) do
test_str = "Test message from rank $mypid"
@info test_str
flush(io)
close(io)

log_content = read(test_filename, String)
if ClimaComms.iamroot(ctx)
@test occursin(test_str, log_content)
else
@test isempty(log_content)
end
end
end

@testset "root logging macros" begin
test_filename, io = mktemp()
logger = Logging.ConsoleLogger(io)

with_logger(logger) do
test_str = "Test root message"
ClimaComms.@root_info test_str
ClimaComms.@root_warn test_str
ClimaComms.@root_error test_str

flush(io)
close(io)
log_content = read(test_filename, String)

if ClimaComms.iamroot(ctx)
@test occursin(test_str, log_content)
else
@test isempty(log_content)
end
@test occursin(test_str, log_content)
end
end

0 comments on commit f802277

Please sign in to comment.