From 9af5a958f0bf53f0188628f4674938f7275eef47 Mon Sep 17 00:00:00 2001 From: Mandar Chitre Date: Mon, 5 Feb 2024 23:59:43 +0800 Subject: [PATCH] refactor: replace reporterror with logerror --- src/container.jl | 58 +++++++++++++++++++++++---------------- src/coroutine_behavior.jl | 8 ++---- src/fsm.jl | 4 +-- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/container.jl b/src/container.jl index 4c58bb3..de12b8b 100644 --- a/src/container.jl +++ b/src/container.jl @@ -701,22 +701,32 @@ _deliver(c::SlaveContainer, msg::Message) = _deliver(c, msg, true) ### stacktrace pretty printing & auto-reconnection -function reporterror(src, ex) - fname = basename(@__FILE__) - bt = String[] - for s ∈ stacktrace(catch_backtrace()) - push!(bt, " $s") - basename(string(s.file)) == fname && s.func == :run && break - end - bts = join(bt, '\n') - if src === nothing - @error "$(ex)\n Stack trace:\n$(bts)" - else - @error "[$(src)] $(ex)\n Stack trace:\n$(bts)" +""" + logerror(f::Function) + logerror(f::Function, src) + +Run function `f()` and log any errors that occur. +""" +function logerror(f::Function, src=nothing) + try + f() + catch ex + logerror(ex, src) end end -reporterror(ex) = reporterror(nothing, ex) +""" + logerror(err::Exception) + logerror(err::Exception, src) + +Log error `err` with a simple stack trace. +""" +function logerror(ex::Exception, src=nothing) + io = IOBuffer() + src === nothing || print(io, "[$src] ") + Base.showerror(IOContext(io, :limit => true), ex, Base.catch_backtrace()) + @error String(take!(io)) +end reconnect(c::StandaloneContainer, ex) = false function reconnect(c::SlaveContainer, ex) @@ -1443,7 +1453,7 @@ function action(b::OneShotBehavior) b.action === nothing || _mutex_call(b.action, b.agent, b) b.onend === nothing || _mutex_call(b.onend, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end b.done = true delete!(b.agent._behaviors, b) @@ -1496,7 +1506,7 @@ function action(b::CyclicBehavior) try b.action === nothing || _mutex_call(b.action, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end yield() else @@ -1505,7 +1515,7 @@ function action(b::CyclicBehavior) end b.onend === nothing || _mutex_call(b.onend, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end b.done = true delete!(b.agent._behaviors, b) @@ -1594,7 +1604,7 @@ function action(b::WakerBehavior) end b.onend === nothing || _mutex_call(b.onend, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end b.done = true delete!(b.agent._behaviors, b) @@ -1666,12 +1676,12 @@ function action(b::TickerBehavior) try b.done || b.action === nothing || _mutex_call(b.action, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end end b.onend === nothing || _mutex_call(b.onend, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end b.done = true delete!(b.agent._behaviors, b) @@ -1733,12 +1743,12 @@ function action(b::PoissonBehavior) try b.done || b.action === nothing || _mutex_call(b.action, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end end b.onend === nothing || _mutex_call(b.onend, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end b.done = true delete!(b.agent._behaviors, b) @@ -1814,12 +1824,12 @@ function action(b::MessageBehavior) msg = take!(ch) msg === nothing || b.action === nothing || _mutex_call(b.action, b.agent, b, msg) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end end b.onend === nothing || _mutex_call(b.onend, b.agent, b) catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) finally _dont_listen(b.agent, ch) close(ch) @@ -2056,7 +2066,7 @@ function _paramreq_action(a::Agent, b::MessageBehavior, msg::ParameterReq) end end catch ex - reconnect(container(a), ex) || reporterror(a, ex) + reconnect(container(a), ex) || logerror(ex, a) end end rmsg = ParameterRsp(performative=Performative.INFORM, inReplyTo=msg.messageID, recipient=msg.sender, readonly=ro, index=ndx) diff --git a/src/coroutine_behavior.jl b/src/coroutine_behavior.jl index a467ee5..e995819 100644 --- a/src/coroutine_behavior.jl +++ b/src/coroutine_behavior.jl @@ -52,15 +52,13 @@ end function action(b::CoroutineBehavior) b.control_task = current_task() b.action_task = Task() do - try + logerror(b.agent) do b.action(b.agent, b) - catch e - reporterror(b.agent, e) end b.done = true yieldto(b.control_task) end - try + logerror(b.agent) do while !b.done if !isnothing(b.block) lock(() -> wait(b.block), b.block) @@ -69,8 +67,6 @@ function action(b::CoroutineBehavior) yieldto(b.action_task) end end - catch ex - reporterror(b.agent, ex) end b.done = true b.control_task = nothing diff --git a/src/fsm.jl b/src/fsm.jl index 72bf756..5b8b056 100644 --- a/src/fsm.jl +++ b/src/fsm.jl @@ -214,7 +214,7 @@ function action(b::FSMBehavior) _mutex_call(onenter, b.agent, b, b.state) end catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end yield() else @@ -222,7 +222,7 @@ function action(b::FSMBehavior) end end catch ex - reconnect(container(b.agent), ex) || reporterror(b.agent, ex) + reconnect(container(b.agent), ex) || logerror(ex, b.agent) end delete!(b.agent._behaviors, b) b.agent = nothing