Add new logging functions, more styling attributes, and test it.

This commit is contained in:
Madeorsk 2023-02-12 16:31:42 +01:00
parent 63c4b3df3e
commit a6f65c9e5b
3 changed files with 78 additions and 9 deletions

View File

@ -1,11 +1,70 @@
#pragma once
#define TERMSTYLE_RESET "\033[0m"
#define TERMSTYLE_BLACK "\033[30m"
#define TERMSTYLE_RED "\033[31m"
#define TERMSTYLE_GREEN "\033[32m"
#define TERMSTYLE_YELLOW "\033[33m"
#define TERMSTYLE_BLUE "\033[34m"
#define TERMSTYLE_MAGENTA "\033[35m"
#define TERMSTYLE_CYAN "\033[36m"
#define TERMSTYLE_WHITE "\033[37m"
#include <stdio.h>
/*
* Logging constants, that can be used to style logs.
*/
#define TERMSTYLE_RESET "\033[0m"
#define TERMSTYLE_DEFAULT_COLOR "\033[39m"
#define TERMSTYLE_BLACK "\033[30m"
#define TERMSTYLE_RED "\033[31m"
#define TERMSTYLE_GREEN "\033[32m"
#define TERMSTYLE_YELLOW "\033[33m"
#define TERMSTYLE_BLUE "\033[34m"
#define TERMSTYLE_MAGENTA "\033[35m"
#define TERMSTYLE_CYAN "\033[36m"
#define TERMSTYLE_WHITE "\033[37m"
#define TERMSTYLE_BOLD "\033[1m"
#define TERMSTYLE_DIM "\033[2m"
#define TERMSTYLE_UNDERLINED "\033[4m"
#define TERMSTYLE_BLINK "\033[5m"
#define TERMSTYLE_INVERTED "\033[7m"
#define TERMSTYLE_HIDDEN "\033[8m"
#define TERMSTYLE_BOLD_RESET "\033[21m"
#define TERMSTYLE_DIM_RESET "\033[22m"
#define TERMSTYLE_UNDERLINED_RESET "\033[24m"
#define TERMSTYLE_BLINK_RESET "\033[25m"
#define TERMSTYLE_INVERTED_RESET "\033[27m"
#define TERMSTYLE_HIDDEN_RESET "\033[28m"
/*
* Default file descriptors for out stream and error stream.
*/
#define KSRLOG_ERR_STREAM stderr
#define KSRLOG_OUT_STREAM stdout
/**
* Create a log string.
*/
#define _ksrlog_log_str(label, color, str, padding_str) padding_str color TERMSTYLE_BOLD "[" label "]" TERMSTYLE_BOLD_RESET " " str TERMSTYLE_DEFAULT_COLOR
/**
* Log an error.
*/
static inline void ksrlog_error(const char *message)
{
fprintf(KSRLOG_ERR_STREAM, _ksrlog_log_str("ERROR", TERMSTYLE_RED, "%s", " "), message);
}
/**
* Log a warning.
*/
static inline void ksrlog_warning(const char *message)
{
fprintf(KSRLOG_ERR_STREAM, _ksrlog_log_str("WARNING", TERMSTYLE_YELLOW, "%s", ""), message);
}
/**
* Log a success.
*/
static inline void ksrlog_success(const char *message)
{
fprintf(KSRLOG_OUT_STREAM, _ksrlog_log_str("SUCCESS", TERMSTYLE_GREEN, "%s", ""), message);
}

View File

@ -34,6 +34,7 @@ test_ksrpromises = executable('test_ksrpromises', [ 'tests/ksrpromises.c' ], dep
test_ksregex = executable('test_ksregex', [ 'tests/ksregex.c' ], dependencies : deps, include_directories : include_dirs, install : false)
test_ksrstring = executable('test_ksrstring', [ 'tests/ksrstring.c' ], dependencies : deps, include_directories : include_dirs, install : false)
test_ksrfiles = executable('test_ksrfiles', [ 'tests/ksrfiles.c' ], dependencies : deps, include_directories : include_dirs, install : false)
test_ksrlogging = executable('test_ksrlogging', [ 'tests/ksrlogging.c' ], dependencies : deps, include_directories : include_dirs, install : false)
# tests.
test('ksrarrays', test_ksrarrays)
@ -42,3 +43,4 @@ test('ksrpromises', test_ksrpromises)
test('ksregex', test_ksregex)
test('ksrfiles', test_ksrfiles)
test('ksrstring', test_ksrstring)
test('ksrlogging', test_ksrlogging)

8
tests/ksrlogging.c Normal file
View File

@ -0,0 +1,8 @@
#include <ksr/logging.h>
int main(void)
{
ksrlog_error("test message.");
ksrlog_warning("test message.");
ksrlog_success("test message.");
}