diff --git a/content/ajv.ts b/content/ajv.ts index 595a04e4c..1ad1a1acc 100644 --- a/content/ajv.ts +++ b/content/ajv.ts @@ -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, diff --git a/content/auto-export.ts b/content/auto-export.ts index c7eceb376..9de50598c 100644 --- a/content/auto-export.ts +++ b/content/auto-export.ts @@ -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' @@ -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) @@ -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 }}) } diff --git a/content/better-bibtex.ts b/content/better-bibtex.ts index 9d6b87bec..f3abc579b 100644 --- a/content/better-bibtex.ts +++ b/content/better-bibtex.ts @@ -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 diff --git a/content/clean_pane_persist.ts b/content/clean_pane_persist.ts index d456efb34..8de1f3822 100644 --- a/content/clean_pane_persist.ts +++ b/content/clean_pane_persist.ts @@ -1,4 +1,4 @@ -import { log } from './logger/simple' +import { log } from './logger' import { is7 } from './client' export function clean_pane_persist(): void { diff --git a/content/db/cache.ts b/content/db/cache.ts index 504e27aa7..bc81dafa3 100644 --- a/content/db/cache.ts +++ b/content/db/cache.ts @@ -1,4 +1,4 @@ -import { log } from '../logger/simple' +import { log } from '../logger' import { stringify } from '../stringify' import { is7, worker } from '../client' @@ -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() diff --git a/content/flash.ts b/content/flash.ts index 18c33c7ae..abb280de7 100644 --- a/content/flash.ts +++ b/content/flash.ts @@ -1,4 +1,4 @@ -import { log } from './logger/simple' +import { log } from './logger' const seconds = 1000 diff --git a/content/item-export-format.ts b/content/item-export-format.ts index 7a3cef4b1..a5e995c95 100644 --- a/content/item-export-format.ts +++ b/content/item-export-format.ts @@ -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 diff --git a/content/key-manager/chinese.ts b/content/key-manager/chinese.ts index 3a6a61103..93b5e7a67 100644 --- a/content/key-manager/chinese.ts +++ b/content/key-manager/chinese.ts @@ -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 diff --git a/content/logger.ts b/content/logger.ts index b40949560..76f97bacd 100644 --- a/content/logger.ts +++ b/content/logger.ts @@ -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 || '' }\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 || '' }]` + 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) { - 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`) +} diff --git a/content/logger/simple.ts b/content/logger/simple.ts deleted file mode 100644 index 5b2230c68..000000000 --- a/content/logger/simple.ts +++ /dev/null @@ -1,99 +0,0 @@ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-empty-function, no-restricted-syntax */ - -declare const Zotero: any - -import * as client from '../client' -const version = require('../../gen/version.js') -export const run = `<${version} ${client.run}>` - -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 {}, -} - -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]' -} - -function stringifyError(obj) { - if (obj instanceof Error) return `[error: ${ obj.message || '' }\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 || '' }]` - return '' -} - -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 (value === null) return value - if (value instanceof Set) return [...value] - if (value instanceof Map) return Object.fromEntries(value) - - switch (typeof value) { - case 'string': - case 'number': - case 'boolean': - return value - - case 'object': - return stringifyXPCOM(value) || stringifyError(value) || value - } - - if (Array.isArray(value)) return value - - return undefined - } -} - -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(' ') -} - -function prefix(error?: any) { - return `{${ error ? 'error: ' : '' }${ client.worker ? 'worker: ' : '' }better-bibtex: ${run}} ` -} - -export function $dump(msg: string, error?: Error): void { - dump(`${prefix(error)}${format(msg, error)}\n`) -} - -export function trace(msg: string, mode = ''): void { - dump(`trace${ mode }\t${ Date.now() }\t${ msg }\n`) -} - -export const log = { - debug(...msg): void { - Zotero.debug(`${prefix()}${format(...msg)}\n`) - }, - info(...msg): void { - Zotero.debug(`${prefix()}${format(...msg)}\n`) - }, - error(...msg): void { - Zotero.debug(`${prefix(true)}${format(...msg)}\n`) - }, - dump(msg: string): void { - $dump(msg) - }, -} diff --git a/content/monkey-patch.ts b/content/monkey-patch.ts index a8f1f0141..c8a16cc29 100644 --- a/content/monkey-patch.ts +++ b/content/monkey-patch.ts @@ -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 diff --git a/content/translators.ts b/content/translators.ts index 508196d0c..e7bd2c6cc 100644 --- a/content/translators.ts +++ b/content/translators.ts @@ -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' @@ -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 diff --git a/package-lock.json b/package-lock.json index e25f80ff6..379b996c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "zotero-better-bibtex", - "version": "6.7.256", + "version": "6.7.257", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "zotero-better-bibtex", - "version": "6.7.256", + "version": "6.7.257", "license": "ISC", "dependencies": { "@artsy/to-title-case": "^1.1.0", @@ -54,7 +54,7 @@ "punycode2": "^1.0.1", "reserved-identifiers": "^1.0.0", "safe-stable-stringify": "^2.5.0", - "shell-quote": "^1.8.1", + "shell-quote": "^1.8.2", "sprintf-js": "^1.1.3", "string.prototype.matchall": "^4.0.11", "text-title-case": "^1.0.2", @@ -65,7 +65,7 @@ "uzip": "^0.20201231.0", "words-to-numbers": "^1.5.1", "xregexp": "^5.1.1", - "zotero-plugin": "3.1.0" + "zotero-plugin": "3.1.1" }, "devDependencies": { "@retorquere/bibtex-parser": "^9.0.17", @@ -123,7 +123,7 @@ "process": "^0.11.10", "properties-reader": "^2.3.0", "pug": "^3.0.3", - "putout": "^36.13.1", + "putout": "^36.14.0", "recast": "^0.23.9", "regenerate": "^1.4.2", "rimraf": "^6.0.1", @@ -3160,6 +3160,19 @@ "putout": ">=36" } }, + "node_modules/@putout/plugin-apply-shorthand-properties": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@putout/plugin-apply-shorthand-properties/-/plugin-apply-shorthand-properties-6.0.1.tgz", + "integrity": "sha512-pGkLkOeybKABlG1NU8LcY2Q6dJ6KAjkaC4H+fjkMhrBvlnZXuKJTnpKGs19KdFgbepeWVTNnrwnVnJHjXlTq+w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "peerDependencies": { + "putout": ">=36" + } + }, "node_modules/@putout/plugin-apply-starts-with": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@putout/plugin-apply-starts-with/-/plugin-apply-starts-with-1.1.0.tgz", @@ -16857,9 +16870,9 @@ } }, "node_modules/putout": { - "version": "36.13.1", - "resolved": "https://registry.npmjs.org/putout/-/putout-36.13.1.tgz", - "integrity": "sha512-c0c9s04xmMgG4w2yljG/WsIupD+O1j4COUqzSkaVV1iXmyA9IIjBR5wl/YFsZyJpUE5zd78+FEbis1ruzbYl/w==", + "version": "36.14.0", + "resolved": "https://registry.npmjs.org/putout/-/putout-36.14.0.tgz", + "integrity": "sha512-NAiUCuiy8Rqu5DRtiurm02P/aM2K6Y6+Ow6I9X3eOSHT1WJWQvPahGs2fSSxS4gSD4iXOjnTelQd1b7qg0MHXg==", "dev": true, "license": "MIT", "dependencies": { @@ -16904,6 +16917,7 @@ "@putout/plugin-apply-flat-map": "^2.0.0", "@putout/plugin-apply-optional-chaining": "^6.0.0", "@putout/plugin-apply-overrides": "^2.0.0", + "@putout/plugin-apply-shorthand-properties": "^6.0.0", "@putout/plugin-apply-starts-with": "^1.0.0", "@putout/plugin-apply-template-literals": "^3.0.0", "@putout/plugin-browserlist": "^2.0.0", @@ -18374,10 +18388,13 @@ } }, "node_modules/shell-quote": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.8.1.tgz", - "integrity": "sha512-6j1W9l1iAs/4xYBI1SYOVZyFcCis9b4KCLQ8fgAGG07QvzaRLVVRQvAy85yNmmZSjYjg4MWh4gNvlPujU/5LpA==", + "version": "1.8.2", + "resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.8.2.tgz", + "integrity": "sha512-AzqKpGKjrj7EM6rKVQEPpB288oCfnrEIuyoT9cyF4nmGa7V8Zk6f7RRqYisX8X9m+Q7bd632aZW4ky7EhbQztA==", "license": "MIT", + "engines": { + "node": ">= 0.4" + }, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -20962,9 +20979,9 @@ } }, "node_modules/zotero-plugin": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/zotero-plugin/-/zotero-plugin-3.1.0.tgz", - "integrity": "sha512-Zg3nSfepbO6NsucZQel3vv+R0T3fjDoJ2IMGyUgRJyp119wW4bTXbQfs0VAu3uloPI1RvhKX14mxNz96K3ctvg==", + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/zotero-plugin/-/zotero-plugin-3.1.1.tgz", + "integrity": "sha512-BqfMVcAuC+ofY+XzOtmitppw2Fhm6ylQ18hnwBul1JFnpKC7xqvtLZAxpgTaKqdlLOU4mnDN4KLk7I5CEJBA3w==", "license": "ISC", "dependencies": { "@octokit/rest": "^21.0.2", diff --git a/package.json b/package.json index e8f035eb0..a21a0c686 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zotero-better-bibtex", - "version": "6.7.256", + "version": "6.7.257", "description": "Make Zotero useful for us LaTeX holdouts.", "homepage": "https://retorque.re/zotero-better-bibtex", "license": "ISC", @@ -109,7 +109,7 @@ "punycode2": "^1.0.1", "reserved-identifiers": "^1.0.0", "safe-stable-stringify": "^2.5.0", - "shell-quote": "^1.8.1", + "shell-quote": "^1.8.2", "sprintf-js": "^1.1.3", "string.prototype.matchall": "^4.0.11", "text-title-case": "^1.0.2", @@ -120,7 +120,7 @@ "uzip": "^0.20201231.0", "words-to-numbers": "^1.5.1", "xregexp": "^5.1.1", - "zotero-plugin": "3.1.0" + "zotero-plugin": "3.1.1" }, "devDependencies": { "@retorquere/bibtex-parser": "^9.0.17", @@ -178,7 +178,7 @@ "process": "^0.11.10", "properties-reader": "^2.3.0", "pug": "^3.0.3", - "putout": "^36.13.1", + "putout": "^36.14.0", "recast": "^0.23.9", "regenerate": "^1.4.2", "rimraf": "^6.0.1", diff --git a/translators/bibtex/bibtex.ts b/translators/bibtex/bibtex.ts index bf602ef02..5f262505f 100644 --- a/translators/bibtex/bibtex.ts +++ b/translators/bibtex/bibtex.ts @@ -1,7 +1,7 @@ declare const Zotero: any import * as escape from '../../content/escape' -import { log } from '../../content/logger/simple' +import { log } from '../../content/logger' import { Exporter as BibTeXExporter } from './exporter' import { parse as arXiv } from '../../content/arXiv' import { validItem } from '../../content/ajv' diff --git a/translators/bibtex/entry.ts b/translators/bibtex/entry.ts index c2db9f86e..2ae3e45b8 100644 --- a/translators/bibtex/entry.ts +++ b/translators/bibtex/entry.ts @@ -20,7 +20,7 @@ import * as ExtraFields from '../../gen/items/extra-fields.json' import { label as propertyLabel } from '../../gen/items/items' import type { Fields as ParsedExtraFields } from '../../content/extra' import { zoteroCreator as ExtraZoteroCreator } from '../../content/extra' -import { log } from '../../content/logger/simple' +import { log } from '../../content/logger' import { babelLanguage, titleCase } from '../../content/text' import BabelTag from '../../gen/babel/tag.json' diff --git a/translators/bibtex/unicode_translator.ts b/translators/bibtex/unicode_translator.ts index 34b8d8bcf..3d894ddec 100644 --- a/translators/bibtex/unicode_translator.ts +++ b/translators/bibtex/unicode_translator.ts @@ -3,7 +3,7 @@ import { HTMLParser } from '../../content/text' import type { MarkupNode } from '../../typings/markup' -import { log } from '../../content/logger/simple' +import { log } from '../../content/logger' import HE = require('he') import { Transform } from 'unicode2latex' diff --git a/translators/csl/csl.ts b/translators/csl/csl.ts index 916bbc9d0..8ffbdff90 100644 --- a/translators/csl/csl.ts +++ b/translators/csl/csl.ts @@ -8,7 +8,7 @@ import { simplifyForExport } from '../../gen/items/simplify' import { Fields as ParsedExtraFields, get as getExtra, cslCreator } from '../../content/extra' import type { ExportedItem } from '../../content/db/cache' import * as ExtraFields from '../../gen/items/extra-fields.json' -import { log } from '../../content/logger/simple' +import { log } from '../../content/logger' import { RegularItem } from '../../gen/typings/serialized-item' import * as postscript from '../lib/postscript' import * as dateparser from '../../content/dateparser' diff --git a/translators/csl/yaml.ts b/translators/csl/yaml.ts index a3b5b6e8a..bd66dee25 100644 --- a/translators/csl/yaml.ts +++ b/translators/csl/yaml.ts @@ -7,7 +7,7 @@ import { Translation } from '../lib/translator' import type { MarkupNode } from '../../typings/markup' import { CSLExporter } from './csl' -import { log } from '../../content/logger/simple' +import { log } from '../../content/logger' import { ParsedDate } from '../../content/dateparser' import { HTMLParser } from '../../content/text' diff --git a/translators/lib/bbtjson.ts b/translators/lib/bbtjson.ts index f25a12abc..aa9ceba4d 100644 --- a/translators/lib/bbtjson.ts +++ b/translators/lib/bbtjson.ts @@ -1,7 +1,7 @@ import type { Collected } from './collect' import type { Library } from './normalize' -import { log } from '../../content/logger/simple' +import { log } from '../../content/logger' import { Translation } from './translator' import { simplifyForExport, simplifyForImport } from '../../gen/items/simplify' const version = require('../../gen/version.js') diff --git a/translators/lib/postscript.ts b/translators/lib/postscript.ts index e9938678e..cf3f45095 100644 --- a/translators/lib/postscript.ts +++ b/translators/lib/postscript.ts @@ -7,7 +7,7 @@ export type Allow = { import type { Translation } from '../lib/translator' import type { Fields as ExtraFields } from '../../content/extra' -import { log } from '../../content/logger/simple' +import { log } from '../../content/logger' export type Postscript = (target: any, source: any, translator: Translation, extra: ExtraFields) => Allow diff --git a/translators/lib/translator.ts b/translators/lib/translator.ts index 70407f5c5..1a20b3c39 100644 --- a/translators/lib/translator.ts +++ b/translators/lib/translator.ts @@ -12,7 +12,7 @@ import { regex as escapeRE } from '../../content/escape' import { Collection, Attachment } from '../../gen/typings/serialized-item' import type { Exporter as BibTeXExporter } from '../bibtex/exporter' import type { CharMap } from 'unicode2latex' -import { log } from '../../content/logger/simple' +import { log } from '../../content/logger' import type { Collected } from './collect' export type Output = {