/
ios-log.js
196 lines (182 loc) · 6.41 KB
/
ios-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
"use strict";
var spawn = require('win-spawn')
, through = require('through')
, path = require('path')
, fs = require('fs')
, _ = require('underscore')
, glob = require('glob')
, logger = require('../../server/logger.js').get('appium');
// Date-Utils: Polyfills for the Date object
require('date-utils');
var IosLog = function (opts) {
this.udid = opts.udid;
this.simUdid = opts.simUdid;
this.xcodeVersion = opts.xcodeVersion;
this.showLogs = opts.showLogs;
this.proc = null;
this.onIosLogStart = null;
this.iosLogStarted = false;
this.iosLogStartTime = null;
this.calledBack = false;
this.loggingModeOn = true;
this.logs = [];
this.logRow = "";
this.logsSinceLastRequest = [];
if (this.xcodeVersion === null) {
logger.warn("Xcode version passed into log capture code as null, assuming Xcode 5");
this.xcodeVersion = '5.0';
}
};
IosLog.prototype.startCapture = function (cb) {
this.onIosLogStart = cb;
// Select cmd for log capture
if (this.udid) {
var spawnEnv = _.clone(process.env);
var limdDir = path.resolve(__dirname,
"../../../build/deviceconsole");
spawnEnv.PATH = process.env.PATH + ":" + limdDir;
spawnEnv.DYLD_LIBRARY_PATH = limdDir + ":" + process.env.DYLD_LIBRARY_PATH;
logger.debug("Starting iOS device log capture via deviceconsole");
// deviceconsole retrieves many old device log lines that came before it was
// started so filter those out until we encounter new log lines.
this.loggingModeOn = false;
this.proc = spawn("deviceconsole", ["-u", this.udid], {env: spawnEnv});
this.finishStartingLogCapture(cb);
} else {
var ver = parseInt(this.xcodeVersion.split(".")[0], 10);
if (ver >= 5) {
var logsPath;
if (ver >= 6) {
logger.debug("Starting iOS 8.* simulator log capture");
if (_.isUndefined(this.simUdid)) {
return cb(new Error("iOS8 log capture requires a sim udid"));
}
logsPath = path.resolve(process.env.HOME, "Library", "Logs",
"CoreSimulator", this.simUdid);
} else {
logger.debug("Starting iOS 7.* simulator log capture");
logsPath = path.resolve(process.env.HOME, "Library", "Logs",
"iOS Simulator", "7.*");
}
glob(path.resolve(logsPath, "system.log"), function (err, files) {
if (err || files.length < 1) {
logger.error("Could not start log capture because no iOS " +
"simulator logs could be found at " + logsPath + "/system.log" +
"Logging will not be functional for this run");
return cb();
} else {
var lastModifiedLogPath = files[0]
, lastModifiedLogTime = fs.statSync(files[0]).mtime;
_.each(files, function (file) {
var mtime = fs.statSync(file).mtime;
if (mtime > lastModifiedLogTime) {
lastModifiedLogPath = file;
lastModifiedLogTime = mtime;
}
});
this.proc = spawn("tail", ["-f", "-n", "1", lastModifiedLogPath]);
// -n 1 is used so that tail returns a line that lets this process know that the
// first line returned wasn't from stderr so it can tell the tailing was successful
this.finishStartingLogCapture(cb);
}
}.bind(this));
} else {
logger.debug("Starting iOS 6.* simulator log capture");
this.proc = spawn("tail", ["-f", "-n", "1", "/var/log/system.log"]);
this.finishStartingLogCapture(cb);
}
}
};
IosLog.prototype.finishStartingLogCapture = function (cb) {
this.proc.stdout.setEncoding('utf8');
this.proc.stderr.setEncoding('utf8');
this.proc.on('error', function (err) {
logger.error("iOS log capture failed: " + err.message);
if (!this.calledBack) {
this.calledBack = true;
cb(err);
}
}.bind(this));
this.proc.stdout.pipe(through(this.onStdout.bind(this)));
this.proc.stderr.pipe(through(this.onStderr.bind(this)));
};
IosLog.prototype.stopCapture = function () {
logger.debug("Stopping iOS log capture");
if (this.proc) {
this.proc.kill();
}
this.proc = null;
};
IosLog.prototype.onStdout = function (data) {
if (!this.calledBack) {
// don't store the first line of the log because it came before the sim or device was launched
this.onOutput(data);
} else {
this.logRow += data;
if (data.substr(-1, 1) === "\n") {
this.onOutput(data, "");
this.logRow = "";
}
}
};
IosLog.prototype.onStderr = function (data) {
if (/execvp\(\)/.test(data)) {
logger.error("iOS log capture process failed to start");
if (!this.calledBack) {
this.calledBack = true;
this.onIosLogStart(new Error("iOS log capture process failed to start"));
return;
}
}
this.onOutput(data, ' STDERR');
};
IosLog.prototype.onOutput = function (data, prefix) {
if (!this.iosLogStarted) {
this.iosLogStarted = true;
this.iosLogStartTime = new Date();
if (!this.calledBack) {
this.calledBack = true;
this.onIosLogStart();
}
}
var logs = this.logRow.split("\n");
_.each(logs, function (log) {
log = log.trim();
if (log) {
// Turn logging on when the 1st row that has a new date comes through.
// - Some system.log lines do not have a leading date and therefore never pass the
// logRowDate.isAfter(this.iosLogStartTime) check so we require a state flag here,
// otherwise all the lines without dates get filtered out even if they are new.
if (!this.loggingModeOn) {
var logRowParts = log.split(/\s+/);
var logRowDate = new Date(
Date.parse(this.iosLogStartTime.getFullYear() + " " + logRowParts[0] + " " + logRowParts[1] + " " + logRowParts[2])
);
if (logRowDate.isAfter(this.iosLogStartTime)) {
this.loggingModeOn = true;
}
}
if (this.loggingModeOn) {
var logObj = {
timestamp: Date.now()
, level: 'ALL'
, message: log
};
this.logs.push(logObj);
this.logsSinceLastRequest.push(logObj);
if (this.showLogs) logger.info('[IOS_SYSLOG_ROW ' + prefix + '] ' + log);
}
}
}.bind(this));
};
IosLog.prototype.getLogs = function () {
var ret = this.logsSinceLastRequest;
this.logsSinceLastRequest = [];
return ret;
};
IosLog.prototype.getAllLogs = function () {
return this.logs;
};
module.exports = function (opts) {
return new IosLog(opts);
};