feat(tui): move logging to server logs

This commit is contained in:
adamdottv
2025-07-09 08:16:10 -05:00
parent 37a86439c4
commit ca8ce88354
19 changed files with 588 additions and 208 deletions

View File

@@ -11,6 +11,19 @@ import { Log } from "../util/log"
export namespace File {
const log = Log.create({ service: "file" })
export const Info = z
.object({
path: z.string(),
added: z.number().int(),
removed: z.number().int(),
status: z.enum(["added", "deleted", "modified"]),
})
.openapi({
ref: "File",
})
export type Info = z.infer<typeof Info>
export const Event = {
Edited: Bus.event(
"file.edited",
@@ -26,14 +39,14 @@ export namespace File {
const diffOutput = await $`git diff --numstat HEAD`.cwd(app.path.cwd).quiet().nothrow().text()
const changedFiles = []
const changedFiles: Info[] = []
if (diffOutput.trim()) {
const lines = diffOutput.trim().split("\n")
for (const line of lines) {
const [added, removed, filepath] = line.split("\t")
changedFiles.push({
file: filepath,
path: filepath,
added: added === "-" ? 0 : parseInt(added, 10),
removed: removed === "-" ? 0 : parseInt(removed, 10),
status: "modified",
@@ -50,7 +63,7 @@ export namespace File {
const content = await Bun.file(path.join(app.path.root, filepath)).text()
const lines = content.split("\n").length
changedFiles.push({
file: filepath,
path: filepath,
added: lines,
removed: 0,
status: "added",
@@ -68,7 +81,7 @@ export namespace File {
const deletedFiles = deletedOutput.trim().split("\n")
for (const filepath of deletedFiles) {
changedFiles.push({
file: filepath,
path: filepath,
added: 0,
removed: 0, // Could get original line count but would require another git command
status: "deleted",
@@ -78,7 +91,7 @@ export namespace File {
return changedFiles.map((x) => ({
...x,
file: path.relative(app.path.cwd, path.join(app.path.root, x.file)),
path: path.relative(app.path.cwd, path.join(app.path.root, x.path)),
}))
}

View File

@@ -34,25 +34,27 @@ export namespace Ripgrep {
export const Match = z.object({
type: z.literal("match"),
data: z.object({
path: z.object({
text: z.string(),
}),
lines: z.object({
text: z.string(),
}),
line_number: z.number(),
absolute_offset: z.number(),
submatches: z.array(
z.object({
match: z.object({
text: z.string(),
}),
start: z.number(),
end: z.number(),
data: z
.object({
path: z.object({
text: z.string(),
}),
),
}),
lines: z.object({
text: z.string(),
}),
line_number: z.number(),
absolute_offset: z.number(),
submatches: z.array(
z.object({
match: z.object({
text: z.string(),
}),
start: z.number(),
end: z.number(),
}),
),
})
.openapi({ ref: "Match" }),
})
const End = z.object({

View File

@@ -28,7 +28,7 @@ export namespace LSP {
}),
})
.openapi({
ref: "LSP.Symbol",
ref: "Symbol",
})
export type Symbol = z.infer<typeof Symbol>

View File

@@ -621,16 +621,7 @@ export namespace Server {
description: "File status",
content: {
"application/json": {
schema: resolver(
z
.object({
file: z.string(),
added: z.number().int(),
removed: z.number().int(),
status: z.enum(["added", "deleted", "modified"]),
})
.array(),
),
schema: resolver(File.Info.array()),
},
},
},
@@ -641,6 +632,52 @@ export namespace Server {
return c.json(content)
},
)
.post(
"/log",
describeRoute({
description: "Write a log entry to the server logs",
responses: {
200: {
description: "Log entry written successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
zValidator(
"json",
z.object({
service: z.string().openapi({ description: "Service name for the log entry" }),
level: z.enum(["info", "error", "warn"]).openapi({ description: "Log level" }),
message: z.string().openapi({ description: "Log message" }),
extra: z
.record(z.string(), z.any())
.optional()
.openapi({ description: "Additional metadata for the log entry" }),
}),
),
async (c) => {
const { service, level, message, extra } = c.req.valid("json")
const logger = Log.create({ service })
switch (level) {
case "info":
logger.info(message, extra)
break
case "error":
logger.error(message, extra)
break
case "warn":
logger.warn(message, extra)
break
}
return c.json(true)
},
)
return result
}

View File

@@ -2,6 +2,23 @@ import path from "path"
import fs from "fs/promises"
import { Global } from "../global"
export namespace Log {
export type Logger = {
info(message?: any, extra?: Record<string, any>): void
error(message?: any, extra?: Record<string, any>): void
warn(message?: any, extra?: Record<string, any>): void
tag(key: string, value: string): Logger
clone(): Logger
time(
message: string,
extra?: Record<string, any>,
): {
stop(): void
[Symbol.dispose](): void
}
}
const loggers = new Map<string, Logger>()
export const Default = create({ service: "default" })
export interface Options {
@@ -9,7 +26,6 @@ export namespace Log {
}
let logpath = ""
export function file() {
return logpath
}
@@ -47,6 +63,14 @@ export namespace Log {
export function create(tags?: Record<string, any>) {
tags = tags || {}
const service = tags["service"]
if (service && typeof service === "string") {
const cached = loggers.get(service)
if (cached) {
return cached
}
}
function build(message: any, extra?: Record<string, any>) {
const prefix = Object.entries({
...tags,
@@ -60,7 +84,7 @@ export namespace Log {
last = next.getTime()
return [next.toISOString().split(".")[0], "+" + diff + "ms", prefix, message].filter(Boolean).join(" ") + "\n"
}
const result = {
const result: Logger = {
info(message?: any, extra?: Record<string, any>) {
process.stderr.write("INFO " + build(message, extra))
},
@@ -96,6 +120,10 @@ export namespace Log {
},
}
if (service && typeof service === "string") {
loggers.set(service, result)
}
return result
}
}