common: Add basic (janky) logging system

Signed-off-by: Ikey Doherty <ikey@solus-project.com>
This commit is contained in:
Ikey Doherty
2017-10-22 00:37:32 +01:00
parent fe83d34984
commit 88550aeffc
3 changed files with 159 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
/*
* This file is part of linux-steam-integration.
*
* Copyright © 2016-2017 Ikey Doherty <ikey@solus-project.com>
*
* 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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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:
*/
+49
View File
@@ -0,0 +1,49 @@
/*
* This file is part of linux-steam-integration.
*
* Copyright © 2016-2017 Ikey Doherty <ikey@solus-project.com>
*
* 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 <stdlib.h>
/**
* 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:
*/
+1
View File
@@ -2,6 +2,7 @@
lsi_common_sources = [
'files.c',
'log.c',
]
lib_lsi_common = static_library(