refactor(browser): use subsystem logger

This commit is contained in:
Peter Steinberger
2025-12-21 13:23:51 +00:00
parent 5b2e7d4464
commit efc12ab28d
3 changed files with 14 additions and 29 deletions

View File

@@ -4,8 +4,7 @@ import os from "node:os";
import path from "node:path"; import path from "node:path";
import { ensurePortAvailable } from "../infra/ports.js"; import { ensurePortAvailable } from "../infra/ports.js";
import { logInfo, logWarn } from "../logger.js"; import { createSubsystemLogger } from "../logging.js";
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
import { CONFIG_DIR } from "../utils.js"; import { CONFIG_DIR } from "../utils.js";
import type { ResolvedBrowserConfig } from "./config.js"; import type { ResolvedBrowserConfig } from "./config.js";
import { import {
@@ -13,6 +12,8 @@ import {
DEFAULT_CLAWD_BROWSER_PROFILE_NAME, DEFAULT_CLAWD_BROWSER_PROFILE_NAME,
} from "./constants.js"; } from "./constants.js";
const log = createSubsystemLogger("browser").child("chrome");
export type BrowserExecutable = { export type BrowserExecutable = {
kind: "canary" | "chromium" | "chrome"; kind: "canary" | "chromium" | "chrome";
path: string; path: string;
@@ -321,7 +322,6 @@ export async function isChromeReachable(
export async function launchClawdChrome( export async function launchClawdChrome(
resolved: ResolvedBrowserConfig, resolved: ResolvedBrowserConfig,
runtime: RuntimeEnv = defaultRuntime,
): Promise<RunningChrome> { ): Promise<RunningChrome> {
await ensurePortAvailable(resolved.cdpPort); await ensurePortAvailable(resolved.cdpPort);
@@ -404,15 +404,9 @@ export async function launchClawdChrome(
if (needsDecorate) { if (needsDecorate) {
try { try {
decorateClawdProfile(userDataDir, { color: resolved.color }); decorateClawdProfile(userDataDir, { color: resolved.color });
logInfo( log.info(`🦞 clawd browser profile decorated (${resolved.color})`);
`🦞 clawd browser profile decorated (${resolved.color})`,
runtime,
);
} catch (err) { } catch (err) {
logWarn( log.warn(`clawd browser profile decoration failed: ${String(err)}`);
`clawd browser profile decoration failed: ${String(err)}`,
runtime,
);
} }
} }
@@ -434,9 +428,8 @@ export async function launchClawdChrome(
} }
const pid = proc.pid ?? -1; const pid = proc.pid ?? -1;
logInfo( log.info(
`🦞 clawd browser started (${exe.kind}) on 127.0.0.1:${resolved.cdpPort} (pid ${pid})`, `🦞 clawd browser started (${exe.kind}) on 127.0.0.1:${resolved.cdpPort} (pid ${pid})`,
runtime,
); );
return { return {

View File

@@ -1,6 +1,5 @@
import type { Server } from "node:http"; import type { Server } from "node:http";
import type { RuntimeEnv } from "../runtime.js";
import { createTargetViaCdp } from "./cdp.js"; import { createTargetViaCdp } from "./cdp.js";
import { import {
isChromeReachable, isChromeReachable,
@@ -41,7 +40,6 @@ export type BrowserRouteContext = {
}; };
type ContextOptions = { type ContextOptions = {
runtime: RuntimeEnv;
getState: () => BrowserServerState | null; getState: () => BrowserServerState | null;
setRunning: (running: RunningChrome | null) => void; setRunning: (running: RunningChrome | null) => void;
}; };
@@ -172,7 +170,7 @@ export function createBrowserRouteContext(
); );
} }
const launched = await launchClawdChrome(current.resolved, opts.runtime); const launched = await launchClawdChrome(current.resolved);
opts.setRunning(launched); opts.setRunning(launched);
launched.proc.on("exit", () => { launched.proc.on("exit", () => {
const live = opts.getState(); const live = opts.getState();

View File

@@ -2,8 +2,7 @@ import type { Server } from "node:http";
import express from "express"; import express from "express";
import { loadConfig } from "../config/config.js"; import { loadConfig } from "../config/config.js";
import { logError, logInfo, logWarn } from "../logger.js"; import { createSubsystemLogger } from "../logging.js";
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
import { import {
resolveBrowserConfig, resolveBrowserConfig,
shouldStartLocalBrowserServer, shouldStartLocalBrowserServer,
@@ -15,9 +14,10 @@ import {
} from "./server-context.js"; } from "./server-context.js";
let state: BrowserServerState | null = null; let state: BrowserServerState | null = null;
const log = createSubsystemLogger("browser");
const logServer = log.child("server");
export async function startBrowserControlServerFromConfig( export async function startBrowserControlServerFromConfig(
runtime: RuntimeEnv = defaultRuntime,
): Promise<BrowserServerState | null> { ): Promise<BrowserServerState | null> {
if (state) return state; if (state) return state;
@@ -26,9 +26,8 @@ export async function startBrowserControlServerFromConfig(
if (!resolved.enabled) return null; if (!resolved.enabled) return null;
if (!shouldStartLocalBrowserServer(resolved)) { if (!shouldStartLocalBrowserServer(resolved)) {
logInfo( logServer.info(
`browser control URL is non-loopback (${resolved.controlUrl}); skipping local server start`, `browser control URL is non-loopback (${resolved.controlUrl}); skipping local server start`,
runtime,
); );
return null; return null;
} }
@@ -37,7 +36,6 @@ export async function startBrowserControlServerFromConfig(
app.use(express.json({ limit: "1mb" })); app.use(express.json({ limit: "1mb" }));
const ctx = createBrowserRouteContext({ const ctx = createBrowserRouteContext({
runtime,
getState: () => state, getState: () => state,
setRunning: (running) => { setRunning: (running) => {
if (state) state.running = running; if (state) state.running = running;
@@ -50,9 +48,8 @@ export async function startBrowserControlServerFromConfig(
const s = app.listen(port, "127.0.0.1", () => resolve(s)); const s = app.listen(port, "127.0.0.1", () => resolve(s));
s.once("error", reject); s.once("error", reject);
}).catch((err) => { }).catch((err) => {
logError( logServer.error(
`clawd browser server failed to bind 127.0.0.1:${port}: ${String(err)}`, `clawd browser server failed to bind 127.0.0.1:${port}: ${String(err)}`,
runtime,
); );
return null; return null;
}); });
@@ -67,21 +64,18 @@ export async function startBrowserControlServerFromConfig(
resolved, resolved,
}; };
logInfo( logServer.info(
`🦞 clawd browser control listening on http://127.0.0.1:${port}/`, `🦞 clawd browser control listening on http://127.0.0.1:${port}/`,
runtime,
); );
return state; return state;
} }
export async function stopBrowserControlServer( export async function stopBrowserControlServer(
runtime: RuntimeEnv = defaultRuntime,
): Promise<void> { ): Promise<void> {
const current = state; const current = state;
if (!current) return; if (!current) return;
const ctx = createBrowserRouteContext({ const ctx = createBrowserRouteContext({
runtime,
getState: () => state, getState: () => state,
setRunning: (running) => { setRunning: (running) => {
if (state) state.running = running; if (state) state.running = running;
@@ -91,7 +85,7 @@ export async function stopBrowserControlServer(
try { try {
await ctx.stopRunningBrowser(); await ctx.stopRunningBrowser();
} catch (err) { } catch (err) {
logWarn(`clawd browser stop failed: ${String(err)}`, runtime); logServer.warn(`clawd browser stop failed: ${String(err)}`);
} }
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {