Dax Raad 9 месяцев назад
Родитель
Сommit
49ad2efef6
3 измененных файлов с 35 добавлено и 10 удалено
  1. 1 0
      js/src/app/index.ts
  2. 8 7
      js/src/server/server.ts
  3. 26 3
      js/src/util/log.ts

+ 1 - 0
js/src/app/index.ts

@@ -12,6 +12,7 @@ export namespace App {
   const ctx = Context.create<Info>("app");
 
   export async function create(input: { directory: string }) {
+    Log.file(input.directory);
     log.info("creating");
 
     const config = await Config.load(input.directory);

+ 8 - 7
js/src/server/server.ts

@@ -11,10 +11,10 @@ export namespace Server {
   const log = Log.create({ service: "server" });
   const PORT = 16713;
 
-  export type App = ReturnType<typeof listen>;
+  export type App = ReturnType<typeof app>;
 
-  export function listen() {
-    const app = new Hono()
+  function app() {
+    return new Hono()
       .get("/event", async (c) => {
         log.info("event connected");
         return streamSSE(c, async (stream) => {
@@ -51,14 +51,15 @@ export namespace Server {
           return c.json(msg);
         },
       );
+  }
 
-    Bun.serve({
+  export function listen() {
+    const server = Bun.serve({
       port: PORT,
       hostname: "0.0.0.0",
       idleTimeout: 0,
-      fetch: app.fetch,
+      fetch: app().fetch,
     });
-
-    return app;
+    return server;
   }
 }

+ 26 - 3
js/src/util/log.ts

@@ -1,4 +1,27 @@
+import fs from "node:fs";
+import path from "node:path";
+import { AppPath } from "../app/path";
 export namespace Log {
+  const write = {
+    out: (msg: string) => {
+      process.stdout.write(msg);
+    },
+    err: (msg: string) => {
+      process.stderr.write(msg);
+    },
+  };
+
+  export function file(directory: string) {
+    const out = Bun.file(
+      path.join(AppPath.data(directory), "opencode.out.log"),
+    );
+    const err = Bun.file(
+      path.join(AppPath.data(directory), "opencode.err.log"),
+    );
+    write["out"] = (msg) => out.write(msg);
+    write["err"] = (msg) => err.write(msg);
+  }
+
   export function create(tags?: Record<string, any>) {
     tags = tags || {};
 
@@ -9,14 +32,14 @@ export namespace Log {
       })
         .map(([key, value]) => `${key}=${value}`)
         .join(" ");
-      return [prefix, message];
+      return [prefix, message].join(" ");
     }
     const result = {
       info(message?: any, extra?: Record<string, any>) {
-        console.log(...build(message, extra));
+        write.out(build(message, extra));
       },
       error(message?: any, extra?: Record<string, any>) {
-        console.error(...build(message, extra));
+        write.err(build(message, extra));
       },
       tag(key: string, value: string) {
         if (tags) tags[key] = value;