Skip to content

Commit

Permalink
Merge branch 'master' into gh-3078
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed Nov 27, 2024
2 parents 7721fe0 + 1fcfb62 commit 4277d33
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 189 deletions.
2 changes: 1 addition & 1 deletion content/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 2020 for prefixItems
import AJV from 'ajv/dist/2020'
import { discard, log } from './logger/simple'
import { discard, log } from './logger'

const options = {
strict: false,
Expand Down
4 changes: 1 addition & 3 deletions content/auto-export.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Components.utils.import('resource://gre/modules/FileUtils.jsm')
declare const FileUtils: any

import { log } from './logger/simple'
import { log } from './logger'

import { Shim } from './os'
import * as client from './client'
Expand Down Expand Up @@ -275,7 +275,6 @@ const queue = new class TaskQueue {
}

private async runAsync(path: string) {
log.debug('3065: scheduling', path)
await Zotero.BetterBibTeX.ready

const ae = AutoExport.get(path)
Expand Down Expand Up @@ -665,7 +664,6 @@ export const AutoExport = new class $AutoExport { // eslint-disable-line @typesc
}

public get(path: string): Job {
log.debug('3065: autoexport.get', path, blink.first(this.db, { where: { path }}))
return blink.first(this.db, { where: { path }})
}

Expand Down
1 change: 0 additions & 1 deletion content/better-bibtex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ $Patcher$.schedule(Zotero.Translate.Export.prototype, 'translate', original => f
}

const displayOptions = this._displayOptions || {}
log.debug('3065: displayOptions =', this._displayOptions)

if (this.location) {
if (displayOptions.exportFileData) { // when exporting file data, the user was asked to pick a directory rather than a file
Expand Down
2 changes: 1 addition & 1 deletion content/clean_pane_persist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from './logger/simple'
import { log } from './logger'

import { is7 } from './client'
export function clean_pane_persist(): void {
Expand Down
5 changes: 1 addition & 4 deletions content/db/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from '../logger/simple'
import { log } from '../logger'
import { stringify } from '../stringify'

import { is7, worker } from '../client'
Expand Down Expand Up @@ -364,9 +364,6 @@ class ZoteroSerialized {
return true
})

log.debug('3065: purge', purge.size)
log.debug('3065: fill', fill.length)

let rejected = await allSettled([...purge].map(id => store.delete(id)))
await touched.clear()
await tx.commit()
Expand Down
2 changes: 1 addition & 1 deletion content/flash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from './logger/simple'
import { log } from './logger'

const seconds = 1000

Expand Down
2 changes: 1 addition & 1 deletion content/item-export-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Shim } from './os'
import { is7 } from './client'
const $OS = is7 ? Shim : OS

import { log } from './logger/simple'
import { log } from './logger'
import { getItemsAsync } from './get-items-async'
import type { Attachment, RegularItem, Item, Note } from '../gen/typings/serialized-item'
export type Serialized = RegularItem | Attachment | Item
Expand Down
2 changes: 1 addition & 1 deletion content/key-manager/chinese.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Preference } from '../prefs'
import { Events } from '../events'
// import { CJK } from '../text'
import { discard } from '../logger/simple'
import { discard } from '../logger'

declare const ChromeUtils: any

Expand Down
133 changes: 85 additions & 48 deletions content/logger.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,109 @@
/* eslint-disable @typescript-eslint/no-empty-function, no-restricted-syntax */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-empty-function, no-restricted-syntax */

import type { Translators as Translator } from '../typings/translators'
declare const TranslationWorker: { job: Translator.Worker.Job }
import { $dump, run } from './logger/simple'
declare const Zotero: any

import { stringify } from './stringify'
import { worker } from './client'
import * as client from './client'
const version = require('./../gen/version.js')
export const run = `<${version} ${client.run}>`

function toString(obj): string {
try {
if (typeof obj === 'string') return obj
return stringify(obj, 0)
}
catch (err) {
return stringify(err, 0)
}
declare const dump: (msg: string) => void

export const discard = {
log(): void {},
error(): void {},
warn(): void {},
debug(): void {},
info(): void {},
clear(): void {},
dir(): void {},
table(): void {},
}

export const log = new class Logger {
protected timestamp: number
public prefix = ''
function stringifyXPCOM(obj): string {
if (!obj.QueryInterface) return ''
if (obj.message) return `[XPCOM error ${ obj.message }]`
if (obj.name) return `[XPCOM object ${ obj.name }]`
return '[XPCOM object]'
}

private format({ error = false }, msg) {
if (Array.isArray(msg)) msg = msg.map(toString).join(' ')
function stringifyError(obj) {
if (obj instanceof Error) return `[error: ${ obj.message || '<unspecified error>' }\n${ obj.stack }]`
// guess it is an errorevent
if (obj.error instanceof Error && obj.message) return `[errorevent: ${ obj.message } ${ stringifyError(obj.error) }]`
if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent) return `[errorevent: ${ obj.message || '<unspecified errorevent>' }]`
return ''
}

let prefix = ''
if (worker) {
prefix += ' worker'
if (typeof TranslationWorker !== 'undefined') prefix += `:${ TranslationWorker.job.translator }`
function replacer() {
const seen = new WeakSet
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular]'
seen.add(value)
}

if (error) prefix += ' error:'
if (value === null) return value
if (value instanceof Set) return [...value]
if (value instanceof Map) return Object.fromEntries(value)

return `{better-bibtex ${run} ${ this.prefix }${ prefix }} ${ msg }`
}
switch (typeof value) {
case 'string':
case 'number':
case 'boolean':
return value

public get enabled(): boolean {
return (
(typeof TranslationWorker !== 'undefined' && TranslationWorker.job.debugEnabled)
|| !Zotero
|| Zotero.Debug?.enabled
|| Zotero.Prefs?.get('debug.store')
) as boolean
}
case 'object':
return stringifyXPCOM(value) || stringifyError(value) || value
}

public debug(...msg) {
Zotero.debug(this.format({}, msg))
if (Array.isArray(value)) return value

return undefined
}
}

public info(msg: string) {
Zotero.debug(this.format({}, msg))
function to_s(obj: any): string {
if (typeof obj === 'string') return obj
return JSON.stringify(obj, replacer(), 2)
}

export function format(...msg): void {
msg.map(to_s).join(' ')
}

export const log = new class {
public prefix = ''

#prefix(error?: any) {
return `{${ error ? 'error: ' : '' }${ client.worker ? 'worker: ' : '' }${this.prefix}better-bibtex: ${run}} `
}

public error(...msg) {
Zotero.debug(this.format({ error: true }, msg))
public debug(...msg): void {
Zotero.debug(`${this.#prefix()}${format(...msg)}\n`)
}

public status({ error = false }, ...msg) {
if (error || this.enabled) Zotero.debug(this.format({ error }, msg))
public info(...msg): void {
Zotero.debug(`${this.#prefix()}${format(...msg)}\n`)
}

public async timed(msg: string, code: () => void | Promise<void>) {
const start = Date.now()
await code()
this.debug(msg, 'took', Date.now() - start, 'ms')
public error(...msg): void {
Zotero.debug(`${this.#prefix(true)}${format(...msg)}\n`)
}

public dump(msg: string) {
$dump(msg)
public dump(msg: string, error?: Error): void {
if (error) {
dump(`${this.#prefix(error)}${format(msg, error)}\n`)
}
else {
dump(`${this.#prefix()}${format(msg)}\n`)
}
}
}

export function $dump(msg: string, error?: Error): void {
log.dump(msg, error)
}

export function trace(msg: string, mode = ''): void {
dump(`trace${ mode }\t${ Date.now() }\t${ msg }\n`)
}
99 changes: 0 additions & 99 deletions content/logger/simple.ts

This file was deleted.

2 changes: 1 addition & 1 deletion content/monkey-patch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-function-type, prefer-rest-params, @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-shadow */

export type Trampoline = Function & { disabled?: boolean }
// import { log } from './logger/simple'
// import { log } from './logger'

type Patch = {
object: any
Expand Down
3 changes: 1 addition & 2 deletions content/translators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ declare const ZOTERO_CONFIG: any
import type { Translators as Translator } from '../typings/translators'
import { Preference } from './prefs'
import { affects, Preferences } from '../gen/preferences/meta'
import { log } from './logger/simple'
import { log } from './logger'
import { flash } from './flash'
import { Events } from './events'
import { Pinger } from './ping'
Expand Down Expand Up @@ -248,7 +248,6 @@ export const Translators = new class { // eslint-disable-line @typescript-eslint
exportPath: job.path || undefined,
exportDir: job.path ? $OS.Path.dirname(job.path) : undefined,
}
log.debug('3065: tr.displayOptions =', displayOptions)

if (job.translate) {
// fake out the stuff that complete expects to be set by .translate
Expand Down
Loading

0 comments on commit 4277d33

Please sign in to comment.