Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/man_pages/project/testing/run-ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Level>` - 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 <Level>` - 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.
Expand Down
2 changes: 2 additions & 0 deletions lib/common/definitions/mobile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ declare global {
image: string;
level: string;
message: string;
subsystem?: string;
category?: string;
}

interface IiOSLogStreamOptions {
Expand Down
10 changes: 10 additions & 0 deletions lib/common/mobile/ios/ios-log-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand All @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions lib/common/mobile/ios/simulator/ios-simulator-log-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 51 additions & 0 deletions lib/common/test/unit-tests/mobile/ios-log-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand Down
36 changes: 36 additions & 0 deletions lib/common/test/unit-tests/mobile/ios-simulator-log-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions lib/controllers/prepare-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
33 changes: 31 additions & 2 deletions lib/services/ios-log-filter.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/controllers/prepare-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
49 changes: 49 additions & 0 deletions test/services/ios-log-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,55 @@ describe("iOSLogFilter", () => {
);
});

describe("forwarded stdio", () => {
const deviceHeader =
"\u0000Oct 29 20:20:54 iPad app(NativeScript)[1514] <Notice>: ";
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);
Expand Down
Loading