import chalk from 'chalk'; import { successSymbol, errorSymbol, warningSymbol, infoSymbol, error, success, } from './log'; import { IApiResponse } from '../apis'; import { formatDuration, getFormattedDate } from './datetime'; import { IEntitySearchResult } from './@types/IEntitySearchResult'; import path from 'path'; import fs from 'fs'; import os from 'os'; import { readVariableKey, readVariableValue } from './variablesHelper'; export function logObj(obj: any, columns: number = 4) { if (!obj) { return; } const keys = Object.keys(obj); const rows = Math.ceil(keys.length / columns); for (let i = 0; i < rows; i++) { let rowStr = ''; for (let j = 0; j < columns; j++) { const index = i + j * rows; if (index < keys.length) { const key = keys[index]; const realKey = key.match(/__(error|warning|success)\(([^)]+)\)/); let valueStr = ''; if (realKey) { const [, type, message] = realKey; const value = obj[key]; if ( (type === 'error' || type === 'warning') && typeof value === 'number' && value <= 0 ) { valueStr = `${successSymbol()} - ${message}: ${chalk.green(value)}`; } else if (type === 'error') { valueStr = `${errorSymbol()} - ${message}: ${chalk.red(value)}`; } else if (type === 'warning') { valueStr = `${warningSymbol()} - ${message}: ${chalk.yellow(value)}`; } else if (type === 'success') { valueStr = `${successSymbol()} - ${message}: ${chalk.green(value)}`; } } else { valueStr = `${infoSymbol()} - ${key}: ${chalk.gray(obj[key])}`; } rowStr += ' ' + valueStr.padEnd(30) + ' '; // Adjust padding as needed } } console.log(rowStr); } } function getLogPath() { return path.join(os.homedir(), 'hexasync', 'logs'); } export function beginSessionLog(event: string, title: string) { const logPath = getLogPath(); if (!fs.existsSync(logPath)) { fs.mkdirSync(logPath, { recursive: true }); } const logFilePath = path.join(logPath, `${event}.log`); fs.appendFileSync(logFilePath, `\n----------${title}----------\n`); } export function endSessionLog(event: string) { const logPath = getLogPath(); const logFilePath = path.join(logPath, `${event}.log`); fs.appendFileSync( logFilePath, `\n--------------------------------------------\n\n`, ); } export function beginLog(event: string) { const logPath = getLogPath(); let logFilePath = path.join(logPath, `${event}.log`); logFilePath = `file://${logFilePath}`; console.log( `${chalk.green('✓')} Begin ${chalk.green(event)}. Check the logs at ${chalk.gray(logFilePath)} for more details.\n`, ); } export function endLog(event: string) { const logPath = getLogPath(); let logFilePath = path.join(logPath, `${event}.log`); logFilePath = `file://${logFilePath}`; console.log( `\n${chalk.green('✓')} ${chalk.green(event)} completed. Check the logs at ${chalk.gray(logFilePath)} for more details.`, ); } export function appendAPIEventLog( event: string, apiResponse: IApiResponse, entity: IEntitySearchResult, start?: number, end?: number, ) { var messages = [ `Event: ${apiResponse.event}`, `Entity Type: ${entity.entityType}`, `Status Code: ${apiResponse.statusCode}`, `Status: ${apiResponse.success ? 'SUCCESS' : 'FAILURE'}`, `File Path: ${entity.filePath}`, `Message: ${apiResponse.message}`, ]; if (apiResponse.data) { messages.push('Data:'); if (apiResponse.event === 'pull-task') { var summary = apiResponse.data.summary[ readVariableValue(entity.content?.id!) || '' ] || {}; const lastActivity = summary.lastActivity ? new Date(summary.lastActivity).toLocaleString() : 'N/A'; messages.push(`- Pulled at: ${lastActivity}`); const dataSummary = summary.dataSummary || []; const dataRows = Math.ceil(dataSummary.length / 4); for (let i = 0; i < dataRows; i++) { let rowStr = ''; for (let j = 0; j < 4; j++) { const index = i + j * dataRows; if (index < dataSummary.length) { const { key, count, type } = dataSummary[index]; const displayKey = type === 1 && count > 0 ? `${key} (*)` : key; rowStr += `- ${displayKey}: ${count}`.padEnd(30) + ' '; } } messages.push(rowStr); } } else { const dataKeys = Object.keys(apiResponse.data); const dataRows = Math.ceil(dataKeys.length / 4); for (let i = 0; i < dataRows; i++) { let rowStr = ''; for (let j = 0; j < 4; j++) { const index = i + j * dataRows; if (index < dataKeys.length) { const key = dataKeys[index]; const realKey = key.match(/__(error|warning|success)\(([^)]+)\)/); let [, , m] = realKey || []; m = m || key; rowStr += `- ${m}: ${apiResponse.data[key]}`.padEnd(30) + ' '; } } messages.push(rowStr); } } } if (apiResponse.stackTrace) { messages.push(`Stack Trace: ${apiResponse.stackTrace}`); } if (start && end && apiResponse.success) { const duration = end - start; if (duration > 60000) { messages.push( `The event took more than 1 minute to proceed. Actual time: ${formatDuration(duration)}`, ); } else { messages.push(`The event succeeded in ${formatDuration(duration)}`); } } const logPath = getLogPath(); const logFilePath = path.join(logPath, `${event}.log`); var message = messages.join('\n'); message = `\n${message}\n`; fs.appendFileSync(logFilePath, message); }