Skip to content

Commit

Permalink
grep better
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jul 4, 2024
1 parent 8f935ff commit 4476870
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
27 changes: 27 additions & 0 deletions lib/device-log/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { LogEntry } from '../commands/types';
import { fs } from 'appium/support';
import { createInterface } from 'node:readline';
import _ from 'lodash';

export const DEFAULT_LOG_LEVEL = 'ALL';
export const MAX_JSON_LOG_LENGTH = 200;
Expand All @@ -11,3 +14,27 @@ export function toLogEntry(message: string, timestamp: number, level: string = D
message,
};
}

export interface GrepOptions {
caseInsensitive?: boolean;
}

export async function grepFile(
fullPath: string,
str: string,
opts: GrepOptions = {}
): Promise<boolean> {
const input = fs.createReadStream(fullPath);
const rl = createInterface({input});
return await new Promise((resolve, reject) => {
input.once('error', reject);
rl.on('line', (line) => {
if (opts.caseInsensitive && _.toLower(line).includes(_.toLower(str))
|| !opts.caseInsensitive && line.includes(str)) {
resolve(true);
input.close();
}
});
input.once('end', () => resolve(false));
});
}
9 changes: 5 additions & 4 deletions lib/device-log/ios-crash-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import _ from 'lodash';
import {Pyidevice} from '../real-device-clients/py-ios-device-client';
import IOSLog from './ios-log';
import { toLogEntry } from './helpers';
import { toLogEntry, grepFile } from './helpers';
import type { AppiumLogger } from '@appium/types';
import type { BaseDeviceClient } from '../real-device-clients/base-device-client';
import type { Simulator } from 'appium-ios-simulator';
Expand Down Expand Up @@ -135,12 +135,13 @@ export class IOSCrashLog extends IOSLog<TSerializedEntry, TSerializedEntry> {
cwd: this._logDir,
absolute: true,
});
const simUdid = (this._sim as Simulator).udid;
// For Simulator only include files, that contain current UDID
return await B.filter(foundFiles, async (x) => {
return await B.filter(foundFiles, async (filePath) => {
try {
const content = await fs.readFile(x, 'utf8');
return content.toUpperCase().includes((this._sim as Simulator).udid.toUpperCase());
return await grepFile(filePath, simUdid, {caseInsensitive: true});
} catch (err) {
this.log.warn(err);
return false;
}
});
Expand Down

0 comments on commit 4476870

Please sign in to comment.