Skip to content

Commit

Permalink
feat: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Jun 29, 2024
1 parent 6d2a2e9 commit ea927fe
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 26 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"fs-extra": "^11.2.0",
"ignore": "^5.3.1",
"lodash": "^4.17.21",
"rotating-file-stream": "^3.2.3",
"uuid": "^10.0.0"
}
}
8 changes: 7 additions & 1 deletion src/ignorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ export class Ignorer {
return this.cache[path]!
}

const ig = this.instance.ignores(path.startsWith("\\") ? path.slice(1) : path.startsWith("/") ? path.slice(1) : path)
const normalizedPath = path.startsWith("\\") ? path.slice(1) : path.startsWith("/") ? path.slice(1) : path

if (normalizedPath.length === 0) {
return false
}

const ig = this.instance.ignores(normalizedPath)

this.cache[path] = ig

Expand Down
24 changes: 14 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { postMessageToMain } from "./lib/ipc"
import { Semaphore } from "./semaphore"
import { SYNC_INTERVAL } from "./constants"
import { serializeError } from "./utils"
import Logger from "./lib/logger"

/**
* SyncWorker
Expand All @@ -16,11 +17,12 @@ import { serializeError } from "./utils"
* @typedef {SyncWorker}
*/
export class SyncWorker {
private readonly syncPairs: SyncPair[]
private readonly syncs: Record<string, Sync> = {}
private readonly dbPath: string
private readonly initSyncPairsMutex = new Semaphore(1)
private readonly sdk: FilenSDK
public readonly syncPairs: SyncPair[]
public readonly syncs: Record<string, Sync> = {}
public readonly dbPath: string
public readonly initSyncPairsMutex = new Semaphore(1)
public readonly sdk: FilenSDK
public readonly logger: Logger

/**
* Creates an instance of SyncWorker.
Expand All @@ -35,6 +37,7 @@ export class SyncWorker {
public constructor({ syncPairs, dbPath, sdkConfig }: { syncPairs: SyncPair[]; dbPath: string; sdkConfig: FilenSDKConfig }) {
this.syncPairs = syncPairs
this.dbPath = dbPath
this.logger = new Logger(dbPath)
this.sdk = new FilenSDK({
...sdkConfig,
connectToSocket: true,
Expand Down Expand Up @@ -65,9 +68,7 @@ export class SyncWorker {
type: "syncPairsUpdated"
})
} catch (e) {
// TODO: Proper debugger

console.error(e)
this.logger.log("error", e, "index.setupMainThreadListeners")

if (e instanceof Error) {
postMessageToMain({
Expand Down Expand Up @@ -126,15 +127,18 @@ export class SyncWorker {
if (!this.syncs[pair.uuid]) {
this.syncs[pair.uuid] = new Sync({
syncPair: pair,
dbPath: this.dbPath,
sdk: this.sdk
worker: this
})

promises.push(this.syncs[pair.uuid]!.initialize())
}
}

await Promise.all(promises)
} catch (e) {
this.logger.log("error", e, "index.initSyncPairs")

throw e
} finally {
this.initSyncPairsMutex.release()
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/filesystems/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export class LocalFileSystem {
tree[entryPath] = item
inodes[stats.ino] = item
} catch (e) {
this.sync.worker.logger.log("error", e, "filesystems.local.getDirectoryTree")

if (e instanceof Error) {
errors.push({
localPath: itemPath,
Expand Down Expand Up @@ -451,6 +453,8 @@ export class LocalFileSystem {
pauseSignal: this.sync.pauseSignals[signalKey],
abortSignal: this.sync.abortControllers[signalKey]?.signal,
onError: err => {
this.sync.worker.logger.log("error", err, "filesystems.local.upload")

postMessageToMain({
type: "transfer",
syncPair: this.sync.syncPair,
Expand Down Expand Up @@ -517,6 +521,8 @@ export class LocalFileSystem {

return item
} catch (e) {
this.sync.worker.logger.log("error", e, "filesystems.local.upload")

if (e instanceof Error) {
postMessageToMain({
type: "transfer",
Expand Down
16 changes: 8 additions & 8 deletions src/lib/filesystems/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ export class RemoteFileSystem {

tree[folderPath] = item
uuids[folder[0]] = item
} catch {
// TODO: Proper logger
} catch (e) {
this.sync.worker.logger.log("error", e, "filesystems.remote.getDirectoryTree")
}
}

Expand Down Expand Up @@ -343,8 +343,8 @@ export class RemoteFileSystem {

tree[filePath] = item
uuids[item.uuid] = item
} catch {
// TODO: Proper logger
} catch (e) {
this.sync.worker.logger.log("error", e, "filesystems.remote.getDirectoryTree")
}
})
)
Expand Down Expand Up @@ -805,6 +805,8 @@ export class RemoteFileSystem {
pauseSignal: this.sync.pauseSignals[signalKey],
abortSignal: this.sync.abortControllers[signalKey]?.signal,
onError: err => {
this.sync.worker.logger.log("error", err, "filesystems.remote.download")

postMessageToMain({
type: "transfer",
syncPair: this.sync.syncPair,
Expand Down Expand Up @@ -863,6 +865,8 @@ export class RemoteFileSystem {

return await fs.stat(localPath)
} catch (e) {
this.sync.worker.logger.log("error", e, "filesystems.remote.download")

if (e instanceof Error) {
postMessageToMain({
type: "transfer",
Expand All @@ -877,10 +881,6 @@ export class RemoteFileSystem {
})
}

// TODO: Proper logging

console.error(e)

throw e
} finally {
delete this.sync.pauseSignals[signalKey]
Expand Down
115 changes: 115 additions & 0 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import pathModule from "path"
import fs from "fs-extra"
import { type RotatingFileStream, createStream } from "rotating-file-stream"
import { Semaphore } from "../semaphore"

export class Logger {
private readonly path: string
private readonly debugPath: string
private readonly errorPath: string
private readonly infoPath: string
private readonly warnPath: string
private readonly debugStream: RotatingFileStream
private readonly infoStream: RotatingFileStream
private readonly errorStream: RotatingFileStream
private readonly warnStream: RotatingFileStream
private readonly infoMutex = new Semaphore(1)
private readonly debugMutex = new Semaphore(1)
private readonly errorMutex = new Semaphore(1)
private readonly warnMutex = new Semaphore(1)

public constructor(dbPath: string) {
this.path = pathModule.join(dbPath, "logs")
this.debugPath = pathModule.join(this.path, "debug.log")
this.errorPath = pathModule.join(this.path, "error.log")
this.infoPath = pathModule.join(this.path, "info.log")
this.warnPath = pathModule.join(this.path, "warn.log")

fs.ensureFileSync(this.debugPath)
fs.ensureFileSync(this.errorPath)
fs.ensureFileSync(this.infoPath)
fs.ensureFileSync(this.warnPath)

this.debugStream = createStream(this.debugPath, {
size: "10M",
interval: "5m",
compress: "gzip",
maxFiles: 3,
encoding: "utf-8"
})

this.infoStream = createStream(this.infoPath, {
size: "10M",
interval: "5m",
compress: "gzip",
maxFiles: 3,
encoding: "utf-8"
})

this.warnStream = createStream(this.warnPath, {
size: "10M",
interval: "5m",
compress: "gzip",
maxFiles: 3,
encoding: "utf-8"
})

this.errorStream = createStream(this.errorPath, {
size: "10M",
interval: "5m",
compress: "gzip",
maxFiles: 3,
encoding: "utf-8"
})
}

public log(level: "info" | "debug" | "warn" | "error", object: unknown, where?: string): void {
// eslint-disable-next-line no-extra-semi
;(async () => {
const mutex =
level === "info"
? this.infoMutex
: level === "debug"
? this.debugMutex
: level === "warn"
? this.warnMutex
: this.errorMutex

await mutex.acquire()

try {
const stream =
level === "info"
? this.infoStream
: level === "debug"
? this.debugStream
: level === "warn"
? this.warnStream
: this.errorStream

if (
!stream.writable ||
stream.destroyed ||
stream.errored ||
stream.closed ||
stream.writableEnded ||
stream.writableFinished
) {
return
}

const log = `[${level}] [${new Date()}] ${where ? `[${where}] ` : ""}${JSON.stringify(object)}`

await new Promise<void>(resolve => {
stream.write(`${log}\n`, () => {
resolve()
})
})
} finally {
mutex.release()
}
})()
}
}

export default Logger
20 changes: 13 additions & 7 deletions src/lib/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { postMessageToMain } from "./ipc"
import { isMainThread, parentPort } from "worker_threads"
import Ignorer from "../ignorer"
import { serializeError } from "../utils"
import type SyncWorker from ".."

/**
* Sync
Expand Down Expand Up @@ -44,24 +45,25 @@ export class Sync {
public paused: boolean
public mode: SyncMode
public excludeDotFiles: boolean
public readonly worker: SyncWorker

/**
* Creates an instance of Sync.
*
* @constructor
* @public
* @param {{ syncPair: SyncPair; dbPath: string; sdk: FilenSDK }} param0
* @param {{ syncPair: SyncPair; worker: SyncWorker }} param0
* @param {SyncPair} param0.syncPair
* @param {string} param0.dbPath
* @param {FilenSDK} param0.sdk
* @param {SyncWorker} param0.worker
*/
public constructor({ syncPair, dbPath, sdk }: { syncPair: SyncPair; dbPath: string; sdk: FilenSDK }) {
public constructor({ syncPair, worker }: { syncPair: SyncPair; worker: SyncWorker }) {
this.worker = worker
this.syncPair = syncPair
this.mode = syncPair.mode
this.paused = syncPair.paused
this.excludeDotFiles = syncPair.excludeDotFiles
this.dbPath = dbPath
this.sdk = sdk
this.dbPath = worker.dbPath
this.sdk = worker.sdk
this.localFileSystem = new LocalFileSystem(this)
this.remoteFileSystem = new RemoteFileSystem(this)
this.deltas = new Deltas(this)
Expand Down Expand Up @@ -143,6 +145,8 @@ export class Sync {

this.run()
} catch (e) {
this.worker.logger.log("error", e, "sync.initialize")

this.isInitialized = false

throw e
Expand Down Expand Up @@ -257,6 +261,8 @@ export class Sync {

console.log({ deltas, localErrors: currentLocalTree.errors })

this.worker.logger.log("info", { deltas, localErrors: currentLocalTree.errors })

postMessageToMain({
type: "cycleProcessingTasksStarted",
syncPair: this.syncPair
Expand Down Expand Up @@ -328,7 +334,7 @@ export class Sync {
syncPair: this.syncPair
})
} catch (e) {
console.error(e) // TODO: Proper debugger
this.worker.logger.log("error", e, "sync.run")

if (e instanceof Error) {
postMessageToMain({
Expand Down
2 changes: 2 additions & 0 deletions src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ export class Tasks {

executed.push(doneTask)
} catch (e) {
this.sync.worker.logger.log("error", e, "tasks.process")

if (e instanceof Error) {
errors.push({
path: delta.path,
Expand Down

0 comments on commit ea927fe

Please sign in to comment.