The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxc/pull/1586

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
When we use only `liblxc`, we have no way to get DEBUG/TRACE
information.

With this patch, we can set `LXC_TRACE=xxx`(xxx may be `trace`,`debug`,
`ifno`, `notice`, `warn` and `error` etc.). and then we can see more info
from stdout.

This is very useful for developers when they debug programs which only
use `liblxc`.

For example:

run lxc tests without `LXC_TRACE=trace`

```bash
$ ./src/tests/lxc-test-startone
all lxc_container tests passed for lxctest1
Destroyed container lxctest1
```

and run lxc tests with `LXC_TRACE=trace`

```bash
$ LXC_TRACE=trace ./src/tests/lxc-test-startone
[INFO] lxc: lsm/lsm.c: lsm_init: 62 Initialized LSM security driver AppArmor
[INFO] lxc: lsm/lsm.c: lsm_init: 62 Initialized LSM security driver AppArmor
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 330 Going to wait for pid 16607.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 349 Trying to sync with child process.
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 3.
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 4.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 364 Successfully synced with child process.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 333 Finished waiting on pid 16607.
[INFO] lxc: lxccontainer.c: do_lxcapi_start: 802 Attempting to set proc title to [lxc monitor] /usr/local/var/lib/lxc lxctest1
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 3.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 330 Going to wait for pid 16611.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 349 Trying to sync with child process.
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 3.
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 4.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 364 Successfully synced with child process.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 333 Finished waiting on pid 16611.
[INFO] lxc: monitor.c: lxc_monitor_sock_name: 201 using monitor socket name "lxc/69d2cd1be5952f52//usr/local/var/lib/lxc" (length of socket name 43 must be <= 105)
[DEBUG] lxc: monitor.c: lxc_monitor_open: 225 opening monitor socket lxc/69d2cd1be5952f52//usr/local/var/lib/lxc with len 43
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in "RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in "RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in "RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in "RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in "RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command stop response data length is 0
[INFO] lxc: commands.c: lxc_cmd_stop: 632 Container "lxctest1" has stopped.
all lxc_container tests passed for lxctest1
[INFO] lxc: commands.c: lxc_cmd_stop: 617 Container "lxctest1" is already stopped.
[INFO] lxc: lsm/lsm.c: lsm_init: 62 Initialized LSM security driver AppArmor
Destroyed container lxctest1
```

Signed-off-by: 0x0916 <[email protected]>
From 884ec13b683b16534bbd7703ccedf6411aa16959 Mon Sep 17 00:00:00 2001
From: 0x0916 <[email protected]>
Date: Thu, 25 May 2017 15:34:26 +0800
Subject: [PATCH] log: add trace feature for liblxc

When we use only `liblxc`, we have no way to get DEBUG/TRACE
information.

With this patch, we can set `LXC_TRACE=xxx`(xxx may be `trace`,`debug`,
`ifno`, `notice`, `warn` and `error` etc.). and then we can see more info
from stdout.

This is very useful for developers when they debug programs which only
use `liblxc`.

For example:

run lxc tests without `LXC_TRACE=trace`

```bash
$ ./src/tests/lxc-test-startone
all lxc_container tests passed for lxctest1
Destroyed container lxctest1
```

and run lxc tests with `LXC_TRACE=trace`

```bash
$ LXC_TRACE=trace ./src/tests/lxc-test-startone
[INFO] lxc: lsm/lsm.c: lsm_init: 62 Initialized LSM security driver AppArmor
[INFO] lxc: lsm/lsm.c: lsm_init: 62 Initialized LSM security driver AppArmor
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 330 Going to wait for pid 16607.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 349 Trying to sync with child 
process.
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 3.
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 4.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 364 Successfully synced with child 
process.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 333 Finished waiting on pid 16607.
[INFO] lxc: lxccontainer.c: do_lxcapi_start: 802 Attempting to set proc title 
to [lxc monitor] /usr/local/var/lib/lxc lxctest1
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 3.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 330 Going to wait for pid 16611.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 349 Trying to sync with child 
process.
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 3.
[INFO] lxc: start.c: lxc_check_inherited: 243 Closed inherited fd: 4.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 364 Successfully synced with child 
process.
[DEBUG] lxc: monitor.c: lxc_monitord_spawn: 333 Finished waiting on pid 16611.
[INFO] lxc: monitor.c: lxc_monitor_sock_name: 201 using monitor socket name 
"lxc/69d2cd1be5952f52//usr/local/var/lib/lxc" (length of socket name 43 must be 
<= 105)
[DEBUG] lxc: monitor.c: lxc_monitor_open: 225 opening monitor socket 
lxc/69d2cd1be5952f52//usr/local/var/lib/lxc with len 43
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data 
length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in 
"RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data 
length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in 
"RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data 
length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in 
"RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data 
length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in 
"RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command get_state response data 
length is 0
[DEBUG] lxc: commands.c: lxc_cmd_get_state: 586 Container "lxctest1" is in 
"RUNNING" state.
[DEBUG] lxc: commands.c: lxc_cmd_rsp_recv: 203 command stop response data 
length is 0
[INFO] lxc: commands.c: lxc_cmd_stop: 632 Container "lxctest1" has stopped.
all lxc_container tests passed for lxctest1
[INFO] lxc: commands.c: lxc_cmd_stop: 617 Container "lxctest1" is already 
stopped.
[INFO] lxc: lsm/lsm.c: lsm_init: 62 Initialized LSM security driver AppArmor
Destroyed container lxctest1
```

Signed-off-by: 0x0916 <[email protected]>
---
 src/lxc/log.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/lxc/log.c b/src/lxc/log.c
index 339b81c61..821e89596 100644
--- a/src/lxc/log.c
+++ b/src/lxc/log.c
@@ -139,6 +139,17 @@ static int log_append_stderr(const struct lxc_log_appender 
*appender,
        return 0;
 }
 
+static int log_append_stdout(const struct lxc_log_appender *appender,
+                            struct lxc_log_event *event)
+{
+       fprintf(stdout, "[%s] ", lxc_log_priority_to_string(event->priority));
+       fprintf(stdout, "%s: %s%s", log_prefix, log_vmname ? log_vmname : "", 
log_vmname ? ": " : "");
+       fprintf(stdout, "%s: %s: %d ", event->locinfo->file, 
event->locinfo->func, event->locinfo->line);
+       vfprintf(stdout, event->fmt, *event->vap);
+       fprintf(stdout, "\n");
+       return 0;
+}
+
 /*---------------------------------------------------------------------------*/
 int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespec 
*time)
 {
@@ -310,6 +321,12 @@ static struct lxc_log_appender log_appender_syslog = {
        .next           = NULL,
 };
 
+static struct lxc_log_appender log_appender_stdout = {
+       .name           = "stdout",
+       .append         = log_append_stdout,
+       .next           = NULL,
+};
+
 static struct lxc_log_appender log_appender_stderr = {
        .name           = "stderr",
        .append         = log_append_stderr,
@@ -687,3 +704,27 @@ extern void lxc_log_options_no_override()
        lxc_quiet_specified = 1;
        lxc_loglevel_specified = 1;
 }
+
+static int lxc_log_stdout() {
+       struct lxc_log_appender *appender;
+
+       if (!lxc_log_category_lxc.appender) {
+               lxc_log_category_lxc.appender = &log_appender_stdout;
+               return 0;
+       }
+       appender = lxc_log_category_lxc.appender;
+       while (appender->next != NULL)
+               appender = appender->next;
+       appender->next = &log_appender_stdout;
+       return 0;
+}
+
+__attribute__((constructor))
+void init_log() {
+       char *level;
+       level = getenv("LXC_TRACE");
+       if (level) {
+               lxc_log_stdout();
+               lxc_log_category_lxc.priority = lxc_log_priority_to_int(level);
+       }
+}
_______________________________________________
lxc-devel mailing list
[email protected]
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to