Skip to content

Introduce a new krun_init_log() API #326

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

Merged
merged 5 commits into from
Jun 3, 2025
Merged
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
183 changes: 129 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions examples/chroot_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -34,6 +35,8 @@ static void print_help(char *const name)
"Usage: %s [OPTIONS] NEWROOT COMMAND [COMMAND_ARGS...]\n"
"OPTIONS: \n"
" -h --help Show help\n"
" --log=PATH Write libkrun log to file or named pipe at PATH\n"
" --color-log=PATH Write libkrun log to file or named pipe at PATH, use color\n"
" --net=NET_MODE Set network mode\n"
" --passt-socket=PATH Instead of starting passt, connect to passt socket at PATH"
"NET_MODE can be either TSI (default) or PASST\n"
Expand All @@ -47,19 +50,36 @@ static void print_help(char *const name)

static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "log", required_argument, NULL, 'L' },
{ "color-log", required_argument, NULL, 'C' },
{ "net_mode", required_argument, NULL, 'N' },
{ "passt-socket", required_argument, NULL, 'P' },
{ NULL, 0, NULL, 0 }
};

struct cmdline {
bool show_help;
int log_target;
uint32_t log_style;
enum net_mode net_mode;
char const *passt_socket_path;
char const *new_root;
char *const *guest_argv;
};

bool cmdline_set_log_target(struct cmdline *cmdline, const char *arg) {
int fd = open(arg, O_WRONLY);
if (fd < 0) {
perror(arg);
return false;
}
if (cmdline->log_target > 0) {
close(cmdline->log_target);
}
cmdline->log_target = fd;
return true;
}

bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
{
assert(cmdline != NULL);
Expand All @@ -71,6 +91,8 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
.passt_socket_path = NULL,
.new_root = NULL,
.guest_argv = NULL,
.log_target = KRUN_LOG_TARGET_DEFAULT,
.log_style = KRUN_LOG_STYLE_AUTO
};

int option_index = 0;
Expand All @@ -81,6 +103,14 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
case 'h':
cmdline->show_help = true;
return true;
case 'C':
cmdline->log_style = KRUN_LOG_STYLE_ALWAYS;
/* fall through */
case 'L':
if (!cmdline_set_log_target(cmdline, optarg)) {
return false;
}
break;
case 'N':
if (strcasecmp("TSI", optarg) == 0) {
cmdline->net_mode = NET_MODE_TSI;
Expand Down Expand Up @@ -217,8 +247,8 @@ int main(int argc, char *const argv[])
return 0;
}

// Set the log level to "off".
err = krun_set_log_level(0);
// Set the log level to "warn".
err = krun_init_log(cmdline.log_target, KRUN_LOG_LEVEL_WARN, cmdline.log_style, 0);
if (err) {
errno = -err;
perror("Error configuring log level");
Expand Down
Loading
Loading