Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

2.1 KB
Raw Blame
import fs from 'node:fs';

/**
 * A prefix to use for all messages we add to the console log.
 */
export const LOG_PREFIX = '[MeetTest] ';

/**
 * Initialize logger for a driver.
 *
 * @param {WebdriverIO.Browser} driver - The driver.
 * @param {string} fileName - The name of the file.
 * @param {string} folder - The folder to save the file.
 * @returns {void}
 */
export function initLogger(driver: WebdriverIO.Browser, fileName: string, folder: string) {
    // @ts-ignore
    driver.logFile = `${folder}/${fileName}.log`;
    driver.sessionSubscribe({ events: [ 'log.entryAdded' ] });

    driver.on('log.entryAdded', (entry: any) => {
        try {
            // @ts-ignore
            fs.appendFileSync(driver.logFile, `${entry.text}\n`);
        } catch (err) {
            console.error(err);
        }
    });
}

/**
 * Returns the content of the log file.
 *
 * @param {WebdriverIO.Browser} driver - The driver which log file is requested.
 * @returns {string} The content of the log file.
 */
export function getLogs(driver: WebdriverIO.Browser) {
    // @ts-ignore
    if (!driver.logFile) {
        return;
    }

    // @ts-ignore
    return fs.readFileSync(driver.logFile, 'utf8');
}

/**
 * Appends value to the log file.
 * @param {WebdriverIO.Browser} driver - The driver which log file is requested.
 * @param {string} value - The content to add to the file.
 */
export function saveLogs(driver: WebdriverIO.Browser, value: string) {
    // @ts-ignore
    if (!driver.logFile) {
        return;
    }

    try {
        // @ts-ignore
        fs.appendFileSync(driver.logFile, value);
    } catch (err) {
        console.error(err);
    }
}

/**
 * Logs a message in the logfile.
 *
 * @param {WebdriverIO.Browser} driver - The participant in which log file to write.
 * @param {string} message - The message to add.
 * @returns {void}
 */
export async function logInfo(driver: WebdriverIO.Browser, message: string) {
    // @ts-ignore
    if (!driver.logFile) {
        return;
    }

    return driver.execute((prefix, msg) =>
        console.log(`${new Date().toISOString()} ${prefix} ${msg}\n`),
        LOG_PREFIX, message);
}