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

View File

@@ -1,6 +1,5 @@
import type { Server } from "node:http";
import type { RuntimeEnv } from "../runtime.js";
import { createTargetViaCdp } from "./cdp.js";
import {
isChromeReachable,
@@ -41,7 +40,6 @@ export type BrowserRouteContext = {
};
type ContextOptions = {
runtime: RuntimeEnv;
getState: () => BrowserServerState | null;
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);
launched.proc.on("exit", () => {
const live = opts.getState();

View File

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