Add structured runtime logging

This commit is contained in:
morluto
2026-07-12 10:57:34 +08:00
parent 5ed56d63e0
commit 03072e0ea7
16 changed files with 795 additions and 48 deletions
+53 -6
View File
@@ -5,6 +5,7 @@ import { runDoctor } from "./application/Doctor.js";
import { runDirectAnalysis } from "./application/DirectAnalysis.js";
import { runSetup } from "./application/Setup.js";
import { PRODUCT_IDENTITY } from "./identity.js";
import { createLogger, parseLogLevel, type Logger } from "./logger.js";
/**
* Build the one-shot Incur CLI without starting Hopper at import time.
@@ -12,6 +13,12 @@ import { PRODUCT_IDENTITY } from "./identity.js";
* `--mcp` are intercepted by the executable dispatcher before this module loads.
*/
export const createCli = (): ReturnType<typeof Cli.create> => {
const logger = createLogger(
"cli",
process.env.REA_LOG_LEVEL === undefined
? "silent"
: parseLogLevel(process.env.REA_LOG_LEVEL),
);
const cli = Cli.create(PRODUCT_IDENTITY.cliBinary, {
version: "0.1.0",
description: "Install, diagnose, and automate Hopper binary analysis.",
@@ -40,21 +47,26 @@ export const createCli = (): ReturnType<typeof Cli.create> => {
.describe("Approve prerequisite installation"),
}),
alias: { yes: "y" },
run: ({ options }) => runSetup(options.yes),
run: ({ options }) =>
logCliCommand(logger, "setup", () => runSetup(options.yes)),
});
cli.command("doctor", {
description: "Diagnose the local Hopper integration",
options: z.object({
target: z.string().optional().describe("Optional binary path to check"),
}),
run: ({ options }) => runDoctor(options.target),
run: ({ options }) =>
logCliCommand(logger, "doctor", () => runDoctor(options.target)),
});
cli.command("analyze", {
description: "Analyze a binary through the shared Hopper core",
args: z.object({
path: z.string().describe("Binary or Hopper database path"),
}),
run: ({ args }) => runDirectAnalysis(args.path, "binary_overview", {}),
run: ({ args }) =>
logCliCommand(logger, "analyze", () =>
runDirectAnalysis(args.path, "binary_overview", {}, logger),
),
});
cli.command("decompile", {
description: "Decompile an address through the shared Hopper core",
@@ -63,9 +75,44 @@ export const createCli = (): ReturnType<typeof Cli.create> => {
address: z.string().describe("Procedure address"),
}),
run: ({ args }) =>
runDirectAnalysis(args.path, "procedure_pseudo_code", {
procedure: args.address,
}),
logCliCommand(logger, "decompile", () =>
runDirectAnalysis(
args.path,
"procedure_pseudo_code",
{ procedure: args.address },
logger,
),
),
});
return cli;
};
const logCliCommand = async <Value>(
logger: Logger,
command: string,
execute: () => Promise<Value>,
): Promise<Value> => {
const startedAt = performance.now();
try {
const value = await execute();
logger.info(
{
command,
durationMs: Math.round((performance.now() - startedAt) * 100) / 100,
status: "ok",
},
"CLI command completed",
);
return value;
} catch (cause: unknown) {
logger.error(
{
command,
durationMs: Math.round((performance.now() - startedAt) * 100) / 100,
status: "error",
},
"CLI command failed",
);
throw cause;
}
};