diff --git a/docs/man_pages/project/testing/run-ios.md b/docs/man_pages/project/testing/run-ios.md index 43259fe40f..c808c8f0e0 100644 --- a/docs/man_pages/project/testing/run-ios.md +++ b/docs/man_pages/project/testing/run-ios.md @@ -33,7 +33,7 @@ Start an emulator with specified device identifier and sdk | `$ ns run ios [--de * `--emulator` - If set, runs the app in all available and configured ios simulators. It will start a simulator if none are already running. * `--sdk` - Specifies the target simulator's sdk. * `--justlaunch` - If set, does not print the application output in the console. -* `--log ` - Also controls how much of the application output is printed. By default only its JavaScript output is shown. `debug` adds everything else the app logs - its native code and the system frameworks it uses, like Xcode's console - and `trace` adds debug level entries as well. +* `--log ` - Also controls how much of the application output is printed. By default only its JavaScript output is shown. `debug` adds everything else the app logs - its native code, `print()` output and the system frameworks it uses, like Xcode's console - and `trace` adds debug level entries as well. * `--clean` - If set, forces the complete rebuild of the native application. * `--no-watch` - If set, changes in your code will not be reflected during the execution of this command. * `--release` - If set, produces a release build by running webpack in production mode and native build in release mode. Otherwise, produces a debug build. diff --git a/lib/common/definitions/mobile.d.ts b/lib/common/definitions/mobile.d.ts index 32a4bb7f4c..12e8de36a6 100644 --- a/lib/common/definitions/mobile.d.ts +++ b/lib/common/definitions/mobile.d.ts @@ -173,6 +173,8 @@ declare global { image: string; level: string; message: string; + subsystem?: string; + category?: string; } interface IiOSLogStreamOptions { diff --git a/lib/common/mobile/ios/ios-log-entry.ts b/lib/common/mobile/ios/ios-log-entry.ts index 2405ce7a26..41d96ad7df 100644 --- a/lib/common/mobile/ios/ios-log-entry.ts +++ b/lib/common/mobile/ios/ios-log-entry.ts @@ -38,6 +38,10 @@ export function isIOSRuntimeLogEntry(entry: Mobile.IiOSLogEntry): boolean { return path.basename(entry.image || "") === "NativeScript"; } +// The runtime forwards the app's stdout and stderr under this subsystem, when +// the CLI asks for it through the forwardStdio key of the app's package.json. +export const IOS_STDIO_SUBSYSTEM = "org.nativescript.stdio"; + // Logged by the runtime itself on every launch, not by the app. const RUNTIME_NOISE = ["NativeScript loaded bundle"]; @@ -57,6 +61,12 @@ export function getIOSLogEntryText( return null; } + if (entry.subsystem === IOS_STDIO_SUBSYSTEM) { + return options.includeNative + ? `${entry.category || "stdout"}: ${entry.message}` + : null; + } + const imageName = path.basename(entry.image || ""); if (isIOSRuntimeLogEntry(entry)) { return RUNTIME_NOISE.some((noise) => entry.message.includes(noise)) diff --git a/lib/common/mobile/ios/simulator/ios-simulator-log-provider.ts b/lib/common/mobile/ios/simulator/ios-simulator-log-provider.ts index 382ba306b0..2cca5e2304 100644 --- a/lib/common/mobile/ios/simulator/ios-simulator-log-provider.ts +++ b/lib/common/mobile/ios/simulator/ios-simulator-log-provider.ts @@ -148,6 +148,8 @@ export class IOSSimulatorLogProvider image: event.senderImagePath, level: (event.messageType || "").toLowerCase(), message: event.eventMessage, + subsystem: event.subsystem, + category: event.category, }, this.$devicePlatformsConstants.iOS, deviceId, diff --git a/lib/common/test/unit-tests/mobile/ios-log-entry.ts b/lib/common/test/unit-tests/mobile/ios-log-entry.ts index 0ed9e7122b..a6f9f930e8 100644 --- a/lib/common/test/unit-tests/mobile/ios-log-entry.ts +++ b/lib/common/test/unit-tests/mobile/ios-log-entry.ts @@ -98,6 +98,57 @@ describe("getIOSLogEntryText", () => { }); }); +describe("getIOSLogEntryText for forwarded stdio", () => { + const entry = { + process: "/var/containers/Bundle/Application/X/app.app/app", + image: "/private/Frameworks/NativeScript.framework/NativeScript", + level: "default", + message: "printed", + subsystem: "org.nativescript.stdio", + category: "stdout", + }; + + it("is hidden by default, unlike the runtime's console output", () => { + assert.isNull(getIOSLogEntryText(entry, { processName: "app" })); + }); + + it("is labeled with its stream along with native output", () => { + assert.equal( + getIOSLogEntryText( + { ...entry, category: "stderr" }, + { processName: "app", includeNative: true }, + ), + "stderr: printed", + ); + }); + + it("is labeled as stdout when it has no category", () => { + assert.equal( + getIOSLogEntryText( + { ...entry, category: undefined }, + { processName: "app", includeNative: true }, + ), + "stdout: printed", + ); + }); + + it("is labeled once when a long line was split", () => { + const stitcher = new IOSLogChunkStitcher(); + const printed: string[] = []; + for (const message of ["[ns 4 1/2] one", "[ns 4 2/2] two"]) { + stitcher.add("process", { ...entry, message }, (complete) => + printed.push( + getIOSLogEntryText(complete, { + processName: "app", + includeNative: true, + }), + ), + ); + } + assert.deepEqual(printed, ["stdout: one two"]); + }); +}); + describe("getIOSLogScope", () => { it("maps the CLI log level to the app logs that are shown", () => { assert.deepEqual(getIOSLogScope("INFO", false), { diff --git a/lib/common/test/unit-tests/mobile/ios-simulator-log-provider.ts b/lib/common/test/unit-tests/mobile/ios-simulator-log-provider.ts index c10015b52c..36e91de8e1 100644 --- a/lib/common/test/unit-tests/mobile/ios-simulator-log-provider.ts +++ b/lib/common/test/unit-tests/mobile/ios-simulator-log-provider.ts @@ -4,6 +4,7 @@ import { PassThrough } from "stream"; import { IOSSimulatorLogProvider } from "../../../mobile/ios/simulator/ios-simulator-log-provider"; import { DevicePlatformsConstants } from "../../../mobile/device-platforms-constants"; import { CommonLoggerStub } from "../stubs"; +import { getIOSLogEntryText } from "../../../mobile/ios/ios-log-entry"; const deviceId = "simulator-id"; @@ -66,10 +67,45 @@ describe("IOSSimulatorLogProvider", () => { image: "/apps/app.app/Frameworks/NativeScript.framework/NativeScript", level: "default", message: "CONSOLE LOG: hello", + subsystem: undefined, + category: undefined, }, ]); }); + it("keeps the subsystem and category, so forwarded stdio stays hidden by default", async () => { + await provider.startLogProcess(deviceId); + logProcess.stdout.write( + JSON.stringify({ + eventType: "logEvent", + processID: 42, + threadID: 7, + processImagePath: "/apps/app.app/app", + senderImagePath: + "/apps/app.app/Frameworks/NativeScript.framework/NativeScript", + subsystem: "org.nativescript.stdio", + category: "stderr", + messageType: "Default", + eventMessage: "printed", + }) + "\n", + ); + await nextTick(); + + assert.equal(entries.length, 1); + assert.include(entries[0], { + subsystem: "org.nativescript.stdio", + category: "stderr", + }); + assert.isNull(getIOSLogEntryText(entries[0], { processName: "app" })); + assert.equal( + getIOSLogEntryText(entries[0], { + processName: "app", + includeNative: true, + }), + "stderr: printed", + ); + }); + it("traces what log stream writes to stderr", async () => { await provider.startLogProcess(deviceId); logProcess.stderr.write("log: Invalid predicate\n"); diff --git a/lib/controllers/prepare-controller.ts b/lib/controllers/prepare-controller.ts index 1c8036d283..18685f72ee 100644 --- a/lib/controllers/prepare-controller.ts +++ b/lib/controllers/prepare-controller.ts @@ -527,6 +527,7 @@ export class PrepareController } if (prepareData && !prepareData.release) { packageData.logFormat = IOS_LOG_FORMAT; + packageData.forwardStdio = true; } if (packageData.ios && packageData.ios.discardUncaughtJsExceptions) { packageData.discardUncaughtJsExceptions = diff --git a/lib/services/ios-log-filter.ts b/lib/services/ios-log-filter.ts index 6b5c5d53fe..4b5c3662c5 100644 --- a/lib/services/ios-log-filter.ts +++ b/lib/services/ios-log-filter.ts @@ -1,5 +1,10 @@ import { injector } from "../common/yok"; -import { IOS_LOG_PIECE_HEADER } from "../common/mobile/ios/ios-log-entry"; +import * as _ from "lodash"; +import { + getIOSLogScope, + IOS_LOG_PIECE_HEADER, + IOS_STDIO_SUBSYSTEM, +} from "../common/mobile/ios/ios-log-entry"; export class IOSLogFilter implements Mobile.IPlatformLogFilter { // Used to recognize output related to the current project @@ -28,11 +33,20 @@ export class IOSLogFilter implements Mobile.IPlatformLogFilter { // lose the header and show as consecutive lines. private pieceHeaderRegex: RegExp = IOS_LOG_PIECE_HEADER; + // Forwarded stdout and stderr lines carry their subsystem and category in + // front of the text, e.g. "[org.nativescript.stdio:stdout] printed". + private stdioTagRegex: RegExp = new RegExp( + `^\\[${_.escapeRegExp(IOS_STDIO_SUBSYSTEM)}:([^\\]]*)\\](?: |$)`, + ); + private filterActive: boolean = true; private partialLine: string = null; - constructor(private $loggingLevels: Mobile.ILoggingLevels) {} + constructor( + private $loggingLevels: Mobile.ILoggingLevels, + private $logger: ILogger, + ) {} public filterData( data: string, @@ -89,6 +103,21 @@ export class IOSLogFilter implements Mobile.IPlatformLogFilter { currentLine = currentLine.replace(filteredLineInfo[0], ""); } + const stdioTag = this.stdioTagRegex.exec(currentLine); + if (stdioTag) { + // Like the structured logs, stdio shows along with native output. + if (!getIOSLogScope(this.$logger.getLevel(), false).includeNative) { + continue; + } + + const text = currentLine + .slice(stdioTag[0].length) + .replace(this.pieceHeaderRegex, "") + .trimEnd(); + output += `${stdioTag[1] || "stdout"}: ${text}\n`; + continue; + } + currentLine = currentLine .replace(this.pieceHeaderRegex, "") // remove lading space before CONSOLE diff --git a/test/controllers/prepare-controller.ts b/test/controllers/prepare-controller.ts index db2993ec74..7104940e8c 100644 --- a/test/controllers/prepare-controller.ts +++ b/test/controllers/prepare-controller.ts @@ -200,9 +200,11 @@ describe("prepareController", () => { assert.equal(rewritten.main, "bundle.123"); }); - it("asks for the structured log format in iOS debug builds only", async () => { + it("asks for the structured log format and stdio in iOS debug builds only", async () => { assert.equal((await writePackageJson("ios", false)).logFormat, 1); assert.isUndefined((await writePackageJson("ios", true)).logFormat); + assert.isTrue((await writePackageJson("ios", false)).forwardStdio); + assert.isUndefined((await writePackageJson("ios", true)).forwardStdio); assert.isUndefined((await writePackageJson("android", false)).logFormat); }); }); diff --git a/test/services/ios-log-filter.ts b/test/services/ios-log-filter.ts index 038597cd80..11a3fee315 100644 --- a/test/services/ios-log-filter.ts +++ b/test/services/ios-log-filter.ts @@ -209,6 +209,55 @@ describe("iOSLogFilter", () => { ); }); + describe("forwarded stdio", () => { + const deviceHeader = + "\u0000Oct 29 20:20:54 iPad app(NativeScript)[1514] : "; + const simulatorHeader = + "2017-10-09 13:34:38.527844+0300 localhost app[52946]: (NativeScript) "; + const lines = [ + `${deviceHeader}[org.nativescript.stdio:stdout] printed\n`, + `${deviceHeader}[org.nativescript.stdio:stderr] logged\n`, + `${deviceHeader}[org.nativescript.stdio:stdout] [ns 2 1/2] long\n`, + `${deviceHeader}[org.nativescript.stdio:stdout] [ns 2 2/2] line\n`, + `${simulatorHeader}[org.nativescript.stdio:stdout] simulated\n`, + `${deviceHeader}CONSOLE LOG: js\n`, + ]; + const filter = (cliLogLevel: string) => { + testInjector = createTestInjector("app"); + testInjector.resolve("logger").getLevel = () => cliLogLevel; + logFilter = testInjector.resolve(IOSLogFilter); + return lines + .map((line) => + logFilter.filterData(line, { + logLevel: infoLogLevel, + projectName: "app", + }), + ) + .filter(Boolean) + .join(""); + }; + + it("is hidden by default", () => { + assert.strictEqual(filter(undefined), "CONSOLE LOG: js\n"); + assert.strictEqual(filter("INFO"), "CONSOLE LOG: js\n"); + }); + + it("shows from --log debug, labeled with its stream", () => { + assert.strictEqual( + filter("DEBUG"), + [ + "stdout: printed", + "stderr: logged", + "stdout: long", + "stdout: line", + "stdout: simulated", + "CONSOLE LOG: js", + "", + ].join("\n"), + ); + }); + }); + it("drops other processes' lines that have no image name, such as the kernel's", () => { testInjector = createTestInjector("nsconsoletablet"); logFilter = testInjector.resolve(IOSLogFilter);