-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce colored fatal error messages
Introduces the fatal() function that 1) Check whether stderr is a terminal, and sets red text color if it is 2) Prints the message 3) Exits the app
- Loading branch information
Showing
3 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
# generated from MANPAGE.md | ||
/earlyoom.1 | ||
/earlyoom.1.gz | ||
|
||
# generated service files | ||
/earlyoom.service | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <stdlib.h> | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
|
||
// Print message to stderr and exit with "code". | ||
// Example: fatal(6, "could not compile regexp '%s'\n", regex_str); | ||
void fatal(int code, char* fmt, ...) | ||
{ | ||
char* red = ""; | ||
char* reset = ""; | ||
if (isatty(fileno(stderr))) { | ||
red = "\033[31m"; | ||
reset = "\033[0m"; | ||
} | ||
char fmt2[100]; | ||
snprintf(fmt2, sizeof(fmt2), "%sfatal: %s%s", red, fmt, reset); | ||
va_list args; | ||
va_start(args, fmt); // yes fmt, NOT fmt2! | ||
vfprintf(stderr, fmt2, args); | ||
va_end(args); | ||
exit(code); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* SPDX-License-Identifier: MIT */ | ||
#ifndef MSG_H | ||
#define MSG_H | ||
|
||
void fatal(int code, char* fmt, ...); | ||
|
||
#endif |