Skip to content

Commit

Permalink
respect maxlog keyword for logs, fixes #1858 (#1877)
Browse files Browse the repository at this point in the history
Co-authored-by: Fons van der Plas <[email protected]>
  • Loading branch information
Pangoraw and fonsp authored Feb 2, 2022
1 parent 7e153ff commit d3bc5ec
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/evaluation/WorkspaceManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ function start_relaying_logs((session, notebook)::SN, log_channel::Distributed.R

fn = next_log["file"]
match = findfirst("#==#", fn)

# We always show the log at the currently running cell, which is given by
running_cell_id = UUID(next_log["cell_id"])
running_cell_id = next_log["cell_id"]::UUID
running_cell = notebook.cells_dict[running_cell_id]

# Some logs originate from outside of the running code, through function calls. Some code here to deal with that:
begin
source_cell_id = if match !== nothing
Expand All @@ -163,6 +163,20 @@ function start_relaying_logs((session, notebook)::SN, log_channel::Distributed.R
end
end

maybe_max_log = findfirst(((key, _),) -> key == "maxlog", next_log["kwargs"])
if maybe_max_log !== nothing
n_logs = count(log -> log["id"] == next_log["id"], running_cell.logs)
try
max_log = parse(Int, next_log["kwargs"][maybe_max_log][2] |> first)

# Don't show message with id more than max_log times
if max_log isa Int && n_logs >= max_log
return
end
catch
end
end

push!(running_cell.logs, next_log)
Pluto.@asynclog update_throttled()
catch e
Expand Down
2 changes: 1 addition & 1 deletion src/runner/PlutoRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2005,7 +2005,7 @@ function Logging.handle_message(::PlutoLogger, level, msg, _module, group, id, f
"file" => file,
"cell_id" => currently_running_cell_id[],
"line" => line,
"kwargs" => Any[(string(k), format_output_default(v)) for (k, v) in kwargs],
"kwargs" => Tuple{String,Any}[(string(k), format_output_default(v)) for (k, v) in kwargs],
)
)

Expand Down
30 changes: 30 additions & 0 deletions test/Logging.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Test
import UUIDs
import Pluto: PlutoRunner, Notebook, WorkspaceManager, Cell, ServerSession, ClientSession, update_run!
using Pluto.WorkspaceManager: poll

@testset "Logging" begin
🍭 = ServerSession()
🍭.options.evaluation.workspace_use_distributed = true

fakeclient = ClientSession(:fake, nothing)
🍭.connected_clients[fakeclient.id] = fakeclient

@testset "Logging respects maxlog" begin
notebook = Notebook(Cell.([
"""
for i in 1:10
@info "logging" i maxlog=2
end
""",
]))

update_run!(🍭, notebook, notebook.cells)
@test notebook.cells[begin] |> noerror

@test poll(5, 1/60) do
length(notebook.cells[begin].logs) == 2
end
WorkspaceManager.unmake_workspace((🍭, notebook))
end
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ include("./Dynamic.jl")
verify_no_running_processes()
include("./MacroAnalysis.jl")
verify_no_running_processes()
include("./Logging.jl")
verify_no_running_processes()
include("./webserver.jl")
verify_no_running_processes()
include("./Notebook.jl")
Expand Down

0 comments on commit d3bc5ec

Please sign in to comment.