Import log instead of using it off of window

This commit is contained in:
Josh Perez 2021-09-17 14:27:53 -04:00 committed by GitHub
parent 8eb0dd3116
commit 65ddf0a9e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
155 changed files with 3654 additions and 3433 deletions

View file

@ -1,6 +1,8 @@
// Copyright 2018-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { memoize, sortBy } from 'lodash';
import { ipcRenderer as ipc } from 'electron';
import { z } from 'zod';
import FormData from 'form-data';
import { gzip } from 'zlib';
@ -8,6 +10,19 @@ import pify from 'pify';
import got, { Response } from 'got';
import { getUserAgent } from '../util/getUserAgent';
import { maybeParseUrl } from '../util/url';
import * as log from './log';
import { reallyJsonStringify } from '../util/reallyJsonStringify';
import {
FetchLogIpcData,
LogEntryType,
LogLevel,
getLogLevelString,
isFetchLogIpcData,
isLogEntry,
levelMaxLength,
} from './shared';
import { redactAll } from '../util/privacy';
import { getEnvironment } from '../environment';
const BASE_URL = 'https://debuglogs.org';
@ -34,7 +49,7 @@ const parseTokenBody = (
return body;
};
export const uploadDebugLogs = async (
export const upload = async (
content: string,
appVersion: string
): Promise<string> => {
@ -62,7 +77,7 @@ export const uploadDebugLogs = async (
filename: `signal-desktop-debug-log-${appVersion}.txt.gz`,
});
window.log.info('Debug log upload starting...');
log.info('Debug log upload starting...');
try {
const { statusCode, body } = await got.post(url, { headers, body: form });
if (statusCode !== 204) {
@ -76,7 +91,92 @@ export const uploadDebugLogs = async (
`Got threw on upload to S3, got status ${response?.statusCode}, body '${response?.body}' `
);
}
window.log.info('Debug log upload complete.');
log.info('Debug log upload complete.');
return `${BASE_URL}/${uploadKey}`;
};
// The mechanics of preparing a log for publish
const headerSectionTitle = (title: string) => `========= ${title} =========`;
const headerSection = (
title: string,
data: Readonly<Record<string, unknown>>
): string => {
const sortedEntries = sortBy(Object.entries(data), ([key]) => key);
return [
headerSectionTitle(title),
...sortedEntries.map(
([key, value]) => `${key}: ${redactAll(String(value))}`
),
'',
].join('\n');
};
const getHeader = ({
capabilities,
remoteConfig,
statistics,
user,
}: Omit<FetchLogIpcData, 'logEntries'>): string =>
[
headerSection('System info', {
Time: Date.now(),
'User agent': window.navigator.userAgent,
'Node version': window.getNodeVersion(),
Environment: getEnvironment(),
'App version': window.getVersion(),
}),
headerSection('User info', user),
headerSection('Capabilities', capabilities),
headerSection('Remote config', remoteConfig),
headerSection('Statistics', statistics),
headerSectionTitle('Logs'),
].join('\n');
const getLevel = memoize((level: LogLevel): string => {
const text = getLogLevelString(level);
return text.toUpperCase().padEnd(levelMaxLength, ' ');
});
function formatLine(mightBeEntry: unknown): string {
const entry: LogEntryType = isLogEntry(mightBeEntry)
? mightBeEntry
: {
level: LogLevel.Error,
msg: `Invalid IPC data when fetching logs. Here's what we could recover: ${reallyJsonStringify(
mightBeEntry
)}`,
time: new Date().toISOString(),
};
return `${getLevel(entry.level)} ${entry.time} ${entry.msg}`;
}
export function fetch(): Promise<string> {
return new Promise(resolve => {
ipc.send('fetch-log');
ipc.on('fetched-log', (_event, data: unknown) => {
let header: string;
let body: string;
if (isFetchLogIpcData(data)) {
const { logEntries } = data;
header = getHeader(data);
body = logEntries.map(formatLine).join('\n');
} else {
header = headerSectionTitle('Partial logs');
const entry: LogEntryType = {
level: LogLevel.Error,
msg: 'Invalid IPC data when fetching logs; dropping all logs',
time: new Date().toISOString(),
};
body = formatLine(entry);
}
const result = `${header}\n${body}`;
resolve(result);
});
});
}