ulogger
athreadsafeloggerwithcoloredoutput
colors.h
Go to the documentation of this file.
1
// Copyright 2020 Bonedaddy (Alexandre Trottier)
2
//
3
// licensed under GNU AFFERO GENERAL PUBLIC LICENSE;
4
// you may not use this file except in compliance with the License;
5
// You may obtain the license via the LICENSE file in the repository root;
6
//
7
// Unless required by applicable law or agreed to in writing, software
8
// distributed under the License is distributed on an "AS IS" BASIS,
9
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
// See the License for the specific language governing permissions and
11
// limitations under the License.
12
13
/*! @file colors.h
14
* @brief macros and utilities for printing color to stdout
15
* from https://www.quora.com/How-do-I-print-a-colored-output-in-C
16
* Pass a `COLORS_x` enum value into `print_colored` and the color will be printed on
17
* stdout Note that if you want to disable printing of that color you'll have to send
18
* the `COLORS_RESET` enum value through to make non-bold change 0->1 (0;31m red) vs
19
* (1;31m bold red)
20
*/
21
22
#pragma once
23
24
#include <stdbool.h>
25
26
#define ANSI_COLOR_RED "\x1b[1;31m"
27
#define ANSI_COLOR_SOFT_RED "\x1b[1;38;5;210m"
28
#define ANSI_COLOR_GREEN "\x1b[1;32m"
29
#define ANSI_COLOR_YELLOW "\x1b[1;33m"
30
#define ANSI_COLOR_BLUE "\x1b[1;34m"
31
#define ANSI_COLOR_MAGENTA "\x1b[1;35m"
32
#define ANSI_COLOR_CYAN "\x1b[1;36m"
33
#define ANSI_COLOR_RESET "\x1b[1;0m"
34
35
#ifdef __cplusplus
36
extern
"C"
{
37
#endif
38
39
/*! @brief allows short-handed references to ANSI color schemes, and enables easier
40
* color selection anytime you want to extend the available colors with an additional
41
* enum, add a switch case in get_ansi_color_scheme
42
*/
43
typedef
enum
{
44
COLORS_RED
,
45
COLORS_SOFT_RED
,
46
COLORS_GREEN
,
47
COLORS_YELLOW
,
48
COLORS_BLUE
,
49
COLORS_MAGENTA
,
50
COLORS_CYAN
,
51
COLORS_RESET
52
}
COLORS
;
53
54
/*! @brief prefixes a message with the given ANSI color code
55
* @return Success: char pointer containing the message prefixed with the ANSI color
56
* code
57
* @return Failure: NULL pointer
58
* @note you must free up the allocate memory for the returned vlaue
59
*/
60
char
*
format_colored
(
COLORS
color,
char
*message);
61
62
/*! @brief returns an ansi color string to be used with printf
63
*/
64
char
*
get_ansi_color_scheme
(
COLORS
color);
65
66
/*! @brief prints message to stdout with the given color
67
*/
68
void
print_colored
(
COLORS
color,
char
*message);
69
70
/*! @brief is like print_colored except it writes the data into the given file
71
* descriptor
72
* @return Success: 0
73
* @return Failure: 1
74
*/
75
int
write_colored
(
COLORS
color,
int
file_descriptor,
char
*message);
76
77
#ifdef __cplusplus
78
}
79
#endif
COLORS_CYAN
@ COLORS_CYAN
Definition:
colors.h:50
COLORS_RED
@ COLORS_RED
Definition:
colors.h:44
COLORS
COLORS
allows short-handed references to ANSI color schemes, and enables easier color selection anytime you ...
Definition:
colors.h:43
format_colored
char * format_colored(COLORS color, char *message)
prefixes a message with the given ANSI color code
Definition:
colors.c:56
write_colored
int write_colored(COLORS color, int file_descriptor, char *message)
is like print_colored except it writes the data into the given file descriptor
Definition:
colors.c:86
COLORS_MAGENTA
@ COLORS_MAGENTA
Definition:
colors.h:49
COLORS_GREEN
@ COLORS_GREEN
Definition:
colors.h:46
COLORS_SOFT_RED
@ COLORS_SOFT_RED
Definition:
colors.h:45
get_ansi_color_scheme
char * get_ansi_color_scheme(COLORS color)
returns an ansi color string to be used with printf
Definition:
colors.c:26
COLORS_RESET
@ COLORS_RESET
Definition:
colors.h:51
COLORS_BLUE
@ COLORS_BLUE
Definition:
colors.h:48
COLORS_YELLOW
@ COLORS_YELLOW
Definition:
colors.h:47
print_colored
void print_colored(COLORS color, char *message)
prints message to stdout with the given color
Definition:
colors.c:77
include
colors.h
Generated by
1.8.18