From 5083e3005422d05ae9205f915129fc401e5abf62 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 30 Mar 2018 08:31:09 +0200 Subject: [PATCH 01/12] doc: update link to freebsd uncrustify's config file Signed-off-by: Akim Demaille --- CODINGSTYLE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CODINGSTYLE.md b/CODINGSTYLE.md index 9b0bebca..a6788d31 100644 --- a/CODINGSTYLE.md +++ b/CODINGSTYLE.md @@ -5,7 +5,7 @@ coding style should follow the You may use tools like [uncrustify](http://uncrustify.sourceforge.net/) with this -[config file](https://github.com/freebsd/pkg/blob/master/freebsd.cfg) +[config file](https://raw.githubusercontent.com/freebsd/pkg/master/freebsd.cfg) for *new* code, though the result may not be perfect. Keep in mind that, especially for most of the `bhyve` derived code, it From f107b67e92c583b88e5824c2f6f63566997953e2 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 30 Mar 2018 08:33:59 +0200 Subject: [PATCH 02/12] Go: factor and log the opening of the tty file In some earlier experiment this function was called several times. Since it made the code slightly easier to understand, I left it. Signed-off-by: Akim Demaille --- go/hyperkit.go | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/go/hyperkit.go b/go/hyperkit.go index 3579afd7..5d116d05 100644 --- a/go/hyperkit.go +++ b/go/hyperkit.go @@ -440,6 +440,22 @@ func (h *HyperKit) buildArgs(cmdline string) { log.Debugf("hyperkit: CmdLine: %#v", h.CmdLine) } +// openTty opens the tty files for reading, and returns it. +func (h *HyperKit) openTty() *os.File { + path := fmt.Sprintf("%s/tty", h.StateDir) + for { + if res, err := os.OpenFile(path, os.O_RDONLY, 0); err != nil { + log.Infof("hyperkit: openTty: %v, retrying", err) + time.Sleep(10 * time.Millisecond) + } else { + log.Infof("hyperkit: openTty: got %v", path) + saneTerminal(res) + setRaw(res) + return res + } + } +} + // execute forges the command to run hyperkit, runs and returns it. // It also plumbs stdin/stdout/stderr. func (h *HyperKit) execute() (*exec.Cmd, error) { @@ -469,20 +485,9 @@ func (h *HyperKit) execute() (*exec.Cmd, error) { cmd.Stderr = os.Stderr } else { go func() { - ttyPath := fmt.Sprintf("%s/tty", h.StateDir) - var tty *os.File - for { - var err error - tty, err = os.OpenFile(ttyPath, os.O_RDONLY, 0) - if err == nil { - break - } - time.Sleep(10 * time.Millisecond) - } - saneTerminal(tty) - setRaw(tty) + tty := h.openTty() + defer tty.Close() io.Copy(os.Stdout, tty) - tty.Close() }() } } else if log != nil { From dd7716ca3a86f4fda5d82610aaa42e96d4ebba59 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 30 Mar 2018 08:45:40 +0200 Subject: [PATCH 03/12] Go: propagate invariants Hyperkit.check verifies that if `h.Console == ConsoleStdio`, then either `isTerminal(os.Stdout)` or `h.StateDir != ""`. In the next commit, the device configuration becomes more complex, and simplifying this conditional by eliminating impossible situations makes this next commit clearer. Signed-off-by: Akim Demaille --- go/hyperkit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/hyperkit.go b/go/hyperkit.go index 5d116d05..cfde6578 100644 --- a/go/hyperkit.go +++ b/go/hyperkit.go @@ -422,7 +422,7 @@ func (h *HyperKit) buildArgs(cmdline string) { if h.Console == ConsoleStdio && isTerminal(os.Stdout) { a = append(a, "-l", "com1,stdio") - } else if h.StateDir != "" { + } else { a = append(a, "-l", fmt.Sprintf("com1,autopty=%s/tty,log=%s/console-ring", h.StateDir, h.StateDir)) } From 9a66ade8b90de3f02642df1e56cef4d631a97552 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 3 Apr 2018 13:22:07 +0200 Subject: [PATCH 04/12] Style: use tabs Signed-off-by: Akim Demaille --- src/lib/uart_emul.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib/uart_emul.c b/src/lib/uart_emul.c index 08796b77..ef236172 100644 --- a/src/lib/uart_emul.c +++ b/src/lib/uart_emul.c @@ -45,9 +45,9 @@ #include #include -#define COM1_BASE 0x3F8 +#define COM1_BASE 0x3F8 #define COM1_IRQ 4 -#define COM2_BASE 0x2F8 +#define COM2_BASE 0x2F8 #define COM2_IRQ 3 #define DEFAULT_RCLK 1843200 @@ -91,7 +91,7 @@ struct fifo { struct ttyfd { bool opened; int fd; /* tty device file descriptor */ - int sfd; + int sfd; char *name; /* slave pty name when using autopty*/ struct termios tio_orig, tio_new; /* I/O Terminals */ }; @@ -427,7 +427,7 @@ uart_write(struct uart_softc *sc, int offset, uint8_t value) } } - switch (offset) { + switch (offset) { case REG_DATA: if (sc->mcr & MCR_LOOPBACK) { if (rxfifo_putchar(sc, value) != 0) @@ -681,15 +681,15 @@ uart_tty_backend(struct uart_softc *sc, const char *backend) static char * copy_up_to_comma(const char *from) { - char *comma = strchr(from, ','); - char *tmp = NULL; - if (comma == NULL) { - tmp = strdup(from); /* rest of string */ - } else { - ptrdiff_t length = comma - from; - tmp = strndup(from, (size_t)length); - } - return tmp; + char *comma = strchr(from, ','); + char *tmp = NULL; + if (comma == NULL) { + tmp = strdup(from); /* rest of string */ + } else { + ptrdiff_t length = comma - from; + tmp = strndup(from, (size_t)length); + } + return tmp; } int From 3fdabd9f6c60744301a49c62dc7ce8e20c17b508 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 29 Mar 2018 13:50:49 +0200 Subject: [PATCH 05/12] Offer a means to send the tty to the logs On Docker for Windows, all the logs are aggregated: GUI, LinuxKit, etc. Currently debugging on Docker for Mac is less easy, in particular because LinuxKit's log are sent to the consolefile rings of fixed sized buffers. As a result, it's hard to find the interleaving of actions between Docker for Mac and LinuxKit, the console files are fragmented and full of '\0', etc. It is possible to have hyperkit redirect the tty's output to its stderr/stdout, and then its caller (Docker for Mac's driver) could redirect these streams to the logger. Unfortunately on recent macOS releases, asl is replaced by os_log which uses exclusively the path of the executable as source (changing `argv[0]` is useless). As a result, hyperkit's logs are "credited" to the driver. The most elegant approach is to embrace asl/os_log. Currently Docker for Mac still targets 10.11, and os_log appeared in macOS 10.12, so we cannot use directly os_log's API, we use it through the asl shim. Add a new device configuration, asl, that sends the tty's output to asl. Signed-off-by: Akim Demaille --- Makefile | 1 + src/include/xhyve/asl.h | 7 ++++++ src/lib/asl.c | 56 +++++++++++++++++++++++++++++++++++++++++ src/lib/uart_emul.c | 8 ++++++ 4 files changed, 72 insertions(+) create mode 100644 src/include/xhyve/asl.h create mode 100644 src/lib/asl.c diff --git a/Makefile b/Makefile index f17b882d..98403e3f 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ VMM_LIB_SRC := \ HYPERKIT_LIB_SRC := \ src/lib/acpitbl.c \ + src/lib/asl.c \ src/lib/atkbdc.c \ src/lib/block_if.c \ src/lib/consport.c \ diff --git a/src/include/xhyve/asl.h b/src/include/xhyve/asl.h new file mode 100644 index 00000000..54b7894d --- /dev/null +++ b/src/include/xhyve/asl.h @@ -0,0 +1,7 @@ +#pragma once + +/* Initialize ASL logger and local buffer. */ +void asl_init(void); + +/* Send one character to the logger: wait for full lines before actually sending. */ +void asl_put(uint8_t _c); diff --git a/src/lib/asl.c b/src/lib/asl.c new file mode 100644 index 00000000..31895aa9 --- /dev/null +++ b/src/lib/asl.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +#include + +#include + +static aslclient asl = NULL; +static aslmsg log_msg = NULL; + +static unsigned char *buf = NULL; +static size_t buf_size = 0; +static size_t buf_capacity = 0; + +/* asl is deprecated in favor of os_log starting with macOS 10.12. */ +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + +/* Grow buf/buf_capacity. */ +static void buf_grow(void) +{ + buf_capacity = buf_capacity ? 2 * buf_capacity : 1024; + buf = realloc(buf, buf_capacity); + if (!buf) { + perror("buf_grow"); + exit(1); + } +} + + +/* Initialize ASL logger and local buffer. */ +void asl_init(void) +{ + asl = asl_open(NULL, NULL, 0); + log_msg = asl_new(ASL_TYPE_MSG); + buf_grow(); +} + + +/* Send one character to the logger: wait for full lines before actually sending. */ +void asl_put(uint8_t c) +{ + if (buf_size + 1 >= buf_capacity) { + buf_grow(); + } + if (c == '\n') { + buf[buf_size] = 0; + asl_log(asl, log_msg, ASL_LEVEL_NOTICE, "%s", buf); + buf_size = 0; + } else { + buf[buf_size] = c; + ++buf_size; + } +} diff --git a/src/lib/uart_emul.c b/src/lib/uart_emul.c index ef236172..33f53b33 100644 --- a/src/lib/uart_emul.c +++ b/src/lib/uart_emul.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -121,6 +122,7 @@ struct uart_softc { struct ttyfd tty; struct log log; + bool asl; /* Output to Apple logger. */ bool thre_int_pending; /* THRE interrupt pending */ void *arg; @@ -436,6 +438,8 @@ uart_write(struct uart_softc *sc, int offset, uint8_t value) ttywrite(&sc->tty, value); if (sc->log.ring) ringwrite(&sc->log, value); + if (sc->asl) + asl_put(value); } /* else drop on floor */ sc->thre_int_pending = true; break; @@ -773,6 +777,10 @@ uart_set_backend(struct uart_softc *sc, const char *backend, const char *devname if (uart_mapring(sc, logname) == -1) { goto err; } + } else if (strcmp("asl", backend) == 0) { + sc->asl = true; + asl_init(); + retval = 0; } else if (uart_tty_backend(sc, backend) == 0) { retval = 0; } else { From 8f07383b3685f44dc0dfa4419f673928b65bbc74 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 3 Apr 2018 17:03:49 +0200 Subject: [PATCH 06/12] Go: add ConsoleLog to redirect the tty to the Apple logs Signed-off-by: Akim Demaille --- go/hyperkit.go | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/go/hyperkit.go b/go/hyperkit.go index cfde6578..26ceab65 100644 --- a/go/hyperkit.go +++ b/go/hyperkit.go @@ -44,6 +44,8 @@ const ( ConsoleStdio = iota // ConsoleFile configures console to a tty and output to a file ConsoleFile + // ConsoleLog configures console to a tty and sends its contents to the logs + ConsoleLog defaultVPNKitSock = "Library/Containers/com.docker.docker/Data/s50" @@ -117,8 +119,7 @@ type HyperKit struct { // Memory is the amount of megabytes of memory for the VM. Memory int `json:"memory"` - // Console defines where the console of the VM should be - // connected to. ConsoleStdio and ConsoleFile are supported. + // Console defines where the console of the VM should be connected to. Console int `json:"console"` // Below here are internal members, but they are exported so @@ -199,11 +200,15 @@ func (h *HyperKit) check() error { log.Debugf("hyperkit: check %#v", h) var err error // Sanity checks on configuration - if h.Console == ConsoleFile && h.StateDir == "" { - return fmt.Errorf("If ConsoleFile is set, StateDir must be specified") - } - if h.Console == ConsoleStdio && !isTerminal(os.Stdout) && h.StateDir == "" { - return fmt.Errorf("If ConsoleStdio is set but stdio is not a terminal, StateDir must be specified") + switch h.Console { + case ConsoleFile, ConsoleLog: + if h.StateDir == "" { + return fmt.Errorf("If ConsoleFile or ConsoleLog is set, StateDir must be specified") + } + case ConsoleStdio: + if !isTerminal(os.Stdout) && h.StateDir == "" { + return fmt.Errorf("If ConsoleStdio is set but stdio is not a terminal, StateDir must be specified") + } } for _, image := range h.ISOImages { if _, err = os.Stat(image); os.IsNotExist(err) { @@ -420,10 +425,20 @@ func (h *HyperKit) buildArgs(cmdline string) { nextSlot++ } - if h.Console == ConsoleStdio && isTerminal(os.Stdout) { - a = append(a, "-l", "com1,stdio") - } else { - a = append(a, "-l", fmt.Sprintf("com1,autopty=%s/tty,log=%s/console-ring", h.StateDir, h.StateDir)) + // -l: LPC device configuration. + { + cfg := "com1" + if h.Console == ConsoleStdio && isTerminal(os.Stdout) { + cfg += fmt.Sprintf(",stdio") + } else { + cfg += fmt.Sprintf(",autopty=%s/tty", h.StateDir) + } + if h.Console == ConsoleLog { + cfg += fmt.Sprintf(",asl") + } else { + cfg += fmt.Sprintf(",log=%s/console-ring", h.StateDir) + } + a = append(a, "-l", cfg) } if h.Bootrom == "" { @@ -440,15 +455,15 @@ func (h *HyperKit) buildArgs(cmdline string) { log.Debugf("hyperkit: CmdLine: %#v", h.CmdLine) } -// openTty opens the tty files for reading, and returns it. -func (h *HyperKit) openTty() *os.File { +// openTTY opens the tty files for reading, and returns it. +func (h *HyperKit) openTTY() *os.File { path := fmt.Sprintf("%s/tty", h.StateDir) for { if res, err := os.OpenFile(path, os.O_RDONLY, 0); err != nil { - log.Infof("hyperkit: openTty: %v, retrying", err) + log.Infof("hyperkit: openTTY: %v, retrying", err) time.Sleep(10 * time.Millisecond) } else { - log.Infof("hyperkit: openTty: got %v", path) + log.Infof("hyperkit: openTTY: got %v", path) saneTerminal(res) setRaw(res) return res @@ -485,7 +500,7 @@ func (h *HyperKit) execute() (*exec.Cmd, error) { cmd.Stderr = os.Stderr } else { go func() { - tty := h.openTty() + tty := h.openTTY() defer tty.Close() io.Copy(os.Stdout, tty) }() From 336fbb825bab762338de2255c0f09d86e1bd6912 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 3 Apr 2018 17:22:01 +0200 Subject: [PATCH 07/12] Asl: prefer a static buffer Suggested by Rolf Neugebauer. Signed-off-by: Akim Demaille --- src/lib/asl.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/lib/asl.c b/src/lib/asl.c index 31895aa9..18e519c5 100644 --- a/src/lib/asl.c +++ b/src/lib/asl.c @@ -11,45 +11,32 @@ static aslclient asl = NULL; static aslmsg log_msg = NULL; -static unsigned char *buf = NULL; +static unsigned char buf[4096]; static size_t buf_size = 0; -static size_t buf_capacity = 0; /* asl is deprecated in favor of os_log starting with macOS 10.12. */ #pragma GCC diagnostic ignored "-Wdeprecated-declarations" -/* Grow buf/buf_capacity. */ -static void buf_grow(void) -{ - buf_capacity = buf_capacity ? 2 * buf_capacity : 1024; - buf = realloc(buf, buf_capacity); - if (!buf) { - perror("buf_grow"); - exit(1); - } -} - - /* Initialize ASL logger and local buffer. */ void asl_init(void) { asl = asl_open(NULL, NULL, 0); log_msg = asl_new(ASL_TYPE_MSG); - buf_grow(); } /* Send one character to the logger: wait for full lines before actually sending. */ void asl_put(uint8_t c) { - if (buf_size + 1 >= buf_capacity) { - buf_grow(); - } if (c == '\n') { buf[buf_size] = 0; asl_log(asl, log_msg, ASL_LEVEL_NOTICE, "%s", buf); buf_size = 0; } else { + if (buf_size + 2 >= sizeof buf) { + /* Running out of space, flush. */ + asl_put('\n'); + } buf[buf_size] = c; ++buf_size; } From 27aa5729052c427af049756bf697f54c0f8eda63 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 3 Apr 2018 17:23:14 +0200 Subject: [PATCH 08/12] Asl: treat \0 like \n Suggested by Ian Campbell. Signed-off-by: Akim Demaille --- src/lib/asl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/asl.c b/src/lib/asl.c index 18e519c5..bdeb954f 100644 --- a/src/lib/asl.c +++ b/src/lib/asl.c @@ -28,7 +28,7 @@ void asl_init(void) /* Send one character to the logger: wait for full lines before actually sending. */ void asl_put(uint8_t c) { - if (c == '\n') { + if ((c == '\n') || (c == 0)) { buf[buf_size] = 0; asl_log(asl, log_msg, ASL_LEVEL_NOTICE, "%s", buf); buf_size = 0; From e2af04bdf4e0fbad5f28e0cbc80e545ddb883d12 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 3 Apr 2018 17:41:15 +0200 Subject: [PATCH 09/12] Asl: use index instead of size to denote the number of stored bytes Suggested by Rolf Neugebauer. Signed-off-by: Akim Demaille --- src/lib/asl.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/lib/asl.c b/src/lib/asl.c index bdeb954f..c47b8ce9 100644 --- a/src/lib/asl.c +++ b/src/lib/asl.c @@ -12,7 +12,8 @@ static aslclient asl = NULL; static aslmsg log_msg = NULL; static unsigned char buf[4096]; -static size_t buf_size = 0; +/* Index of the _next_ character to insert in the buffer. */ +static size_t buf_idx = 0; /* asl is deprecated in favor of os_log starting with macOS 10.12. */ #pragma GCC diagnostic ignored "-Wdeprecated-declarations" @@ -29,15 +30,15 @@ void asl_init(void) void asl_put(uint8_t c) { if ((c == '\n') || (c == 0)) { - buf[buf_size] = 0; + buf[buf_idx] = 0; asl_log(asl, log_msg, ASL_LEVEL_NOTICE, "%s", buf); - buf_size = 0; + buf_idx = 0; } else { - if (buf_size + 2 >= sizeof buf) { + if (buf_idx + 2 >= sizeof buf) { /* Running out of space, flush. */ asl_put('\n'); } - buf[buf_size] = c; - ++buf_size; + buf[buf_idx] = c; + ++buf_idx; } } From bf1404f78100ee13c98cdfb6713800f09956ef22 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 4 Apr 2018 10:03:42 +0200 Subject: [PATCH 10/12] Asl: prefer sizeof() to sizeof, even for a variable Suggested by Ian Campbell. --- src/lib/asl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/asl.c b/src/lib/asl.c index c47b8ce9..d11802f2 100644 --- a/src/lib/asl.c +++ b/src/lib/asl.c @@ -34,7 +34,7 @@ void asl_put(uint8_t c) asl_log(asl, log_msg, ASL_LEVEL_NOTICE, "%s", buf); buf_idx = 0; } else { - if (buf_idx + 2 >= sizeof buf) { + if (buf_idx + 2 >= sizeof(buf)) { /* Running out of space, flush. */ asl_put('\n'); } From 5b93edd28e5811519ef9dd04647cb5d908163f83 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 5 Apr 2018 07:49:18 +0200 Subject: [PATCH 11/12] Asl: stay away from the asl prefix Suggested by Ian Campbell. Signed-off-by: Akim Demaille --- Makefile | 2 +- src/include/xhyve/{asl.h => log.h} | 4 ++-- src/lib/{asl.c => log.c} | 14 +++++++------- src/lib/uart_emul.c | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) rename src/include/xhyve/{asl.h => log.h} (75%) rename src/lib/{asl.c => log.c} (78%) diff --git a/Makefile b/Makefile index 98403e3f..6a1a6a6a 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,6 @@ VMM_LIB_SRC := \ HYPERKIT_LIB_SRC := \ src/lib/acpitbl.c \ - src/lib/asl.c \ src/lib/atkbdc.c \ src/lib/block_if.c \ src/lib/consport.c \ @@ -44,6 +43,7 @@ HYPERKIT_LIB_SRC := \ src/lib/fwctl.c \ src/lib/inout.c \ src/lib/ioapic.c \ + src/lib/log.c \ src/lib/md5c.c \ src/lib/mem.c \ src/lib/mevent.c \ diff --git a/src/include/xhyve/asl.h b/src/include/xhyve/log.h similarity index 75% rename from src/include/xhyve/asl.h rename to src/include/xhyve/log.h index 54b7894d..6853e846 100644 --- a/src/include/xhyve/asl.h +++ b/src/include/xhyve/log.h @@ -1,7 +1,7 @@ #pragma once /* Initialize ASL logger and local buffer. */ -void asl_init(void); +void log_init(void); /* Send one character to the logger: wait for full lines before actually sending. */ -void asl_put(uint8_t _c); +void log_put(uint8_t _c); diff --git a/src/lib/asl.c b/src/lib/log.c similarity index 78% rename from src/lib/asl.c rename to src/lib/log.c index d11802f2..0af47258 100644 --- a/src/lib/asl.c +++ b/src/lib/log.c @@ -6,9 +6,9 @@ #include -#include +#include -static aslclient asl = NULL; +static aslclient log_client = NULL; static aslmsg log_msg = NULL; static unsigned char buf[4096]; @@ -19,24 +19,24 @@ static size_t buf_idx = 0; #pragma GCC diagnostic ignored "-Wdeprecated-declarations" /* Initialize ASL logger and local buffer. */ -void asl_init(void) +void log_init(void) { - asl = asl_open(NULL, NULL, 0); + log_client = asl_open(NULL, NULL, 0); log_msg = asl_new(ASL_TYPE_MSG); } /* Send one character to the logger: wait for full lines before actually sending. */ -void asl_put(uint8_t c) +void log_put(uint8_t c) { if ((c == '\n') || (c == 0)) { buf[buf_idx] = 0; - asl_log(asl, log_msg, ASL_LEVEL_NOTICE, "%s", buf); + asl_log(log_client, log_msg, ASL_LEVEL_NOTICE, "%s", buf); buf_idx = 0; } else { if (buf_idx + 2 >= sizeof(buf)) { /* Running out of space, flush. */ - asl_put('\n'); + log_put('\n'); } buf[buf_idx] = c; ++buf_idx; diff --git a/src/lib/uart_emul.c b/src/lib/uart_emul.c index 33f53b33..69f23a4a 100644 --- a/src/lib/uart_emul.c +++ b/src/lib/uart_emul.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include #include @@ -439,7 +439,7 @@ uart_write(struct uart_softc *sc, int offset, uint8_t value) if (sc->log.ring) ringwrite(&sc->log, value); if (sc->asl) - asl_put(value); + log_put(value); } /* else drop on floor */ sc->thre_int_pending = true; break; @@ -779,7 +779,7 @@ uart_set_backend(struct uart_softc *sc, const char *backend, const char *devname } } else if (strcmp("asl", backend) == 0) { sc->asl = true; - asl_init(); + log_init(); retval = 0; } else if (uart_tty_backend(sc, backend) == 0) { retval = 0; From 679700db1ecdd1eb4939e2340e78fc2491e0aa6d Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 5 Apr 2018 07:57:06 +0200 Subject: [PATCH 12/12] Log: a function to flush Suggested by Ian Campbell. Signed-off-by: Akim Demaille --- src/lib/log.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib/log.c b/src/lib/log.c index 0af47258..98cc7871 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -26,17 +26,23 @@ void log_init(void) } +/* Send the content of the buffer to the logger. */ +static void log_flush(void) +{ + buf[buf_idx] = 0; + asl_log(log_client, log_msg, ASL_LEVEL_NOTICE, "%s", buf); + buf_idx = 0; +} + + /* Send one character to the logger: wait for full lines before actually sending. */ void log_put(uint8_t c) { if ((c == '\n') || (c == 0)) { - buf[buf_idx] = 0; - asl_log(log_client, log_msg, ASL_LEVEL_NOTICE, "%s", buf); - buf_idx = 0; + log_flush(); } else { if (buf_idx + 2 >= sizeof(buf)) { - /* Running out of space, flush. */ - log_put('\n'); + log_flush(); } buf[buf_idx] = c; ++buf_idx;