Support arbitrary parameters like console.log

This commit is contained in:
Manav Rathi 2024-03-27 20:21:27 +05:30
parent 3699118f0c
commit 5ac4799ce1
No known key found for this signature in database
2 changed files with 21 additions and 10 deletions

View file

@ -161,12 +161,13 @@ export async function handleDockIconHideOnAutoLaunch() {
export function logStartupBanner() {
const version = isDev ? "dev" : app.getVersion();
log.info(`hello from ente-photos-desktop ${version}`);
log.info(`Hello from ente-photos-desktop ${version}`);
const systemVersion = process.getSystemVersion();
const osName = process.platform;
const platform = process.platform;
const osRelease = os.release();
log.info(`system info ${{ osName, osRelease, systemVersion }}`);
const systemVersion = process.getSystemVersion();
log.info("Running on", { platform, osRelease, systemVersion });
log.debug(() => ({ platform, osRelease, systemVersion }));
}
async function checkIfInstalledViaBrew() {

View file

@ -1,4 +1,5 @@
import log from "electron-log";
import util from "node:util";
import { isDev } from "./util";
/**
@ -72,13 +73,16 @@ const logError_ = (message: string) => {
if (isDev) console.error(`[error] ${message}`);
};
const logInfo = (message: string) => {
const logInfo = (...params: any[]) => {
const message = params
.map((p) => (typeof p == "string" ? p : util.inspect(p)))
.join(" ");
log.info(`[main] ${message}`);
if (isDev) console.log(message);
};
const logDebug = (message: () => string) => {
if (isDev) console.log(`[debug] ${message()}`);
const logDebug = (param: () => any) => {
if (isDev) console.log(`[debug] ${util.inspect(param())}`);
};
/**
@ -103,6 +107,9 @@ export default {
/**
* Log a message.
*
* This is meant as a replacement of {@link console.log}, and takes an
* arbitrary number of arbitrary parameters that it then serializes.
*
* The log is written to disk. In development builds, the log is also
* printed to the (Node.js process') console.
*/
@ -110,12 +117,15 @@ export default {
/**
* Log a debug message.
*
* The log is not written to disk. And it is printed to the (Node.js
* process') console only on development builds.
*
* To avoid running unnecessary code in release builds, this takes a
* function to call to get the log message instead of directly taking the
* message. The provided function will only be called in development builds.
*
* The function can return an arbitrary value which is serialied before
* being logged.
*
* This log is not written to disk. It is printed to the (Node.js process')
* console only on development builds.
*/
debug: logDebug,
};