From 88550aeffc1f448a3dd138bf2c4aaf1d9e4790db Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Sun, 22 Oct 2017 00:37:23 +0100 Subject: [PATCH] common: Add basic (janky) logging system Signed-off-by: Ikey Doherty --- src/common/log.c | 109 +++++++++++++++++++++++++++++++++++++++++ src/common/log.h | 49 ++++++++++++++++++ src/common/meson.build | 1 + 3 files changed, 159 insertions(+) create mode 100644 src/common/log.c create mode 100644 src/common/log.h diff --git a/src/common/log.c b/src/common/log.c new file mode 100644 index 0000000..218f47e --- /dev/null +++ b/src/common/log.c @@ -0,0 +1,109 @@ +/* + * This file is part of linux-steam-integration. + * + * Copyright © 2016-2017 Ikey Doherty + * + * linux-steam-integration is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include + +#include "log.h" +#include "nica/util.h" + +void lsi_log_debug(const char *format, ...) +{ + if (!getenv("LSI_DEBUG")) { + return; + } + + autofree(char) *p = NULL; + va_list va; + va_start(va, format); + + if (!vasprintf(&p, format, va)) { + /* At least print *something */ + fprintf(stderr, "%s\n", format); + goto end; + } + + fprintf(stderr, "\033[32;1m[lsi:debug]\033[0m: %s\n", p); + +end: + va_end(va); +} + +void lsi_log_info(const char *format, ...) +{ + autofree(char) *p = NULL; + va_list va; + va_start(va, format); + + if (!vasprintf(&p, format, va)) { + /* At least print *something */ + fprintf(stderr, "%s\n", format); + goto end; + } + + fprintf(stderr, "\034[32;1m[lsi:info]\033[0m: %s\n", p); + +end: + va_end(va); +} + +void lsi_log_warn(const char *format, ...) +{ + autofree(char) *p = NULL; + va_list va; + va_start(va, format); + + if (!vasprintf(&p, format, va)) { + /* At least print *something */ + fprintf(stderr, "%s\n", format); + goto end; + } + + fprintf(stderr, "\033[33;1m[lsi:warn]\033[0m: %s\n", p); + +end: + va_end(va); +} + +void lsi_log_error(const char *format, ...) +{ + autofree(char) *p = NULL; + va_list va; + va_start(va, format); + + if (!vasprintf(&p, format, va)) { + /* At least print *something */ + fprintf(stderr, "%s\n", format); + goto end; + } + + fprintf(stderr, "\033[31;1m[lsi:error]\033[0m: %s\n", p); + +end: + va_end(va); +} + +/* + * Editor modelines - https://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 8 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * vi: set shiftwidth=8 tabstop=8 expandtab: + * :indentSize=8:tabSize=8:noTabs=true: + */ diff --git a/src/common/log.h b/src/common/log.h new file mode 100644 index 0000000..18fbcc1 --- /dev/null +++ b/src/common/log.h @@ -0,0 +1,49 @@ +/* + * This file is part of linux-steam-integration. + * + * Copyright © 2016-2017 Ikey Doherty + * + * linux-steam-integration is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + */ + +#pragma once + +#define _GNU_SOURCE + +#include + +/** + * Emit some debug information if LSI_DEBUG is set + */ +void lsi_log_debug(const char *format, ...) __attribute__((format(printf, 1, 2))); + +/** + * Emit information, always shown + */ +void lsi_log_info(const char *format, ...) __attribute__((format(printf, 1, 2))); + +/** + * Emit a warning, always shown + */ +void lsi_log_warn(const char *format, ...) __attribute__((format(printf, 1, 2))); + +/** + * Emit an error, always shown + */ +void lsi_log_error(const char *format, ...) __attribute__((format(printf, 1, 2))); + +/* + * Editor modelines - https://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 8 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * vi: set shiftwidth=8 tabstop=8 expandtab: + * :indentSize=8:tabSize=8:noTabs=true: + */ diff --git a/src/common/meson.build b/src/common/meson.build index 4b25a0a..8693323 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -2,6 +2,7 @@ lsi_common_sources = [ 'files.c', + 'log.c', ] lib_lsi_common = static_library(