Enable lg2 log output to stderr in OpenBMC CI test
Similar to printing logs to stderr in a TTY, output to stderr can be
enabled during OpenBMC CI test by setting the `LG2_FORCE_STDERR`
environment variable to any value.
Using meson, this can be done with something like:
test('test_name', executable(...), env: ['LG2_FORCE_STDERR=yes'])
Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
Change-Id: I0323fbb75fe211148ec4dc7e263fe18aabb8ffc6
diff --git a/docs/structured-logging.md b/docs/structured-logging.md
index b7a4178..ba1d44c 100644
--- a/docs/structured-logging.md
+++ b/docs/structured-logging.md
@@ -68,7 +68,7 @@
`lg2::log`. The basic format of a log call is:
```
- lg2::level("A {TAG0} occured.", "TAG0", "foo"_s, "TAG1", lg2::hex, 2);
+ lg2::level("A {TAG0} occured.", "TAG0", "foo"s, "TAG1", lg2::hex, 2);
```
Each log call has a level or priority, but the level is indicated by the
@@ -95,14 +95,14 @@
- The [integer] data should be formatted in the requested manner.
- Decimal is the default.
- Examples:
- + `(bin, 0xabcd)` -> `0b1010101111001101`
- + `(hex, 1234)` -> `0x4d2`
+ + `bin, 0xabcd` -> `0b1010101111001101`
+ + `hex, 1234` -> `0x4d2`
- `field8`, `field16`, `field32`, `field64`
- The [integer] data should be padded as if it were a field of
specified bit-length (useful only for `bin` or `hex` data).
- Examples:
- + `(bin | field8, 0xff)` -> `0b11111111`
- + `(hex | field16, 10)` -> `0x000a`
+ + `(bin | field8), 0xff` -> `0b11111111`
+ + `(hex | field16), 10` -> `0x000a`
Format flags can be OR'd together as necessary: `hex | field32`.
@@ -144,11 +144,11 @@
The code that enables compile-time header checking imposes two constraints:
1. Keys / headers must be passed as constant C-string values.
- - `"KEY"` is valid; `"KEY"_s` or `variable_key` is not.
+ - `"KEY"` is valid; `"KEY"s` or `variable_key` is not.
2. Any constant C-string may be interpreted as a key and give non-obvious
compile warnings about format violations.
- Constant C-strings (`"a string"`) should be passed as a C++ literal
- (`"a string"_s`) instead.
+ (`"a string"s`) instead.
### stderr output
@@ -156,6 +156,10 @@
not be obvious that the application is writing to the journal. The `lg2` APIs
detect if the application is running on a TTY and additionally log to the TTY.
+Output to stderr can also be forced by setting the `LG2_FORCE_STDERR`
+environment variable to any value. This is especially useful to see log output
+in OpenBMC CI test verfication.
+
The format of information sent to the TTY can be adjusted by setting the
desired format string in the `LG2_FORMAT` environment variable. Supported
fields are:
diff --git a/lib/lg2_logger.cpp b/lib/lg2_logger.cpp
index 3276499..3c61003 100644
--- a/lib/lg2_logger.cpp
+++ b/lib/lg2_logger.cpp
@@ -207,9 +207,12 @@
std::cerr << std::endl;
}
-// Use the cerr output method if we are on a TTY.
+// Use the cerr output method if we are on a TTY or if explicitly set via
+// environment variable.
static auto extra_output_method =
- isatty(fileno(stderr)) ? cerr_extra_output : noop_extra_output;
+ (isatty(fileno(stderr)) || nullptr != getenv("LG2_FORCE_STDERR"))
+ ? cerr_extra_output
+ : noop_extra_output;
// Do_log implementation.
void do_log(level l, const lg2::source_location& s, const char* m, ...)