-
Notifications
You must be signed in to change notification settings - Fork 249
vminitd: Add ability to set log level #531
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
dcantah
wants to merge
1
commit into
apple:main
Choose a base branch
from
dcantah:vminitd-log-level
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.
+88
−40
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,46 +14,44 @@ | |
| // limitations under the License. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import ArgumentParser | ||
| import ContainerizationOS | ||
| import Foundation | ||
| import Logging | ||
|
|
||
| @main | ||
| struct Application { | ||
| static func main() async throws { | ||
| LoggingSystem.bootstrap(StreamLogHandler.standardError) | ||
|
|
||
| // Parse command line arguments | ||
| let args = CommandLine.arguments | ||
| let command = args.count > 1 ? args[1] : "init" | ||
| struct Application: AsyncParsableCommand { | ||
| static let configuration = CommandConfiguration( | ||
| commandName: "vminitd", | ||
| abstract: "Virtual machine init daemon", | ||
| version: "0.1.0", | ||
| subcommands: [ | ||
| InitCommand.self, | ||
| PauseCommand.self, | ||
| ], | ||
| defaultSubcommand: InitCommand.self | ||
| ) | ||
|
|
||
| switch command { | ||
| case "pause": | ||
| let log = Logger(label: "pause") | ||
|
|
||
| log.info("Running pause command") | ||
| try PauseCommand.run(log: log) | ||
| case "init": | ||
| fallthrough | ||
| default: | ||
| let log = Logger(label: "vminitd") | ||
| static func main() async throws { | ||
| // Swift has issues spawning threads if /proc isn't mounted, | ||
| // so we do this synchronously before any async code runs. | ||
| try mountProc() | ||
|
|
||
| log.info("Running init command") | ||
| try Self.mountProc(log: log) | ||
| try await InitCommand.run(log: log) | ||
| var command = try parseAsRoot() | ||
| if let asyncCommand = command as? AsyncParsableCommand { | ||
| nonisolated(unsafe) var unsafeCommand = asyncCommand | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does this need to be an unsafe variable? |
||
| try await unsafeCommand.run() | ||
| } else { | ||
| try command.run() | ||
| } | ||
| } | ||
|
|
||
| // Swift seems like it has some fun issues trying to spawn threads if /proc isn't around, so we | ||
| // do this before calling our first async function. | ||
| static func mountProc(log: Logger) throws { | ||
| private static func mountProc() throws { | ||
| // Is it already mounted (would only be true in debug builds where we re-exec ourselves)? | ||
| if isProcMounted() { | ||
| return | ||
| } | ||
|
|
||
| log.info("mounting /proc") | ||
|
|
||
| let mnt = ContainerizationOS.Mount( | ||
| type: "proc", | ||
| source: "proc", | ||
|
|
@@ -63,7 +61,7 @@ struct Application { | |
| try mnt.mount(createWithPerms: 0o755) | ||
| } | ||
|
|
||
| static func isProcMounted() -> Bool { | ||
| private static func isProcMounted() -> Bool { | ||
| guard let data = try? String(contentsOfFile: "/proc/mounts", encoding: .utf8) else { | ||
| return false | ||
| } | ||
|
|
@@ -81,3 +79,36 @@ struct Application { | |
| return false | ||
| } | ||
| } | ||
|
|
||
| struct LogLevelOption: ParsableArguments { | ||
| @Option(name: .long, help: "Set the log level (trace, debug, info, notice, warning, error, critical)") | ||
| var logLevel: String = "info" | ||
|
|
||
| func resolvedLogLevel() -> Logger.Level { | ||
| switch logLevel.lowercased() { | ||
| case "trace": | ||
| return .trace | ||
| case "debug": | ||
| return .debug | ||
| case "info": | ||
| return .info | ||
| case "notice": | ||
| return .notice | ||
| case "warning": | ||
| return .warning | ||
| case "error": | ||
| return .error | ||
| case "critical": | ||
| return .critical | ||
| default: | ||
| return .info | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func makeLogger(label: String, level: Logger.Level) -> Logger { | ||
| LoggingSystem.bootstrap(StreamLogHandler.standardError) | ||
| var log = Logger(label: label) | ||
| log.logLevel = level | ||
| return log | ||
| } | ||
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
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.
Should this be the version of the previous tag + commit info if not at a tagged commit?
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.
We could, I'm not too worried about the version at the moment