Skip to content

Commit 5b042a2

Browse files
committed
web: send startup output to browser Tcl console
Move WebServer::serve() before showSplash() so the WebLogSink captures all Logger output from the start. Buffer pending log lines when no browser is connected yet, then flush the accumulated output when the first WebSocket session accepts. Signed-off-by: Matt Liberty <mliberty@precisioninno.com>
1 parent 191e997 commit 5b042a2

5 files changed

Lines changed: 35 additions & 16 deletions

File tree

src/Main.cc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -381,22 +381,8 @@ static int tclAppInit(int& argc,
381381
ord::initOpenRoad(
382382
interp, log_filename, metrics_filename, exit_after_cmd_file);
383383

384-
bool no_splash = findCmdLineFlag(argc, argv, "-no_splash");
385-
if (!no_splash) {
386-
showSplash();
387-
}
388-
389-
const char* threads = findCmdLineKey(argc, argv, "-threads");
390-
if (threads) {
391-
ord::OpenRoad::openRoad()->setThreadCount(threads, !no_splash);
392-
} else {
393-
// set to default number of threads
394-
ord::OpenRoad::openRoad()->setThreadCount(
395-
ord::OpenRoad::openRoad()->getThreadCount(), false);
396-
}
397-
398-
// Start the web server before sourcing the script so the user can
399-
// watch execution in real-time (analogous to -gui).
384+
// Start the web server before splash/thread output so the
385+
// WebLogSink captures all startup messages for the browser console.
400386
if (web_enabled) {
401387
int port = 0;
402388
if (web_port_arg) {
@@ -411,6 +397,20 @@ static int tclAppInit(int& argc,
411397
ord::OpenRoad::openRoad()->getWebServer()->serve(port);
412398
}
413399

400+
bool no_splash = findCmdLineFlag(argc, argv, "-no_splash");
401+
if (!no_splash) {
402+
showSplash();
403+
}
404+
405+
const char* threads = findCmdLineKey(argc, argv, "-threads");
406+
if (threads) {
407+
ord::OpenRoad::openRoad()->setThreadCount(threads, !no_splash);
408+
} else {
409+
// set to default number of threads
410+
ord::OpenRoad::openRoad()->setThreadCount(
411+
ord::OpenRoad::openRoad()->getThreadCount(), false);
412+
}
413+
414414
// gui::Gui::enabled() is true when a HeadlessViewer is installed
415415
// (which the web server does). But addRestoreStateCommand() only
416416
// works with the Qt event loop — the web server executes scripts

src/web/src/web.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ void WebSocketSession::on_accept(beast::error_code ec)
445445
}
446446
net::post(self->strand_, std::move(fn));
447447
});
448+
449+
// Flush any log output that accumulated before this client
450+
// connected (splash screen, script output, etc.).
451+
viewer_hook_->drainLogs();
448452
}
449453

450454
// Build search indices in the background; tiles render without shapes

src/web/src/web_serve.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class WebLogSink : public spdlog::sinks::base_sink<std::mutex>
8686
if (pending_.empty()) {
8787
return;
8888
}
89+
// Keep accumulating when nobody is listening so the first
90+
// client that connects receives the full startup output.
91+
if (!hook_->sessions().hasClients()) {
92+
return;
93+
}
8994
while (!pending_.empty()
9095
&& (pending_.back() == '\n' || pending_.back() == '\r')) {
9196
pending_.pop_back();

src/web/src/web_viewer_hook.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ bool WebViewerHook::isPaused() const
262262
return paused_.load(std::memory_order_acquire);
263263
}
264264

265+
void WebViewerHook::drainLogs()
266+
{
267+
if (drain_logs_) {
268+
drain_logs_();
269+
}
270+
}
271+
265272
void WebViewerHook::setDrainLogsFn(DrainLogsFn fn)
266273
{
267274
drain_logs_ = std::move(fn);

src/web/src/web_viewer_hook.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class WebViewerHook : public gui::HeadlessViewer
8686

8787
SessionRegistry& sessions() { return sessions_; }
8888

89+
// Flush accumulated log output to all connected clients.
90+
void drainLogs();
91+
8992
// --- gui::HeadlessViewer ---
9093
void redraw() override;
9194
void pause(int timeout_ms) override;

0 commit comments

Comments
 (0)