move logging.js to ts

pull/2239/head
Audric Ackermann 3 years ago
parent 7d570fec52
commit 15260c9718
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

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

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

@ -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;
};

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

@ -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 // We pull these dependencies in now, from here, because they have Node.js dependencies
require('./js/logging'); require('./ts/util/logging');
if (config.proxyUrl) { if (config.proxyUrl) {
window.log.info('Using provided proxy url'); window.log.info('Using provided proxy url');

@ -4,8 +4,18 @@ import moment from 'moment';
import * as Attachment from '../../types/Attachment'; import * as Attachment from '../../types/Attachment';
import * as MIME from '../../types/MIME'; import * as MIME from '../../types/MIME';
import { SignalService } from '../../protobuf'; 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 // tslint:disable-next-line: max-func-body-length
describe('Attachment', () => { describe('Attachment', () => {

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

2
ts/window.d.ts vendored

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

Loading…
Cancel
Save