-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathlog.js
220 lines (202 loc) · 6.82 KB
/
log.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import {DEFAULT_WS_PATHNAME_PREFIX, BaseDriver} from 'appium/driver';
import _ from 'lodash';
import os from 'node:os';
import WebSocket from 'ws';
import {
GET_SERVER_LOGS_FEATURE,
toLogRecord,
nativeLogEntryToSeleniumEntry,
} from '../utils';
import { NATIVE_WIN } from './context/helpers';
import { BIDI_EVENT_NAME } from './bidi/constants';
import { makeLogEntryAddedEvent } from './bidi/models';
export const supportedLogTypes = {
logcat: {
description: 'Logs for Android applications on real device and emulators via ADB',
/**
*
* @param {import('../driver').AndroidDriver} self
* @returns
*/
getter: (self) => /** @type {ADB} */ (self.adb).getLogcatLogs(),
},
bugreport: {
description: `'adb bugreport' output for advanced issues diagnostic`,
/**
*
* @param {import('../driver').AndroidDriver} self
* @returns
*/
getter: async (self) => {
const output = await /** @type {ADB} */ (self.adb).bugreport();
const timestamp = Date.now();
return output.split(os.EOL).map((x) => toLogRecord(timestamp, x));
},
},
server: {
description: 'Appium server logs',
/**
*
* @param {import('../driver').AndroidDriver} self
* @returns
*/
getter: (self) => {
self.assertFeatureEnabled(GET_SERVER_LOGS_FEATURE);
return self.log.unwrap().record.map(nativeLogEntryToSeleniumEntry);
},
},
};
/**
* Starts Android logcat broadcast websocket on the same host and port
* where Appium server is running at `/ws/session/:sessionId:/appium/logcat` endpoint. The method
* will return immediately if the web socket is already listening.
*
* Each connected websocket listener will receive logcat log lines
* as soon as they are visible to Appium.
*
* @this {import('../driver').AndroidDriver}
* @returns {Promise<void>}
*/
export async function mobileStartLogsBroadcast() {
const server = /** @type {import('@appium/types').AppiumServer} */ (this.server);
const pathname = WEBSOCKET_ENDPOINT(/** @type {string} */ (this.sessionId));
if (!_.isEmpty(await server.getWebSocketHandlers(pathname))) {
this.log.debug(`The logcat broadcasting web socket server is already listening at ${pathname}`);
return;
}
this.log.info(
`Starting logcat broadcasting on web socket server ` +
`${JSON.stringify(server.address())} to ${pathname}`,
);
// https://github.com/websockets/ws/blob/master/doc/ws.md
const wss = new WebSocket.Server({
noServer: true,
});
wss.on('connection', (ws, req) => {
if (req) {
const remoteIp = _.isEmpty(req.headers['x-forwarded-for'])
? req.connection?.remoteAddress
: req.headers['x-forwarded-for'];
this.log.debug(`Established a new logcat listener web socket connection from ${remoteIp}`);
} else {
this.log.debug('Established a new logcat listener web socket connection');
}
if (_.isEmpty(this._logcatWebsocketListener)) {
this._logcatWebsocketListener = (logRecord) => {
if (ws?.readyState === WebSocket.OPEN) {
ws.send(logRecord.message);
}
};
}
this.adb.setLogcatListener(this._logcatWebsocketListener);
ws.on('close', (code, reason) => {
if (!_.isEmpty(this._logcatWebsocketListener)) {
try {
this.adb.removeLogcatListener(this._logcatWebsocketListener);
} catch {}
this._logcatWebsocketListener = undefined;
}
let closeMsg = 'Logcat listener web socket is closed.';
if (!_.isEmpty(code)) {
closeMsg += ` Code: ${code}.`;
}
if (!_.isEmpty(reason)) {
closeMsg += ` Reason: ${reason.toString()}.`;
}
this.log.debug(closeMsg);
});
});
await server.addWebSocketHandler(pathname, /** @type {import('@appium/types').WSServer} */ (wss));
}
/**
* Stops the previously started logcat broadcasting wesocket server.
* This method will return immediately if no server is running.
*
* @this {import('../driver').AndroidDriver}
* @returns {Promise<void>}
*/
export async function mobileStopLogsBroadcast() {
const pathname = WEBSOCKET_ENDPOINT(/** @type {string} */ (this.sessionId));
const server = /** @type {import('@appium/types').AppiumServer} */ (this.server);
if (_.isEmpty(await server.getWebSocketHandlers(pathname))) {
return;
}
this.log.debug(
`Stopping logcat broadcasting on web socket server ` +
`${JSON.stringify(server.address())} to ${pathname}`,
);
await server.removeWebSocketHandler(pathname);
}
/**
* @this {import('../driver').AndroidDriver}
* @returns {Promise<string[]>}
*/
export async function getLogTypes() {
// XXX why doesn't `super` work here?
const nativeLogTypes = await BaseDriver.prototype.getLogTypes.call(this);
if (this.isWebContext()) {
const webLogTypes = /** @type {string[]} */ (
await /** @type {import('appium-chromedriver').Chromedriver} */ (
this.chromedriver
).jwproxy.command('/log/types', 'GET')
);
return [...nativeLogTypes, ...webLogTypes];
}
return nativeLogTypes;
}
/**
* https://w3c.github.io/webdriver-bidi/#event-log-entryAdded
*
* @template {import('node:events').EventEmitter} EE
* @this {import('../driver').AndroidDriver}
* @param {EE} logEmitter
* @param {BiDiListenerProperties} properties
* @returns {[EE, LogListener]}
*/
export function assignBiDiLogListener (logEmitter, properties) {
const {
type,
context = NATIVE_WIN,
srcEventName = 'output',
entryTransformer,
} = properties;
const listener = (/** @type {import('../utils').LogEntry} */ logEntry) => {
const finalEntry = entryTransformer ? entryTransformer(logEntry) : logEntry;
this.eventEmitter.emit(BIDI_EVENT_NAME, makeLogEntryAddedEvent(finalEntry, context, type));
};
logEmitter.on(srcEventName, listener);
return [logEmitter, listener];
}
/**
* @this {import('../driver').AndroidDriver}
* @param {string} logType
* @returns {Promise<any>}
*/
export async function getLog(logType) {
if (this.isWebContext() && !_.keys(this.supportedLogTypes).includes(logType)) {
return await /** @type {import('appium-chromedriver').Chromedriver} */ (
this.chromedriver
).jwproxy.command('/log', 'POST', {type: logType});
}
// XXX why doesn't `super` work here?
return await BaseDriver.prototype.getLog.call(this, logType);
}
// #region Internal helpers
/**
* @param {string} sessionId
* @returns {string}
*/
const WEBSOCKET_ENDPOINT = (sessionId) =>
`${DEFAULT_WS_PATHNAME_PREFIX}/session/${sessionId}/appium/device/logcat`;
// #endregion
/**
* @typedef {import('appium-adb').ADB} ADB
*/
/**
* @typedef {Object} BiDiListenerProperties
* @property {string} type
* @property {string} [srcEventName='output']
* @property {string} [context=NATIVE_WIN]
* @property {(x: Object) => import('../utils').LogEntry} [entryTransformer]
*/
/** @typedef {(logEntry: import('../utils').LogEntry) => any} LogListener */