move logging.js to ts

This commit is contained in:
Audric Ackermann 2022-03-23 16:35:29 +11:00
parent 7d570fec52
commit 15260c9718
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4
8 changed files with 50 additions and 43 deletions

View File

@ -20,4 +20,4 @@ window.closeAbout = () => ipcRenderer.send('close-about');
window.i18n = i18n.setup(locale, localeMessages);
require('./js/logging');
require('./ts/util/logging');

View File

@ -23,7 +23,7 @@ window.nodeSetImmediate = setImmediate;
window.getNodeVersion = () => config.node_version;
window.getEnvironment = () => config.environment;
require('./js/logging');
require('./ts/util/logging');
const os = require('os');
window.getOSRelease = () => `${os.type()} ${os.release} ${os.platform()}`;

View File

@ -1,11 +0,0 @@
exports.stringToArrayBuffer = string => {
if (typeof string !== 'string') {
throw new TypeError("'string' must be a string");
}
const array = new Uint8Array(string.length);
for (let i = 0; i < string.length; i += 1) {
array[i] = string.charCodeAt(i);
}
return array.buffer;
};

View File

@ -47,4 +47,4 @@ window.onLogin = passPhrase =>
ipcRenderer.send('password-window-login', passPhrase);
});
require('./js/logging');
require('./ts/util/logging');

View File

@ -177,7 +177,7 @@ ipc.on('get-ready-for-shutdown', async () => {
// We pull these dependencies in now, from here, because they have Node.js dependencies
require('./js/logging');
require('./ts/util/logging');
if (config.proxyUrl) {
window.log.info('Using provided proxy url');

View File

@ -4,8 +4,18 @@ import moment from 'moment';
import * as Attachment from '../../types/Attachment';
import * as MIME from '../../types/MIME';
import { SignalService } from '../../protobuf';
// @ts-ignore
import { stringToArrayBuffer } from '../../../js/modules/string_to_array_buffer';
const stringToArrayBuffer = (str: string) => {
if (typeof str !== 'string') {
throw new TypeError("'string' must be a string");
}
const array = new Uint8Array(str.length);
for (let i = 0; i < str.length; i += 1) {
array[i] = str.charCodeAt(i);
}
return array.buffer;
};
// tslint:disable-next-line: max-func-body-length
describe('Attachment', () => {

View File

@ -3,17 +3,17 @@
/* eslint strict: ['error', 'never'] */
/* eslint-disable no-console */
const { ipcRenderer } = require('electron');
const _ = require('lodash');
import { ipcRenderer } from 'electron';
import _ from 'lodash';
const Privacy = require('../ts/util/privacy');
import { redactAll } from './privacy';
const ipc = ipcRenderer;
// Default Bunyan levels: https://github.com/trentm/node-bunyan#levels
// To make it easier to visually scan logs, we make all levels the same length
const BLANK_LEVEL = ' ';
const LEVELS = {
const LEVELS: Record<number, string> = {
60: 'fatal',
50: 'error',
40: 'warn ',
@ -29,8 +29,8 @@ function now() {
}
// To avoid [Object object] in our log since console.log handles non-strings smoothly
function cleanArgsForIPC(args) {
const str = args.map(item => {
function cleanArgsForIPC(args: any) {
const str = args.map((item: any) => {
if (typeof item !== 'string') {
try {
return JSON.stringify(item);
@ -45,19 +45,19 @@ function cleanArgsForIPC(args) {
return str.join(' ');
}
function log(...args) {
function log(...args: any) {
logAtLevel('info', 'INFO ', ...args);
}
if (window.console) {
console._log = console.log;
console.log = log;
console._trace = console.trace;
console._debug = console.debug;
console._info = console.info;
console._warn = console.warn;
console._error = console.error;
console._fatal = console.error;
(console as any)._log = (console as any).log;
(console as any).log = log;
(console as any)._trace = (console as any).trace;
(console as any)._debug = (console as any).debug;
(console as any)._info = (console as any).info;
(console as any)._warn = (console as any).warn;
(console as any)._error = (console as any).error;
(console as any)._fatal = (console as any).error;
}
// The mechanics of preparing a log for publish
@ -71,7 +71,7 @@ function getHeader() {
return header;
}
function getLevel(level) {
function getLevel(level: number) {
const text = LEVELS[level];
if (!text) {
return BLANK_LEVEL;
@ -80,19 +80,25 @@ function getLevel(level) {
return text.toUpperCase();
}
function formatLine(entry) {
type EntryType = {
level: number;
time: number;
msg: string;
};
function formatLine(entry: EntryType) {
return `${getLevel(entry.level)} ${entry.time} ${entry.msg}`;
}
function format(entries) {
return Privacy.redactAll(entries.map(formatLine).join('\n'));
function format(entries: Array<EntryType>) {
return redactAll(entries.map(formatLine).join('\n'));
}
function fetch() {
async function fetch() {
return new Promise(resolve => {
ipc.send('fetch-log');
ipc.on('fetched-log', (event, text) => {
ipc.on('fetched-log', (_event, text) => {
const result = `${getHeader()}\n${format(text)}`;
resolve(result);
});
@ -104,16 +110,16 @@ const development = window.getEnvironment() !== 'production';
// A modern logging interface for the browser
// The Bunyan API: https://github.com/trentm/node-bunyan#log-method-api
function logAtLevel(level, prefix, ...args) {
function logAtLevel(level: string, prefix: string, ...args: any) {
if (development) {
const fn = `_${level}`;
console[fn](prefix, now(), ...args);
(console as any)[fn](prefix, now(), ...args);
} else {
console._log(prefix, now(), ...args);
(console as any)._log(prefix, now(), ...args);
}
const str = cleanArgsForIPC(args);
const logText = Privacy.redactAll(str);
const logText = redactAll(str);
ipc.send(`log-${level}`, logText);
}
@ -127,7 +133,7 @@ window.log = {
fetch,
};
window.onerror = (message, script, line, col, error) => {
window.onerror = (_message, _script, _line, _col, error) => {
const errorInfo = error && error.stack ? error.stack : JSON.stringify(error);
window.log.error(`Top-level unhandled error: ${errorInfo}`);
};

2
ts/window.d.ts vendored
View File

@ -65,6 +65,8 @@ declare global {
platform: string;
openFromNotification: (convoId: string) => void;
getEnvironment: () => string;
getNodeVersion: () => string;
contextMenuShown: boolean;
inboxStore?: Store;