mirror of
https://gitlab.freedesktop.org/pipewire/pipewire
synced 2024-07-21 18:26:24 +00:00
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:
parent
17cda59478
commit
86bd0eb708
|
@ -171,5 +171,7 @@ environment variables:
|
||||||
- `PIPEWIRE_LOG_SYSTEMD=false`: Disable logging to the systemd journal.
|
- `PIPEWIRE_LOG_SYSTEMD=false`: Disable logging to the systemd journal.
|
||||||
- `PIPEWIRE_LOG=<filename>`: Redirect the log to the given filename.
|
- `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_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.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -284,7 +284,8 @@ do { \
|
||||||
|
|
||||||
/** keys can be given when initializing the logger handle */
|
/** keys can be given when initializing the logger handle */
|
||||||
#define SPA_KEY_LOG_LEVEL "log.level" /**< the default log level */
|
#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
|
#define SPA_KEY_LOG_FILE "log.file" /**< log to the specified file instead of
|
||||||
* stderr. */
|
* stderr. */
|
||||||
#define SPA_KEY_LOG_TIMESTAMP "log.timestamp" /**< log timestamps */
|
#define SPA_KEY_LOG_TIMESTAMP "log.timestamp" /**< log timestamps */
|
||||||
|
|
|
@ -295,6 +295,7 @@ impl_init(const struct spa_handle_factory *factory,
|
||||||
struct spa_loop *loop = NULL;
|
struct spa_loop *loop = NULL;
|
||||||
const char *str, *dest = "";
|
const char *str, *dest = "";
|
||||||
bool linebuf = false;
|
bool linebuf = false;
|
||||||
|
bool force_colors = false;
|
||||||
|
|
||||||
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||||
spa_return_val_if_fail(handle != 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);
|
this->timestamp = spa_atob(str);
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LINE)) != NULL)
|
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LINE)) != NULL)
|
||||||
this->line = spa_atob(str);
|
this->line = spa_atob(str);
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_COLORS)) != NULL)
|
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_COLORS)) != NULL) {
|
||||||
this->colors = spa_atob(str);
|
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)
|
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_LEVEL)) != NULL)
|
||||||
this->log.level = atoi(str);
|
this->log.level = atoi(str);
|
||||||
if ((str = spa_dict_lookup(info, SPA_KEY_LOG_FILE)) != NULL) {
|
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)
|
if (linebuf)
|
||||||
setlinebuf(this->file);
|
setlinebuf(this->file);
|
||||||
|
|
||||||
if (!isatty(fileno(this->file)))
|
if (!isatty(fileno(this->file)) && !force_colors) {
|
||||||
this->colors = false;
|
this->colors = false;
|
||||||
|
}
|
||||||
|
|
||||||
spa_ringbuffer_init(&this->trace_rb);
|
spa_ringbuffer_init(&this->trace_rb);
|
||||||
|
|
||||||
|
|
|
@ -598,8 +598,11 @@ void pw_init(int *argc, char **argv[])
|
||||||
char *patterns = NULL;
|
char *patterns = NULL;
|
||||||
|
|
||||||
n_items = 0;
|
n_items = 0;
|
||||||
if (!support->no_color)
|
if (!support->no_color) {
|
||||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_COLORS, "true");
|
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");
|
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_TIMESTAMP, "true");
|
||||||
if ((str = getenv("PIPEWIRE_LOG_LINE")) == NULL || spa_atob(str))
|
if ((str = getenv("PIPEWIRE_LOG_LINE")) == NULL || spa_atob(str))
|
||||||
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_LINE, "true");
|
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_LOG_LINE, "true");
|
||||||
|
|
Loading…
Reference in a new issue