logger: Add a mechanism to force colourised logging

This is handy if we want to redirect to a file but have colours to make
manual parsing easier (for example with `less -R`).
This commit is contained in:
Arun Raghavan 2023-08-08 08:23:31 -04:00 committed by Wim Taymans
parent 17cda59478
commit 86bd0eb708
4 changed files with 20 additions and 6 deletions

View File

@ -171,5 +171,7 @@ environment variables:
- `PIPEWIRE_LOG_SYSTEMD=false`: Disable logging to the systemd journal.
- `PIPEWIRE_LOG=<filename>`: Redirect the log to the given filename.
- `PIPEWIRE_LOG_LINE=false`: Don't log filename, function, and source code line.
- `PIPEWIRE_LOG_COLOR=true/false/force`: Enable/disable color logging, and optionally force
colors even when logging to a file.
*/

View File

@ -284,7 +284,8 @@ do { \
/** keys can be given when initializing the logger handle */
#define SPA_KEY_LOG_LEVEL "log.level" /**< the default log level */
#define SPA_KEY_LOG_COLORS "log.colors" /**< enable colors in the logger */
#define SPA_KEY_LOG_COLORS "log.colors" /**< enable colors in the logger, set to "force" to enable
* colors even when not logging to a terminal */
#define SPA_KEY_LOG_FILE "log.file" /**< log to the specified file instead of
* stderr. */
#define SPA_KEY_LOG_TIMESTAMP "log.timestamp" /**< log timestamps */

View File

@ -295,6 +295,7 @@ impl_init(const struct spa_handle_factory *factory,
struct spa_loop *loop = NULL;
const char *str, *dest = "";
bool linebuf = false;
bool force_colors = false;
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(handle != NULL, -EINVAL);
@ -333,8 +334,14 @@ impl_init(const struct spa_handle_factory *factory,
this->timestamp = spa_atob(str);
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LINE)) != NULL)
this->line = spa_atob(str);
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_COLORS)) != NULL)
this->colors = spa_atob(str);
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_COLORS)) != NULL) {
if (spa_streq(str, "force")) {
this->colors = true;
force_colors = true;
} else {
this->colors = spa_atob(str);
}
}
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LEVEL)) != NULL)
this->log.level = atoi(str);
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_FILE)) != NULL) {
@ -363,8 +370,9 @@ impl_init(const struct spa_handle_factory *factory,
if (linebuf)
setlinebuf(this->file);
if (!isatty(fileno(this->file)))
if (!isatty(fileno(this->file)) && !force_colors) {
this->colors = false;
}
spa_ringbuffer_init(&this->trace_rb);

View File

@ -598,8 +598,11 @@ void pw_init(int *argc, char **argv[])
char *patterns = NULL;
n_items = 0;
if (!support->no_color)
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_COLORS, "true");
if (!support->no_color) {
if ((str = getenv("PIPEWIRE_LOG_COLOR")) == NULL)
str = "true";
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_COLORS, str);
}
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_TIMESTAMP, "true");
if ((str = getenv("PIPEWIRE_LOG_LINE")) == NULL || spa_atob(str))
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_LINE, "true");