summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorHaishan <[email protected]>2021-05-31 21:50:46 +0800
committerHaishan <[email protected]>2021-05-31 22:03:53 +0800
commitfcab7cad4f4cc65a7f30427be35c12a909633e2e (patch)
treef677ca7eb8b05837426b6477e943e50608123215 /src/api
parentd4015f64237b2b429a04af0ebe82fdf883b45b01 (diff)
Minor log style tweak
Diffstat (limited to 'src/api')
-rw-r--r--src/api/logs.ts26
1 files changed, 16 insertions, 10 deletions
diff --git a/src/api/logs.ts b/src/api/logs.ts
index 5965588..a65c55e 100644
--- a/src/api/logs.ts
+++ b/src/api/logs.ts
@@ -1,14 +1,9 @@
-import { LogsAPIConfig } from 'src/types';
+import { pad0 } from 'src/misc/utils';
+import { Log, LogsAPIConfig } from 'src/types';
import { buildLogsWebSocketURL, getURLAndInit } from '../misc/request-helper';
-type LogEntry = {
- time?: string;
- id?: string;
- even?: boolean;
- // and some other props
-};
-type AppendLogFn = (x: LogEntry) => void;
+type AppendLogFn = (x: Log) => void;
const endpoint = '/logs';
const textDecoder = new TextDecoder('utf-8');
@@ -22,7 +17,7 @@ let fetched = false;
let decoded = '';
function appendData(s: string, callback: AppendLogFn) {
- let o: LogEntry;
+ let o: Partial<Log>;
try {
o = JSON.parse(s);
} catch (err) {
@@ -31,7 +26,7 @@ function appendData(s: string, callback: AppendLogFn) {
}
const now = new Date();
- const time = now.toLocaleString('zh-Hans');
+ const time = formatDate(now);
// mutate input param in place intentionally
o.time = time;
o.id = +now - 0 + getRandomStr();
@@ -39,6 +34,17 @@ function appendData(s: string, callback: AppendLogFn) {
callback(o);
}
+function formatDate(d: Date) {
+ // 19-03-09 12:45
+ const YY = d.getFullYear() % 100;
+ const MM = pad0(d.getMonth() + 1, 2);
+ const dd = pad0(d.getDate(), 2);
+ const HH = pad0(d.getHours(), 2);
+ const mm = pad0(d.getMinutes(), 2);
+ const ss = pad0(d.getSeconds(), 2);
+ return `${YY}-${MM}-${dd} ${HH}:${mm}:${ss}`;
+}
+
function pump(reader: ReadableStreamDefaultReader, appendLog: AppendLogFn) {
return reader.read().then(({ done, value }) => {
const str = textDecoder.decode(value, { stream: !done });