Skip to content

Commit

Permalink
add RemoteLogger for distributed logging
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Jan 4, 2023
1 parent 52af407 commit cafdce9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions stdlib/Distributed/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name = "Distributed"
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[deps]
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
Expand Down
1 change: 1 addition & 0 deletions stdlib/Distributed/src/Distributed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ hash(r::RRID, h::UInt) = hash(r.whence, hash(r.id, h))
==(r::RRID, s::RRID) = (r.whence==s.whence && r.id==s.id)

include("clusterserialize.jl")
include("logger.jl")
include("cluster.jl") # cluster setup and management, addprocs
include("messages.jl")
include("process_messages.jl") # process incoming messages
Expand Down
1 change: 1 addition & 0 deletions stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ function start_worker(out::IO, cookie::AbstractString=readline(stdin); close_std
# To prevent hanging processes on remote machines, newly launched workers exit if the
# master process does not connect in time.
check_master_connect()
Logging.global_logger(RemoteLogger(1, Logging.Info))
while true; wait(); end
catch err
print(stderr, "unhandled exception on $(myid()): $(err)\nexiting.\n")
Expand Down
23 changes: 23 additions & 0 deletions stdlib/Distributed/src/logger.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Logging

struct RemoteLogger <: Logging.AbstractLogger
pid::Int
min_level::Logging.LogLevel
end
function RemoteLogger(pid=1)
RemoteLogger(pid, Logging.Info)
end

Logging.min_enabled_level(logger::RemoteLogger) = logger.min_level
Logging.shouldlog(logger::RemoteLogger, level, _module, group, id) = true

# TODO: probably should live in base/logging.jl?
function logmsg(level::Logging.LogLevel, message, _module, _group, _id, _file, _line; kwargs...)
Logging.@logmsg level message _module=_module _group=_group _id=_id _file=_file _line=_line kwargs...
end

function Logging.handle_message(logger::RemoteLogger, level::Logging.LogLevel, message, _module, _group, _id,
_file, _line; kwargs...)
@nospecialize
remote_do(logmsg, logger.pid, level, message, _module, _group, _id, _file, _line; pid=myid(), kwargs...)
end

0 comments on commit cafdce9

Please sign in to comment.