67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import fs from "fs"
|
|
import os from "os"
|
|
import path from "path"
|
|
|
|
const LOG_DIR = path.join(os.homedir(), ".opencode-matrix-plugin")
|
|
const LOG_FILE = path.join(LOG_DIR, "plugin.log")
|
|
|
|
let appLog: ((data: any) => Promise<void>) | null = null
|
|
|
|
export function setAppLog(fn: (data: any) => Promise<void>): void {
|
|
appLog = fn
|
|
}
|
|
|
|
export function log(message: string): void {
|
|
// Try OpenCode's logging API first
|
|
if (appLog) {
|
|
appLog({
|
|
service: "matrix-plugin",
|
|
level: "info",
|
|
message,
|
|
}).catch(() => {
|
|
// Fallback to file logging
|
|
try {
|
|
fs.mkdirSync(LOG_DIR, { recursive: true })
|
|
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ${message}\n`)
|
|
} catch {
|
|
// silent
|
|
}
|
|
})
|
|
} else {
|
|
// No app log available, use file logging
|
|
try {
|
|
fs.mkdirSync(LOG_DIR, { recursive: true })
|
|
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ${message}\n`)
|
|
} catch {
|
|
// silent
|
|
}
|
|
}
|
|
}
|
|
|
|
export function logError(message: string): void {
|
|
// Try OpenCode's logging API first
|
|
if (appLog) {
|
|
appLog({
|
|
service: "matrix-plugin",
|
|
level: "error",
|
|
message,
|
|
}).catch(() => {
|
|
// Fallback to file logging
|
|
try {
|
|
fs.mkdirSync(LOG_DIR, { recursive: true })
|
|
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ERROR: ${message}\n`)
|
|
} catch {
|
|
// silent
|
|
}
|
|
})
|
|
} else {
|
|
// No app log available, use file logging
|
|
try {
|
|
fs.mkdirSync(LOG_DIR, { recursive: true })
|
|
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ERROR: ${message}\n`)
|
|
} catch {
|
|
// silent
|
|
}
|
|
}
|
|
}
|