Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Base.close for TBLogger #109

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions src/TBLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ mutable struct TBLogger{P,S} <: AbstractLogger
global_step::Int
step_increment::Int
min_level::LogLevel

function TBLogger{P,S}(logdir::P,
file::S,
all_files::Dict{String, S},
global_step::Int,
step_increment::Int,
min_level::LogLevel) where {P,S}
lg = new{P, S}(logdir, file, all_files, global_step, step_increment, min_level)
return Base.finalizer(Base.close, lg)
end
end


Expand Down Expand Up @@ -195,6 +205,18 @@ Returns the internal step counter of the logger.
"""
step(lg::TBLogger) = lg.global_step

"""
close(lg)

Close the TBLogger `lg`, releasing all file handles.
"""
function Base.close(lg::TBLogger)
# close open streams
for k=keys(lg.all_files)
close(lg.all_files[k])
end
end

"""
reset!(lg)

Expand Down
25 changes: 25 additions & 0 deletions test/test_TBLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ end
close.(values(tbl.all_files))
end

@testset "closing" begin
tbl = TBLogger(test_log_dir*"run", tb_overwrite)
TensorBoardLogger.add_eventfile(tbl, "pp")
files = keys(tbl.all_files)

close(tbl)
@test begin
foreach(f -> rm(joinpath(test_log_dir*"run", f)), files)
# rm will error if the file is still open
true
end

tbl = TBLogger(test_log_dir*"run", tb_overwrite)
TensorBoardLogger.add_eventfile(tbl, "pp")
files = keys(tbl.all_files)

tbl = nothing
Base.finalize(tbl)
@test begin
foreach(f -> rm(joinpath(test_log_dir*"run", f)), files)
# rm will error if the file is still open
true
end
end

@testset "resetting" begin
tbl = TBLogger(test_log_dir*"run", tb_overwrite)
TensorBoardLogger.add_eventfile(tbl, "pp")
Expand Down