From d3bc5ec22c612da3df02026dd981a53223b129f3 Mon Sep 17 00:00:00 2001 From: Paul Berg Date: Wed, 2 Feb 2022 21:14:49 +0100 Subject: [PATCH] respect maxlog keyword for logs, fixes #1858 (#1877) Co-authored-by: Fons van der Plas --- src/evaluation/WorkspaceManager.jl | 20 +++++++++++++++++--- src/runner/PlutoRunner.jl | 2 +- test/Logging.jl | 30 ++++++++++++++++++++++++++++++ test/runtests.jl | 2 ++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 test/Logging.jl diff --git a/src/evaluation/WorkspaceManager.jl b/src/evaluation/WorkspaceManager.jl index 62f2b3b0c9..a606823c88 100644 --- a/src/evaluation/WorkspaceManager.jl +++ b/src/evaluation/WorkspaceManager.jl @@ -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 @@ -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 diff --git a/src/runner/PlutoRunner.jl b/src/runner/PlutoRunner.jl index 46970f73f0..20f1653f8c 100644 --- a/src/runner/PlutoRunner.jl +++ b/src/runner/PlutoRunner.jl @@ -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], ) ) diff --git a/test/Logging.jl b/test/Logging.jl new file mode 100644 index 0000000000..eb0930cc7f --- /dev/null +++ b/test/Logging.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 554de40c76..ba9ea13bff 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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")