Skip to content

Commit

Permalink
chore: improve logging with timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
sandor-trombitas committed Aug 16, 2024
1 parent 4c956e5 commit 10eb375
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions ts-binary-wrapper/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export class WrapperConfiguration {
}
}

const logErrorWithTimeStamps = (...args) => {
console.error(`${new Date().toISOString()}:`, ...args);
}

export function determineBinaryName(platform: string, arch: string): string {
let osname = platform;
let archname = arch;
Expand Down Expand Up @@ -180,7 +184,7 @@ export function runWrapper(executable: string, cliArguments: string[]): number {
const debug = debugEnabled(cliArguments);

if (debug) {
console.error('Executing: ' + executable + ' ' + cliArguments.join(' '));
logErrorWithTimeStamps('Executing: ' + executable + ' ' + cliArguments.join(' '));
}

const res = spawnSync(executable, cliArguments, {
Expand All @@ -195,14 +199,14 @@ export function runWrapper(executable: string, cliArguments: string[]): number {

if (res.status !== null) {
if (debug) {
console.error(res);
logErrorWithTimeStamps(res);
}

return res.status;
} else {
console.error(res);
logErrorWithTimeStamps(res);
if (!formatErrorMessage((res.error as SpawnError).code)) {
console.error('Failed to spawn child process. (' + executable + ')');
logErrorWithTimeStamps('Failed to spawn child process. (' + executable + ')');
}

return 2;
Expand Down Expand Up @@ -243,7 +247,7 @@ export function formatErrorMessage(message: string): boolean {
return false;
}

console.error(getWarningMessage(warning));
logErrorWithTimeStamps(getWarningMessage(warning));
return true;
}

Expand All @@ -253,6 +257,7 @@ export function downloadExecutable(
filenameShasum: string,
): Promise<Error | undefined> {
return new Promise<Error | undefined>(function(resolve) {
logErrorWithTimeStamps('Starting download');
const options = new URL(`${downloadUrl}?utm_source=${integrationName}`);
const temp = path.join(__dirname, Date.now().toString());
const fileStream = fs.createWriteStream(temp);
Expand Down Expand Up @@ -282,19 +287,19 @@ export function downloadExecutable(
if (filenameShasum && actualShasum != filenameShasum) {
cleanupAfterError(Error('Shasum comparison failed!\n' + debugMessage));
} else {
console.error(debugMessage);
logErrorWithTimeStamps(debugMessage);

// finally rename the file and change permissions
fs.renameSync(temp, filename);
fs.chmodSync(filename, 0o755);
console.error('Downloaded successfull! ');
logErrorWithTimeStamps('Downloaded successfull! ');
}

resolve(undefined);
});

console.error(
"Downloading from '" + downloadUrl + "' to '" + filename + "'",
logErrorWithTimeStamps(
"Downloading from '" + options.toString() + "' to '" + filename + "'",
);

const req = https.get(options, (res) => {
Expand Down Expand Up @@ -346,22 +351,22 @@ export async function downloadWithBackup(
filenameShasum,
);
if (error) {
console.error(error);
console.error(
'Download failed! Trying to download from backup location...',
logErrorWithTimeStamps(error);
logErrorWithTimeStamps(
`Failed to download from ${downloadUrl}! Trying to download from ${backupUrl} location...`,
);
const backupError = await downloadExecutable(
backupUrl,
filename,
filenameShasum,
);

console.error(backupError);
logErrorWithTimeStamps(backupError);
return backupError;
}
} catch (err) {
// Handle any unexpected errors
console.error('An unexpected error occurred:', err);
logErrorWithTimeStamps('An unexpected error occurred:', err);
throw err; // Rethrow if you want to propagate the error upwards
}
}
Expand Down Expand Up @@ -389,7 +394,7 @@ export async function logError(

// finally log the error to the console as well
if (printToConsole) {
console.error('\n' + err);
logErrorWithTimeStamps('\n' + err);
formatErrorMessage(err.message);
}
}
Expand Down

0 comments on commit 10eb375

Please sign in to comment.