Skip to content

Commit

Permalink
file logging
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed Sep 25, 2024
1 parent 00f8b93 commit 5510c46
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 165 deletions.
58 changes: 55 additions & 3 deletions bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
interfaces: Ci,
results: Cr,
utils: Cu,
Constructor: CC,
Constructor: Cc,
} = Components

if (typeof Zotero == 'undefined') {
Expand Down Expand Up @@ -50,8 +50,8 @@ async function waitForZotero() {
var listener = {
onOpenWindow(aWindow) {
// Wait for the window to finish loading
const domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow)
const domWindow = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowInternal || Components.interfaces.nsIDOMWindow)
domWindow.addEventListener('load', function() {
domWindow.removeEventListener('load', arguments.callee, false)
if (domWindow.Zotero) {
Expand All @@ -72,8 +72,55 @@ export function install() {
// nothing to do
}

function writer(dir: any, name: string): ((m: string) => void) {
const file = dir.clone()
file.append(name)
log(`logging to ${file.path}`)
const rawStream = Components.classes['@mozilla.org/network/file-output-stream;1'].createInstance(Components.interfaces.nsIFileOutputStream)
log('have raw stream')
rawStream.init(file, 0x02 | 0x08 | 0x20, 0o664, 0); // eslint-disable-line no-bitwise
log('raw stream init')
const outputStream = Components.classes['@mozilla.org/intl/converter-output-stream;1'].createInstance(Components.interfaces.nsIConverterOutputStream)
log('have output stream')
outputStream.init(rawStream, 'UTF-8', 1024, '?'.charCodeAt(0));
log('output stream init')

return (m: string) => {
if (!m.match(/\r?\n$/)) m += '\n'
outputStream.writeString(m)
}
}

let logwriter: ReturnType<typeof writer>

export async function startup({ id, version, resourceURI, rootURI = resourceURI.spec }) {
await waitForZotero()

const env = Components.classes['@mozilla.org/process/environment;1'].getService(Components.interfaces.nsIEnvironment)
let path = env.get('ZOTERO_DEBUG_LOG')
log(`log enabled? ${!!path}`)

if (path === '.') {
const dirService = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties)
const cwd = dirService.get('CurWorkD', Components.interfaces.nsIFile)
path = cwd.path
}

if (path) {
const dir = Zotero.File.pathToFile(path)
if (!dir.isDirectory()) {
log(`${JSON.stringify(path)} is not a directory, log-writer disabled`)
}
else {
log(`logging to ${path}`)
const start = new Date
const logid = new Date(start.getTime() - start.getTimezoneOffset() * 60000).toISOString().replace('Z', '').replace(/:/g, '.')
logwriter = writer(dir, `${logid}.txt`)
// Zotero.Debug.addConsoleViewerListener(logwriter)
Zotero.Debug.addListener(logwriter)
}
}

log('registering')
try {
DebugLog.register('Debug Log')
Expand All @@ -85,6 +132,11 @@ export async function startup({ id, version, resourceURI, rootURI = resourceURI.

export function shutdown() {
DebugLog.unregister('Debug Log')
if (logwriter) {
Zotero.Debug.removeListener(logwriter)
Zotero.Debug.removeConsoleViewerListener(logwriter)
logwriter = null
}
}

export function uninstall() {
Expand Down
Loading

0 comments on commit 5510c46

Please sign in to comment.