Repository navigation
fix: stream downloadable emulator logs to Emulator UI #11271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
christhompsongoogle
wants to merge
1
commit into
main
Choose a base branch
from
fix-downloadable-emulator-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| const lsofi = require("lsofi"); | ||
|
Check warning on line 1 in src/emulator/downloadableEmulators.ts
|
||
| import { | ||
| Emulators, | ||
| DownloadableEmulators, | ||
|
|
@@ -116,7 +116,7 @@ | |
| version: emulatorUiDetails.version, | ||
| downloadPath: path.join(CACHE_DIR, emulatorUiDetails.downloadPathRelativeToCacheDir), | ||
| unzipDir: path.join(CACHE_DIR, `ui-v${emulatorUiDetails.version}`), | ||
| binaryPath: path.join(CACHE_DIR, emulatorUiDetails.binaryPathRelativeToCacheDir!), | ||
| opts: { | ||
| ...emulatorUiDetails, | ||
| cacheDir: CACHE_DIR, | ||
|
|
@@ -136,7 +136,7 @@ | |
| unzipDir: path.join(CACHE_DIR, `pubsub-emulator-${EMULATOR_UPDATE_DETAILS.pubsub.version}`), | ||
| binaryPath: path.join( | ||
| CACHE_DIR, | ||
| EMULATOR_UPDATE_DETAILS.pubsub.binaryPathRelativeToCacheDir!, | ||
| ), | ||
| opts: { | ||
| ...EMULATOR_UPDATE_DETAILS.pubsub, | ||
|
|
@@ -165,7 +165,7 @@ | |
|
|
||
| if (overrideVersion && overrideVersion !== details.version) { | ||
| const oldVersion = details.version; | ||
| const replaceVersion = (s: string) => s.split(oldVersion).join(overrideVersion); | ||
|
|
||
| details.version = overrideVersion; | ||
| details.downloadPath = replaceVersion(details.downloadPath); | ||
|
|
@@ -273,7 +273,7 @@ | |
| shell: false, | ||
| }, | ||
| pubsub: { | ||
| binary: `${getExecPath(Emulators.PUBSUB)!}`, | ||
| args: [], | ||
| optionalArgs: ["port", "host"], | ||
| joinArgs: true, | ||
|
|
@@ -309,7 +309,7 @@ | |
| } | ||
|
|
||
| /** | ||
| * @param name | ||
|
Check warning on line 312 in src/emulator/downloadableEmulators.ts
|
||
| */ | ||
| export function getLogFileName(name: string): string { | ||
| return `${name}-debug.log`; | ||
|
|
@@ -322,7 +322,7 @@ | |
| */ | ||
| export function _getCommand( | ||
| emulator: DownloadableEmulators, | ||
| args: { [s: string]: any }, | ||
| ): DownloadableEmulatorCommand { | ||
| const baseCmd = Commands[emulator]; | ||
| const defaultPort = Constants.getDefaultPort(emulator); | ||
|
|
@@ -335,7 +335,7 @@ | |
| if ( | ||
| baseCmd.binary === "java" && | ||
| utils.isRunningInWSL() && | ||
| (!args.host || !args.host.includes(":")) | ||
|
Check warning on line 338 in src/emulator/downloadableEmulators.ts
|
||
| ) { | ||
| // HACK(https://github.com/firebase/firebase-tools-ui/issues/332): Force | ||
| // Java to use IPv4 sockets in WSL (unless IPv6 address explicitly used). | ||
|
|
@@ -433,6 +433,16 @@ | |
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Parses the severity level from a downloadable emulator output line. | ||
| */ | ||
| export function parseLogLevel(text: string): "INFO" | "WARN" | "ERROR" | "DEBUG" { | ||
| if (/\b(SEVERE|ERROR)\b/i.test(text)) return "ERROR"; | ||
| if (/\b(WARNING|WARN)\b/i.test(text)) return "WARN"; | ||
| if (/\bINFO\b/i.test(text)) return "INFO"; | ||
| return "DEBUG"; | ||
| } | ||
|
|
||
| async function _runBinary( | ||
| emulator: DownloadableEmulatorDetails, | ||
| command: DownloadableEmulatorCommand, | ||
|
|
@@ -492,23 +502,25 @@ | |
| `${description} logging to ${clc.bold(getLogFileName(emulator.name))}`, | ||
| ); | ||
|
|
||
| emulator.instance.stdout?.on("data", (data) => { | ||
| logger.log("DEBUG", data.toString()); | ||
| emulator.instance.stdout?.on("data", (data: Buffer) => { | ||
| const text = data.toString(); | ||
| logger.log(parseLogLevel(text), text); | ||
| emulator.stdout.write(data); | ||
| }); | ||
| emulator.instance.stderr?.on("data", (data) => { | ||
| logger.log("DEBUG", data.toString()); | ||
| emulator.instance.stderr?.on("data", (data: Buffer) => { | ||
| const text = data.toString(); | ||
| logger.log(parseLogLevel(text), text); | ||
| emulator.stdout.write(data); | ||
|
|
||
| if (data.toString().includes("java.lang.UnsupportedClassVersionError")) { | ||
| if (text.includes("java.lang.UnsupportedClassVersionError")) { | ||
| logger.logLabeled( | ||
| "WARN", | ||
| emulator.name, | ||
| "Unsupported java version, make sure java --version reports 1.8 or higher.", | ||
| ); | ||
| } | ||
|
|
||
| if (data.toString().includes("address already in use")) { | ||
| if (text.includes("address already in use")) { | ||
| const message = `${description} has exited because its configured port ${command.port} is already in use. Are you running another copy of the emulator suite?`; | ||
| logger.logLabeled("ERROR", emulator.name, message); | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing the log level and logging it directly via
logger.log(parseLogLevel(text), text)will cause allINFO,WARN, andERRORlogs from the emulator child processes to be printed directly to the user's terminal/console. This will result in massive console spam during normal emulator operation.To prevent this console spam while still streaming the logs to the Emulator UI, we can log them at the
"INFO"level but include a metadata flag (e.g.,emulatorLog: true) and the parsed level inmetadata.level. We can then filter out these logs in the Console transport unless--debugis enabled.