CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/log.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
declare var window;
7
8
import { RUNNING_IN_NODE, seconds2hms } from "./misc";
9
10
let smc_logger_timestamp, smc_logger_timestamp_last, smc_start_time;
11
smc_logger_timestamp =
12
smc_logger_timestamp_last =
13
smc_start_time =
14
new Date().getTime() / 1000.0;
15
16
export const get_start_time_ts = () => new Date(smc_start_time * 1000);
17
18
export const get_uptime = () =>
19
seconds2hms(new Date().getTime() / 1000.0 - smc_start_time);
20
21
export function log(..._args): void {
22
smc_logger_timestamp = new Date().getTime() / 1000.0;
23
const t = seconds2hms(smc_logger_timestamp - smc_start_time);
24
const dt = seconds2hms(smc_logger_timestamp - smc_logger_timestamp_last);
25
// support for string interpolation for the actual console.log
26
const [msg, ...args] = Array.from(Array.prototype.slice.call(arguments));
27
let prompt = `[${t} Δ ${dt} - ${new Date().toISOString()}]`;
28
if (typeof msg == "string") {
29
prompt = `${prompt} ${msg}`;
30
(console as any).log_original(prompt, ...Array.from(args));
31
} else {
32
(console as any).log_original(prompt, msg, ...Array.from(args));
33
}
34
smc_logger_timestamp_last = smc_logger_timestamp;
35
}
36
37
export function wrap_log(): void {
38
if (!RUNNING_IN_NODE && typeof window != "undefined") {
39
(window.console as any).log_original = window.console.log;
40
window.console.log = log;
41
}
42
}
43
44