Skip to content

Commit

Permalink
chore: Rewrite logging-related classes to typescript (#2420)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jul 1, 2024
1 parent 4bd3b69 commit 9789575
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 280 deletions.
5 changes: 4 additions & 1 deletion lib/commands/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,10 @@ const commands = {
// attempt to start performance logging, if requested
if (this.opts.enablePerformanceLogging && this.remote) {
this.log.debug(`Starting performance log on '${this.curContext}'`);
this.logs.performance = new IOSPerformanceLog(this.remote);
this.logs.performance = new IOSPerformanceLog({
remoteDebugger: this.remote,
log: this.log,
});
await this.logs.performance.startCapture();
}

Expand Down
5 changes: 3 additions & 2 deletions lib/commands/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ export default {
this.logs.syslog = new IOSDeviceLog({
udid: this.opts.udid,
showLogs: this.opts.showIOSLog,
log: this.log,
});
} else {
this.logs.syslog = new IOSSimulatorLog({
sim: this.device,
sim: /** @type {import('appium-ios-simulator').Simulator} */ (this.device),
showLogs: this.opts.showIOSLog,
xcodeVersion: this.xcodeVersion,
iosSimulatorLogsPredicate: this.opts.iosSimulatorLogsPredicate,
log: this.log,
});
}
this.logs.safariConsole = new SafariConsoleLog(!!this.opts.showSafariConsoleLog);
Expand Down
56 changes: 0 additions & 56 deletions lib/device-log/ios-device-log.js

This file was deleted.

52 changes: 52 additions & 0 deletions lib/device-log/ios-device-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {services} from 'appium-ios-device';
import { LineConsumingLog } from './line-consuming-log';
import type { AppiumLogger } from '@appium/types';

export interface IOSDeviceLogOpts {
udid: string;
showLogs?: boolean;
log?: AppiumLogger;
}

export class IOSDeviceLog extends LineConsumingLog {
private udid: string;
private showLogs: boolean;
private service: any | null;

constructor(opts: IOSDeviceLogOpts) {
super({log: opts.log});
this.udid = opts.udid;
this.showLogs = !!opts.showLogs;
this.service = null;
}

override async startCapture(): Promise<void> {
if (this.service) {
return;
}
this.service = await services.startSyslogService(this.udid);
this.service.start(this.onLog.bind(this));
}

override get isCapturing(): boolean {
return !!this.service;
}

// eslint-disable-next-line require-await
override async stopCapture(): Promise<void> {
if (!this.service) {
return;
}
this.service.close();
this.service = null;
}

private onLog(logLine: string): void {
this.broadcast(logLine);
if (this.showLogs) {
this.log.info(`[IOS_SYSLOG_ROW] ${logLine}`);
}
}
}

export default IOSDeviceLog;
116 changes: 0 additions & 116 deletions lib/device-log/ios-log.js

This file was deleted.

87 changes: 87 additions & 0 deletions lib/device-log/ios-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {EventEmitter} from 'events';
import { LRUCache } from 'lru-cache';
import type { LogEntry } from '../commands/types';
import type { AppiumLogger } from '@appium/types';
import {logger} from 'appium/support';

// We keep only the most recent log entries to avoid out of memory error
const MAX_LOG_ENTRIES_COUNT = 10000;

export interface IOSLogOptions {
maxBufferSize?: number;
log?: AppiumLogger;
}

export abstract class IOSLog<
TRawEntry,
TSerializedEntry extends object
> extends EventEmitter {
private maxBufferSize: number;
private logs: LRUCache<number, TSerializedEntry>;
private logIndexSinceLastRequest: number | null;
private _log: AppiumLogger;

constructor(opts: IOSLogOptions = {}) {
super();
this.maxBufferSize = opts.maxBufferSize ?? MAX_LOG_ENTRIES_COUNT;
this.logs = new LRUCache({
max: this.maxBufferSize,
});
this.logIndexSinceLastRequest = null;
this._log = opts.log ?? logger.getLogger(this.constructor.name);
}

abstract startCapture(): Promise<void>;
abstract stopCapture(): Promise<void>;
abstract get isCapturing(): boolean;

get log(): AppiumLogger {
return this._log;
}

broadcast(entry: TRawEntry): void {
let recentIndex = -1;
for (const key of this.logs.rkeys()) {
recentIndex = key;
break;
}
const serializedEntry = this._serializeEntry(entry);
this.logs.set(++recentIndex, serializedEntry);
if (this.listenerCount('output')) {
this.emit('output', this._deserializeEntry(serializedEntry));
}
}

getLogs(): LogEntry[] {
const result: LogEntry[] = [];
let recentLogIndex: number | null = null;
for (const [index, value] of this.logs.entries()) {
if (this.logIndexSinceLastRequest && index > this.logIndexSinceLastRequest
|| !this.logIndexSinceLastRequest) {
recentLogIndex = index;
result.push(this._deserializeEntry(value));
}
}
if (recentLogIndex !== null) {
this.logIndexSinceLastRequest = recentLogIndex;
}
return result;
}

getAllLogs(): LogEntry[] {
const result: LogEntry[] = [];
for (const value of this.logs.values()) {
result.push(this._deserializeEntry(value));
}
return result;
}

protected abstract _serializeEntry(value: TRawEntry): TSerializedEntry;
protected abstract _deserializeEntry(value: TSerializedEntry): LogEntry;

protected _clearEntries() {
this.logs.clear();
}
}

export default IOSLog;
Loading

0 comments on commit 9789575

Please sign in to comment.