ulogger
athreadsafeloggerwithcoloredoutput
colors.c
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 #include "colors.h"
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /*! @brief returns an ansi color string to be used with printf
25  */
27 
28  switch (color) {
29  case COLORS_RED:
30  return ANSI_COLOR_RED;
31  case COLORS_SOFT_RED:
32  return ANSI_COLOR_SOFT_RED;
33  case COLORS_GREEN:
34  return ANSI_COLOR_GREEN;
35  case COLORS_YELLOW:
36  return ANSI_COLOR_YELLOW;
37  case COLORS_BLUE:
38  return ANSI_COLOR_BLUE;
39  case COLORS_MAGENTA:
40  return ANSI_COLOR_MAGENTA;
41  case COLORS_CYAN:
42  return ANSI_COLOR_CYAN;
43  case COLORS_RESET:
44  return ANSI_COLOR_RESET;
45  default:
46  return NULL;
47  }
48 }
49 
50 /*! @brief prefixes a message with the given ANSI color code
51  * @return Success: char pointer containing the message prefixed with the ANSI color
52  * code
53  * @return Failure: NULL pointer
54  * @note you must free up the allocate memory for the returned vlaue
55  */
56 char *format_colored(COLORS color, char *message) {
57 
58  char *pcolor = get_ansi_color_scheme(color);
59  if (pcolor == NULL) {
60  return NULL;
61  }
62 
63  char *formatted = malloc(sizeof(message) + sizeof(pcolor));
64  if (formatted == NULL) {
65  printf("failed to format colored string\n");
66  return NULL;
67  }
68 
69  strcat(formatted, pcolor);
70  strcat(formatted, message);
71 
72  return formatted;
73 }
74 
75 /*! @brief prints message to stdout with the given color
76  */
77 void print_colored(COLORS color, char *message) {
78  printf("%s%s%s\n", get_ansi_color_scheme(color), message, ANSI_COLOR_RESET);
79 }
80 
81 /*! @brief is like print_colored except it writes the data into the given file
82  * descriptor
83  * @return Success: 0
84  * @return Failure: 1
85  */
86 int write_colored(COLORS color, int file_descriptor, char *message) {
87 
88  char *pcolor = get_ansi_color_scheme(color);
89  if (pcolor == NULL) {
90  return -1;
91  }
92 
93  char *reset = get_ansi_color_scheme(COLORS_RESET);
94  if (reset == NULL) {
95  return -1;
96  }
97 
98  size_t write_msg_size =
99  strlen(pcolor) + strlen(reset) + strlen(message) + 2; // 2 for \n
100  char write_message[write_msg_size];
101  memset(write_message, 0, write_msg_size);
102 
103  strcat(write_message, pcolor);
104  strcat(write_message, message);
105  strcat(write_message, reset);
106  strcat(write_message, "\n");
107 
108  int response = write(file_descriptor, write_message, strlen(write_message));
109  if (response == -1) {
110  printf("failed to write colored message\n");
111  return response;
112  }
113 
114  return 0;
115 }
116 
117 #ifdef __cplusplus
118 }
119 #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
COLORS_MAGENTA
@ COLORS_MAGENTA
Definition: colors.h:49
COLORS_GREEN
@ COLORS_GREEN
Definition: colors.h:46
ANSI_COLOR_RESET
#define ANSI_COLOR_RESET
Definition: colors.h:33
ANSI_COLOR_YELLOW
#define ANSI_COLOR_YELLOW
Definition: colors.h:29
format_colored
char * format_colored(COLORS color, char *message)
prefixes a message with the given ANSI color code
Definition: colors.c:56
COLORS_SOFT_RED
@ COLORS_SOFT_RED
Definition: colors.h:45
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_RESET
@ COLORS_RESET
Definition: colors.h:51
COLORS_BLUE
@ COLORS_BLUE
Definition: colors.h:48
COLORS_YELLOW
@ COLORS_YELLOW
Definition: colors.h:47
colors.h
macros and utilities for printing color to stdout from https://www.quora.com/How-do-I-print-a-colored...
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
ANSI_COLOR_RED
#define ANSI_COLOR_RED
Definition: colors.h:26
ANSI_COLOR_CYAN
#define ANSI_COLOR_CYAN
Definition: colors.h:32
ANSI_COLOR_MAGENTA
#define ANSI_COLOR_MAGENTA
Definition: colors.h:31
ANSI_COLOR_SOFT_RED
#define ANSI_COLOR_SOFT_RED
Definition: colors.h:27
ANSI_COLOR_BLUE
#define ANSI_COLOR_BLUE
Definition: colors.h:30
ANSI_COLOR_GREEN
#define ANSI_COLOR_GREEN
Definition: colors.h:28
print_colored
void print_colored(COLORS color, char *message)
prints message to stdout with the given color
Definition: colors.c:77