unity-builder/src/model/cloud-runner/services/cloud-runner-logger.ts

79 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-08-21 23:26:03 +00:00
import * as core from '@actions/core';
2022-01-02 00:33:18 +00:00
import { exec } from 'child_process';
import { Input } from '../..';
2022-01-02 18:07:27 +00:00
import fs from 'fs';
2021-08-21 23:26:03 +00:00
2021-09-21 18:27:04 +00:00
class CloudRunnerLogger {
2021-08-21 23:26:03 +00:00
private static timestamp: number;
private static globalTimestamp: number;
public static setup() {
this.timestamp = this.createTimestamp();
this.globalTimestamp = this.timestamp;
}
2021-09-21 18:27:04 +00:00
public static log(message: string) {
core.info(message);
2022-01-02 19:04:14 +00:00
fs.appendFile('cloud-runner-logs', message, () => {});
2021-09-21 18:27:04 +00:00
}
2021-12-31 20:12:47 +00:00
public static logWarning(message: string) {
core.warning(message);
2022-01-02 19:04:14 +00:00
fs.appendFile('cloud-runner-logs', message, () => {});
2021-12-29 14:35:13 +00:00
}
public static logLine(message: string) {
core.info(`${message}\n`);
2022-01-02 19:04:14 +00:00
fs.appendFile('cloud-runner-logs', message, () => {});
}
2021-09-21 18:27:04 +00:00
public static error(message: string) {
core.error(message);
2022-01-02 19:04:14 +00:00
fs.appendFile('cloud-runner-logs', message, () => {});
2021-09-21 18:27:04 +00:00
}
2021-08-21 23:26:03 +00:00
public static logWithTime(message: string) {
const newTimestamp = this.createTimestamp();
core.info(
`${message} (Since previous: ${this.calculateTimeDiff(
newTimestamp,
this.timestamp,
)}, Total time: ${this.calculateTimeDiff(newTimestamp, this.globalTimestamp)})`,
);
this.timestamp = newTimestamp;
}
private static calculateTimeDiff(x: number, y: number) {
return Math.floor((x - y) / 1000);
}
private static createTimestamp() {
return Date.now();
}
2022-01-01 18:36:17 +00:00
2022-01-02 00:33:18 +00:00
public static InitHook() {
if (process.env.INIT_HOOK === undefined || !Input.cloudRunnerTests) {
2022-01-01 18:36:17 +00:00
return;
}
2022-01-02 04:03:14 +00:00
CloudRunnerLogger.log(`STARTING INIT HOOK ${process.env.INIT_HOOK}`);
2022-01-02 17:07:56 +00:00
const child = exec(process.env.INIT_HOOK, (error: any, stdout: string, stderr: any) => {
2022-01-02 00:33:18 +00:00
if (error) {
2022-01-02 18:50:08 +00:00
CloudRunnerLogger.error(`[GCP-LOGGER][ERROR]${error}`);
2022-01-02 00:33:18 +00:00
return;
}
if (stderr) {
2022-01-02 04:03:14 +00:00
CloudRunnerLogger.logWarning(`[GCP-LOGGER][DIAGNOSTIC]${stderr}`);
2022-01-02 00:33:18 +00:00
return;
}
2022-01-02 04:03:14 +00:00
CloudRunnerLogger.log(`[GCP-LOGGER]${stdout}`);
2022-01-02 00:33:18 +00:00
});
2022-01-02 17:07:56 +00:00
child.on('close', function (code) {
CloudRunnerLogger.log(`[GCP-LOGGER][Exit code ${code}]`);
if (code !== 0) {
throw new Error(`${code}`);
}
});
2022-01-01 18:36:17 +00:00
}
2021-08-21 23:26:03 +00:00
}
2021-09-21 18:27:04 +00:00
export default CloudRunnerLogger;