Skip to content
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

Logger output buffered when piping #58

Closed
kjcjohnson opened this issue Mar 13, 2024 · 2 comments
Closed

Logger output buffered when piping #58

kjcjohnson opened this issue Mar 13, 2024 · 2 comments
Assignees

Comments

@kjcjohnson
Copy link

The print function in Swift is fully buffered by default, so when piping gen-ir output, the logger outputs get delayed and, occasionally, split.

Example (gen-ir 0.5.0-alpha)
In a CI script, I want to save off the output of gen-ir into a separate file, as well as see the progress in the console. I call gen-ir in a pipe to do so:

set -o pipefail && gen-ir <build-log> <xcarchive> --project-path <workspace> | tee gen-ir.log

Here, each Running command (clang) XX of YY. Target modules processed: ZZ no longer is printed in real time, until enough are printed to fill the buffer. Occasionally, we'll also see, e.g., Running comm or similar split lines, when the full line doesn't fit inside the IO buffer.

Impact
Makes it more difficult to track the progress of gen-ir. In our case, it takes ~15 minutes to process everything, so it would be nice to have the real-time updates instead of waiting for the buffer to fill, which takes ~30s - ~1min.

Suggested Fix
Flush stdout after printing a log message. Not sure what other ways there are, but adding fflush(__stdoutp) after the print call in StdoutLogHandler.swift did it for me.

func log(
level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
source: String,
file: String,
function: String,
line: UInt
) {
let levelPrefix = prefix(for: level)
let timestamp = timestamp(for: level)
let lineInfo = lineInfo(for: level, file: file, function: function, line: line)
print("\(timestamp)\(lineInfo)\(levelPrefix)\(message)")
}

@NinjaLikesCheez
Copy link
Collaborator

@bmxav bmxav self-assigned this Sep 10, 2024
@bmxav
Copy link
Contributor

bmxav commented Sep 10, 2024

This has since been merged and will be available in v0.5.0 which we are planning to get out within the next week.

@bmxav bmxav closed this as completed Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants