-
-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Rewrite logging-related classes to typescript (#2420)
- Loading branch information
1 parent
4bd3b69
commit 9789575
Showing
10 changed files
with
259 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.