ScribeLogger moved to common
Summary: There's nothing specific nor private about the ScribeLogger, so it is moved to flipper-common. Reviewed By: LukeDefeo Differential Revision: D48556074 fbshipit-source-id: aa9446036fd07fe6e6debc5a978a42308fc93fe0
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b69358f6a2
commit
e7b6882ef2
@@ -21,6 +21,7 @@ export {
|
|||||||
NoopLogger,
|
NoopLogger,
|
||||||
} from './utils/Logger';
|
} from './utils/Logger';
|
||||||
export * from './utils/LoggerTailer';
|
export * from './utils/LoggerTailer';
|
||||||
|
export * from './utils/ScribeLogger';
|
||||||
export * from './server-types';
|
export * from './server-types';
|
||||||
export * from './companion-types';
|
export * from './companion-types';
|
||||||
export * from './ServerAddOn';
|
export * from './ServerAddOn';
|
||||||
|
|||||||
94
desktop/flipper-common/src/utils/ScribeLogger.tsx
Normal file
94
desktop/flipper-common/src/utils/ScribeLogger.tsx
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*
|
||||||
|
* @format
|
||||||
|
*/
|
||||||
|
|
||||||
|
const FLUSH_TIMEOUT = 500;
|
||||||
|
const MAX_MESSAGES = 1000;
|
||||||
|
|
||||||
|
export type ScribeMessage = {
|
||||||
|
category: string;
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send messages to arbitrary Scribe categories. This is useful to log data to
|
||||||
|
* LogView, Scuba, Hive, etc.
|
||||||
|
*/
|
||||||
|
export class ScribeLogger {
|
||||||
|
constructor(
|
||||||
|
sender: (messages: Array<ScribeMessage>) => Promise<void>,
|
||||||
|
disabled = false,
|
||||||
|
flushTimeout = FLUSH_TIMEOUT,
|
||||||
|
) {
|
||||||
|
this.isFlushPending = false;
|
||||||
|
this.queue = [];
|
||||||
|
this.sendMessages = sender;
|
||||||
|
this.flushTimeout = flushTimeout;
|
||||||
|
this.disabled = disabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
isFlushPending: boolean;
|
||||||
|
queue: Array<ScribeMessage>;
|
||||||
|
sendMessages: (messages: Array<ScribeMessage>) => Promise<void>;
|
||||||
|
flushTimeout: number;
|
||||||
|
disabled: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send all the queued messages and reset.
|
||||||
|
*/
|
||||||
|
flushQueue = async () => {
|
||||||
|
const messages = this.queue.splice(0); // copy & clear
|
||||||
|
try {
|
||||||
|
// send all the message currently in the queue
|
||||||
|
await this.sendMessages(messages);
|
||||||
|
} catch (e) {
|
||||||
|
console.debug(`[ScribeLogger] Failed to send messages: ${e}`);
|
||||||
|
// restore messages if emit failed
|
||||||
|
this.queue = messages.concat(this.queue);
|
||||||
|
this.enforceQueueLimit();
|
||||||
|
} finally {
|
||||||
|
// reset the queue
|
||||||
|
this.isFlushPending = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a message to be queued. Start a queue flush timer if one isn't already pending.
|
||||||
|
*/
|
||||||
|
queueSend(message: ScribeMessage) {
|
||||||
|
this.queue.push(message);
|
||||||
|
this.enforceQueueLimit();
|
||||||
|
|
||||||
|
// start a flush timer if none exists
|
||||||
|
if (this.isFlushPending === false) {
|
||||||
|
this.isFlushPending = true;
|
||||||
|
if (this.flushTimeout >= 0) {
|
||||||
|
setTimeout(this.flushQueue, FLUSH_TIMEOUT);
|
||||||
|
} else {
|
||||||
|
this.flushQueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enforceQueueLimit() {
|
||||||
|
if (this.queue.length > MAX_MESSAGES) {
|
||||||
|
console.debug('[ScribeLogger] Queue full, dropping oldest messages');
|
||||||
|
this.queue = this.queue.slice(Math.floor(MAX_MESSAGES / 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Messages are not guaranteed to be sent right away.
|
||||||
|
*/
|
||||||
|
send(message: ScribeMessage) {
|
||||||
|
if (this.disabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.queueSend(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user