ulogger
athreadsafeloggerwithcoloredoutput
colors.h File Reference

macros and utilities for printing color to stdout from https://www.quora.com/How-do-I-print-a-colored-output-in-C Pass a COLORS_x enum value into print_colored and the color will be printed on stdout Note that if you want to disable printing of that color you'll have to send the COLORS_RESET enum value through to make non-bold change 0->1 (0;31m red) vs (1;31m bold red) More...

#include <stdbool.h>
Include dependency graph for colors.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ANSI_COLOR_RED   "\x1b[1;31m"
 
#define ANSI_COLOR_SOFT_RED   "\x1b[1;38;5;210m"
 
#define ANSI_COLOR_GREEN   "\x1b[1;32m"
 
#define ANSI_COLOR_YELLOW   "\x1b[1;33m"
 
#define ANSI_COLOR_BLUE   "\x1b[1;34m"
 
#define ANSI_COLOR_MAGENTA   "\x1b[1;35m"
 
#define ANSI_COLOR_CYAN   "\x1b[1;36m"
 
#define ANSI_COLOR_RESET   "\x1b[1;0m"
 

Enumerations

enum  COLORS {
  COLORS_RED, COLORS_SOFT_RED, COLORS_GREEN, COLORS_YELLOW,
  COLORS_BLUE, COLORS_MAGENTA, COLORS_CYAN, COLORS_RESET
}
 allows short-handed references to ANSI color schemes, and enables easier color selection anytime you want to extend the available colors with an additional enum, add a switch case in get_ansi_color_scheme More...
 

Functions

char * format_colored (COLORS color, char *message)
 prefixes a message with the given ANSI color code More...
 
char * get_ansi_color_scheme (COLORS color)
 returns an ansi color string to be used with printf More...
 
void print_colored (COLORS color, char *message)
 prints message to stdout with the given color More...
 
int write_colored (COLORS color, int file_descriptor, char *message)
 is like print_colored except it writes the data into the given file descriptor More...
 

Detailed Description

macros and utilities for printing color to stdout from https://www.quora.com/How-do-I-print-a-colored-output-in-C Pass a COLORS_x enum value into print_colored and the color will be printed on stdout Note that if you want to disable printing of that color you'll have to send the COLORS_RESET enum value through to make non-bold change 0->1 (0;31m red) vs (1;31m bold red)

Definition in file colors.h.

Macro Definition Documentation

◆ ANSI_COLOR_BLUE

#define ANSI_COLOR_BLUE   "\x1b[1;34m"

Definition at line 31 of file colors.h.

◆ ANSI_COLOR_CYAN

#define ANSI_COLOR_CYAN   "\x1b[1;36m"

Definition at line 33 of file colors.h.

◆ ANSI_COLOR_GREEN

#define ANSI_COLOR_GREEN   "\x1b[1;32m"

Definition at line 29 of file colors.h.

◆ ANSI_COLOR_MAGENTA

#define ANSI_COLOR_MAGENTA   "\x1b[1;35m"

Definition at line 32 of file colors.h.

◆ ANSI_COLOR_RED

#define ANSI_COLOR_RED   "\x1b[1;31m"

Definition at line 27 of file colors.h.

◆ ANSI_COLOR_RESET

#define ANSI_COLOR_RESET   "\x1b[1;0m"

Definition at line 34 of file colors.h.

◆ ANSI_COLOR_SOFT_RED

#define ANSI_COLOR_SOFT_RED   "\x1b[1;38;5;210m"

Definition at line 28 of file colors.h.

◆ ANSI_COLOR_YELLOW

#define ANSI_COLOR_YELLOW   "\x1b[1;33m"

Definition at line 30 of file colors.h.

Enumeration Type Documentation

◆ COLORS

enum COLORS

allows short-handed references to ANSI color schemes, and enables easier color selection anytime you want to extend the available colors with an additional enum, add a switch case in get_ansi_color_scheme

Enumerator
COLORS_RED 
COLORS_SOFT_RED 
COLORS_GREEN 
COLORS_YELLOW 
COLORS_BLUE 
COLORS_MAGENTA 
COLORS_CYAN 
COLORS_RESET 

Definition at line 43 of file colors.h.

Function Documentation

◆ format_colored()

char* format_colored ( COLORS  color,
char *  message 
)

prefixes a message with the given ANSI color code

Returns
Success: char pointer containing the message prefixed with the ANSI color code
Failure: NULL pointer
Note
you must free up the allocate memory for the returned vlaue

Definition at line 56 of file colors.c.

Here is the call graph for this function:

◆ get_ansi_color_scheme()

char* get_ansi_color_scheme ( COLORS  color)

returns an ansi color string to be used with printf

Definition at line 26 of file colors.c.

Here is the caller graph for this function:

◆ print_colored()

void print_colored ( COLORS  color,
char *  message 
)

prints message to stdout with the given color

Definition at line 77 of file colors.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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

Returns
Success: 0
Failure: 1

Definition at line 86 of file colors.c.

Here is the call graph for this function: