[Dev] API: Colored logs (#2052)
This commit is contained in:
parent
7d131a57c1
commit
3f1da18e39
@ -2,6 +2,20 @@ import { Api as GramJs } from '../../lib/gramjs';
|
|||||||
import localDb from './localDb';
|
import localDb from './localDb';
|
||||||
import { buildApiPeerId, getApiChatIdFromMtpPeer } from './apiBuilders/peers';
|
import { buildApiPeerId, getApiChatIdFromMtpPeer } from './apiBuilders/peers';
|
||||||
|
|
||||||
|
const LOG_BACKGROUND = '#111111DD';
|
||||||
|
const LOG_PREFIX_COLOR = '#E4D00A';
|
||||||
|
const LOG_SUFFIX = {
|
||||||
|
INVOKE: '#49DBF5',
|
||||||
|
'INVOKE RESPONSE': '#6887F7',
|
||||||
|
CONNECTING: '#E4D00A',
|
||||||
|
CONNECTED: '#26D907',
|
||||||
|
'CONNECTING ERROR': '#D1191C',
|
||||||
|
'INVOKE ERROR': '#D1191C',
|
||||||
|
UPDATE: '#0DD151',
|
||||||
|
'UNEXPECTED UPDATE': '#9C9C9C',
|
||||||
|
'UNEXPECTED RESPONSE': '#D1191C',
|
||||||
|
};
|
||||||
|
|
||||||
export function resolveMessageApiChatId(mtpMessage: GramJs.TypeMessage) {
|
export function resolveMessageApiChatId(mtpMessage: GramJs.TypeMessage) {
|
||||||
if (!(mtpMessage instanceof GramJs.Message || mtpMessage instanceof GramJs.MessageService)) {
|
if (!(mtpMessage instanceof GramJs.Message || mtpMessage instanceof GramJs.MessageService)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -83,3 +97,18 @@ export function serializeBytes(value: Buffer) {
|
|||||||
export function deserializeBytes(value: string) {
|
export function deserializeBytes(value: string) {
|
||||||
return Buffer.from(value, 'binary');
|
return Buffer.from(value, 'binary');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function log(suffix: keyof typeof LOG_SUFFIX, ...data: any) {
|
||||||
|
/* eslint-disable max-len */
|
||||||
|
/* eslint-disable no-console */
|
||||||
|
const func = suffix === 'UNEXPECTED RESPONSE' ? console.error
|
||||||
|
: suffix === 'INVOKE ERROR' || suffix === 'UNEXPECTED UPDATE' ? console.warn : console.log;
|
||||||
|
/* eslint-enable no-console */
|
||||||
|
func(
|
||||||
|
`%cGramJS%c${suffix}`,
|
||||||
|
`color: ${LOG_PREFIX_COLOR}; background: ${LOG_BACKGROUND}; padding: 0.25rem; border-radius: 0.25rem;`,
|
||||||
|
`color: ${LOG_SUFFIX[suffix]}; background: ${LOG_BACKGROUND}; padding: 0.25rem; border-radius: 0.25rem; margin-left: 0.25rem;`,
|
||||||
|
...data,
|
||||||
|
);
|
||||||
|
/* eslint-enable max-len */
|
||||||
|
}
|
||||||
|
|||||||
@ -27,7 +27,7 @@ import downloadMediaWithClient, { parseMediaUrl } from './media';
|
|||||||
import { buildApiUserFromFull } from '../apiBuilders/users';
|
import { buildApiUserFromFull } from '../apiBuilders/users';
|
||||||
import localDb, { clearLocalDb } from '../localDb';
|
import localDb, { clearLocalDb } from '../localDb';
|
||||||
import { buildApiPeerId } from '../apiBuilders/peers';
|
import { buildApiPeerId } from '../apiBuilders/peers';
|
||||||
import { addMessageToLocalDb } from '../helpers';
|
import { addMessageToLocalDb, log } from '../helpers';
|
||||||
|
|
||||||
const DEFAULT_USER_AGENT = 'Unknown UserAgent';
|
const DEFAULT_USER_AGENT = 'Unknown UserAgent';
|
||||||
const DEFAULT_PLATFORM = 'Unknown platform';
|
const DEFAULT_PLATFORM = 'Unknown platform';
|
||||||
@ -83,8 +83,7 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// eslint-disable-next-line no-console
|
log('CONNECTING');
|
||||||
console.log('[GramJs/client] CONNECTING');
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-restricted-globals
|
// eslint-disable-next-line no-restricted-globals
|
||||||
(self as any).invoke = invokeRequest;
|
(self as any).invoke = invokeRequest;
|
||||||
@ -120,8 +119,7 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
|
|||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log('>>> FINISH INIT API');
|
console.log('>>> FINISH INIT API');
|
||||||
// eslint-disable-next-line no-console
|
log('CONNECTED');
|
||||||
console.log('[GramJs/client] CONNECTED');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onAuthReady();
|
onAuthReady();
|
||||||
@ -131,8 +129,7 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs)
|
|||||||
void fetchCurrentUser();
|
void fetchCurrentUser();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// eslint-disable-next-line no-console
|
log('CONNECTING ERROR', err);
|
||||||
console.log('[GramJs/client] CONNECTING ERROR', err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw err;
|
throw err;
|
||||||
@ -214,8 +211,7 @@ export async function invokeRequest<T extends GramJs.AnyRequest>(
|
|||||||
) {
|
) {
|
||||||
if (!isConnected) {
|
if (!isConnected) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// eslint-disable-next-line no-console
|
log('INVOKE ERROR', request.className, 'Client is not connected');
|
||||||
console.warn(`[GramJs/client] INVOKE ERROR ${request.className}: Client is not connected`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -223,15 +219,13 @@ export async function invokeRequest<T extends GramJs.AnyRequest>(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// eslint-disable-next-line no-console
|
log('INVOKE', request.className);
|
||||||
console.log(`[GramJs/client] INVOKE ${request.className}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await client.invoke(request, dcId);
|
const result = await client.invoke(request, dcId);
|
||||||
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// eslint-disable-next-line no-console
|
log('INVOKE RESPONSE', request.className, result);
|
||||||
console.log(`[GramJs/client] INVOKE RESPONSE ${request.className}`, result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!shouldIgnoreUpdates) {
|
if (!shouldIgnoreUpdates) {
|
||||||
@ -241,8 +235,7 @@ export async function invokeRequest<T extends GramJs.AnyRequest>(
|
|||||||
return shouldReturnTrue ? result && true : result;
|
return shouldReturnTrue ? result && true : result;
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// eslint-disable-next-line no-console
|
log('INVOKE ERROR', request.className);
|
||||||
console.log(`[GramJs/client] INVOKE ERROR ${request.className}`);
|
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,6 +39,7 @@ import {
|
|||||||
addPhotoToLocalDb,
|
addPhotoToLocalDb,
|
||||||
resolveMessageApiChatId,
|
resolveMessageApiChatId,
|
||||||
serializeBytes,
|
serializeBytes,
|
||||||
|
log,
|
||||||
} from './helpers';
|
} from './helpers';
|
||||||
import { buildApiNotifyException, buildPrivacyKey, buildPrivacyRules } from './apiBuilders/misc';
|
import { buildApiNotifyException, buildPrivacyKey, buildPrivacyRules } from './apiBuilders/misc';
|
||||||
import { buildApiPhoto } from './apiBuilders/common';
|
import { buildApiPhoto } from './apiBuilders/common';
|
||||||
@ -984,7 +985,6 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) {
|
|||||||
}
|
}
|
||||||
} else if (DEBUG) {
|
} else if (DEBUG) {
|
||||||
const params = typeof update === 'object' && 'className' in update ? update.className : update;
|
const params = typeof update === 'object' && 'className' in update ? update.className : update;
|
||||||
// eslint-disable-next-line no-console
|
log('UNEXPECTED UPDATE', params);
|
||||||
console.warn('[GramJs/updater] Unexpected update:', params);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import type { OriginMessageEvent, WorkerMessageData } from './types';
|
|||||||
|
|
||||||
import { DEBUG } from '../../../config';
|
import { DEBUG } from '../../../config';
|
||||||
import { initApi, callApi, cancelApiProgress } from '../provider';
|
import { initApi, callApi, cancelApiProgress } from '../provider';
|
||||||
|
import { log } from '../helpers';
|
||||||
|
|
||||||
declare const self: WorkerGlobalScope;
|
declare const self: WorkerGlobalScope;
|
||||||
|
|
||||||
@ -47,8 +48,7 @@ onmessage = async (message: OriginMessageEvent) => {
|
|||||||
const response = await callApi(name, ...args);
|
const response = await callApi(name, ...args);
|
||||||
|
|
||||||
if (DEBUG && typeof response === 'object' && 'CONSTRUCTOR_ID' in response) {
|
if (DEBUG && typeof response === 'object' && 'CONSTRUCTOR_ID' in response) {
|
||||||
// eslint-disable-next-line no-console
|
log('UNEXPECTED RESPONSE', `${name}: ${response.className}`);
|
||||||
console.error(`[GramJs/worker] \`${name}\`: Unexpected response \`${(response as any).className}\``);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { arrayBuffer } = (typeof response === 'object' && 'arrayBuffer' in response && response) || {};
|
const { arrayBuffer } = (typeof response === 'object' && 'arrayBuffer' in response && response) || {};
|
||||||
@ -119,6 +119,10 @@ function onUpdate(update: ApiUpdate) {
|
|||||||
type: 'update',
|
type: 'update',
|
||||||
update,
|
update,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (DEBUG && update['@type'] !== 'updateUserStatus' && update['@type'] !== 'updateServerTimeOffset') {
|
||||||
|
log('UPDATE', update['@type'], update);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendToOrigin(data: WorkerMessageData, arrayBuffer?: ArrayBuffer) {
|
function sendToOrigin(data: WorkerMessageData, arrayBuffer?: ArrayBuffer) {
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import type {
|
|||||||
ApiUpdateSession,
|
ApiUpdateSession,
|
||||||
ApiUpdateCurrentUser, ApiUpdateServerTimeOffset,
|
ApiUpdateCurrentUser, ApiUpdateServerTimeOffset,
|
||||||
} from '../../../api/types';
|
} from '../../../api/types';
|
||||||
import { DEBUG, SESSION_USER_KEY } from '../../../config';
|
import { SESSION_USER_KEY } from '../../../config';
|
||||||
import { subscribe } from '../../../util/notifications';
|
import { subscribe } from '../../../util/notifications';
|
||||||
import { updateUser } from '../../reducers';
|
import { updateUser } from '../../reducers';
|
||||||
import { setLanguage } from '../../../util/langProvider';
|
import { setLanguage } from '../../../util/langProvider';
|
||||||
@ -20,13 +20,6 @@ import { forceWebsync } from '../../../util/websync';
|
|||||||
import { getShippingError, shouldClosePaymentModal } from '../../../util/getReadableErrorText';
|
import { getShippingError, shouldClosePaymentModal } from '../../../util/getReadableErrorText';
|
||||||
|
|
||||||
addActionHandler('apiUpdate', (global, actions, update) => {
|
addActionHandler('apiUpdate', (global, actions, update) => {
|
||||||
if (DEBUG) {
|
|
||||||
if (update['@type'] !== 'updateUserStatus' && update['@type'] !== 'updateServerTimeOffset') {
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.log('[GramJs] UPDATE', update['@type'], { update });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (update['@type']) {
|
switch (update['@type']) {
|
||||||
case 'updateApiReady':
|
case 'updateApiReady':
|
||||||
onUpdateApiReady(global);
|
onUpdateApiReady(global);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user