Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/log.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45declare var window;67import { RUNNING_IN_NODE, seconds2hms } from "./misc";89let smc_logger_timestamp, smc_logger_timestamp_last, smc_start_time;10smc_logger_timestamp =11smc_logger_timestamp_last =12smc_start_time =13new Date().getTime() / 1000.0;1415export const get_start_time_ts = () => new Date(smc_start_time * 1000);1617export const get_uptime = () =>18seconds2hms(new Date().getTime() / 1000.0 - smc_start_time);1920export function log(..._args): void {21smc_logger_timestamp = new Date().getTime() / 1000.0;22const t = seconds2hms(smc_logger_timestamp - smc_start_time);23const dt = seconds2hms(smc_logger_timestamp - smc_logger_timestamp_last);24// support for string interpolation for the actual console.log25const [msg, ...args] = Array.from(Array.prototype.slice.call(arguments));26let prompt = `[${t} Δ ${dt} - ${new Date().toISOString()}]`;27if (typeof msg == "string") {28prompt = `${prompt} ${msg}`;29(console as any).log_original(prompt, ...Array.from(args));30} else {31(console as any).log_original(prompt, msg, ...Array.from(args));32}33smc_logger_timestamp_last = smc_logger_timestamp;34}3536export function wrap_log(): void {37if (!RUNNING_IN_NODE && typeof window != "undefined") {38(window.console as any).log_original = window.console.log;39window.console.log = log;40}41}424344