Change logger to use critical and fatal instead of error

This commit is contained in:
2023-11-01 13:56:56 +01:00
parent a0f5d27909
commit 0af40da311
2 changed files with 37 additions and 18 deletions

View File

@@ -39,7 +39,8 @@ To use the logging system, include `log.h` in your source file, and use the foll
LOG_DEBUG(TAG, fmt, ...)
LOG_INFO(TAG, fmt, ...)
LOG_WARN(TAG, fmt, ...)
LOG_ERROR(TAG, fmt, ...)
LOG_CRIT(TAG, fmt, ...)
LOG_FATAL(TAG, fmt, ...)
```
The same format specifiers as in `printf` can be used.
@@ -73,7 +74,13 @@ before you include `log.h`, you can define custom log levels by defining the fol
```c
// Only log messages with level ERROR will be printed
#define LOGGER_LEVEL_ERROR
#define LOGGER_LEVEL_CRITICAL
#include "log.h"
```
```c
// Only log messages with level ERROR will be printed
#define LOGGER_LEVEL_FATAL
#include "log.h"
```
@@ -84,7 +91,7 @@ Default is `0`
### Log Output Format
Each log entry is formatted to include the following information:
- Log level ([Debug], [Info], [Warn], [Error])
- Log level ([Debug], [Info], [Warn], [Critical], [Fatal])
- Timestamp (in milliseconds since boot)
- Tag
- The log message
@@ -106,7 +113,8 @@ int main(void) {
LOG_DEBUG(TAG, "This message will not be printed");
LOG_INFO(TAG, "This message will be printed");
LOG_WARN(TAG, "This message will be printed");
LOG_ERROR(TAG, "This message will be printed");
LOG_CRIT(TAG, "This message will be printed");
LOG_FATAL(TAG, "This message will be printed");
for (int i = 0; i < 3; i++) {
LOG_INFO(TAG, "Iteration %d of %d", i, 3);
@@ -117,10 +125,11 @@ int main(void) {
```
Result:
```
[Info] (2009) main: This message will be printed
[Warning] (2026) main: This message will be printed
[Error] (2033) main: This message will be printed
[Info] (2040) main: Iteration 0 of 3
[Info] (2047) main: Iteration 1 of 3
[Info] (2054) main: Iteration 2 of 3
[Info] (2009) main: This message will be printed
[Warning] (2026) main: This message will be printed
[CRITICAL] (2033) main: This message will be printed
[FATAL] (2040) main: This message will be printed
[Info] (2040) main: Iteration 0 of 3
[Info] (2047) main: Iteration 1 of 3
[Info] (2054) main: Iteration 2 of 3
```

View File

@@ -18,7 +18,10 @@
#include <sys/times.h>
#include <sys/unistd.h>
#ifdef LOGGER_LEVEL_ERROR
#if LOGGER_LEVEL_FATAL
#define LOGGER_LEVEL 5
#endif
#ifdef LOGGER_LEVEL_CRITICAL
#define LOGGER_LEVEL 4
#endif
#ifdef LOGGER_LEVEL_WARN
@@ -66,37 +69,44 @@
#if LOGGER_USE_COLOR == 1
#define LOG_RESET_COLOR "\033[0m"
#define LOG_COLOR_E "\033[0;31m" // Red
#define LOG_COLOR_F "\033[0;31m" // Red
#define LOG_COLOR_C "\033[0;31m" // Red
#define LOG_COLOR_W "\033[0;33m" // Brown
#define LOG_COLOR_I "\033[0;32m" // Green
#define LOG_COLOR_D LOG_RESET_COLOR // Default
#else
#define LOG_RESET_COLOR
#define LOG_COLOR_E
#define LOG_COLOR_F
#define LOG_COLOR_C
#define LOG_COLOR_W
#define LOG_COLOR_I
#define LOG_COLOR_D
#endif
#if LOGGER_LEVEL <= 1
#define LOG_DEBUG(tag, fmt, ...) printf(LOG_Color_D"[Debug] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#define LOG_DEBUG(tag, fmt, ...) printf(LOG_Color_D"[Debug] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#else
#define LOG_DEBUG(tag, fmt, ...)
#endif
#if LOGGER_LEVEL <= 2
#define LOG_INFO(tag, fmt, ...) printf(LOG_COLOR_I"[Info] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#define LOG_INFO(tag, fmt, ...) printf(LOG_COLOR_I"[Info] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#else
#define LOG_INFO(tag, fmt, ...)
#endif
#if LOGGER_LEVEL <= 3
#define LOG_WARN(tag, fmt, ...) printf(LOG_COLOR_W"[Warning] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#define LOG_WARN(tag, fmt, ...) printf(LOG_COLOR_W"[Warning] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#else
#define LOG_WARN(tag, fmt, ...)
#endif
#if LOGGER_LEVEL <= 4
#define LOG_ERROR(tag, fmt, ...) printf(LOG_COLOR_E"[Error] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#define LOG_CRIT(tag, fmt, ...) printf(LOG_COLOR_C"[Critical] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#else
#define LOG_ERROR(tag, fmt, ...)
#define LOG_CRIT(tag, fmt, ...)
#endif
#if LOGGER_LEVEL <= 4
#define LOG_FATAL(tag, fmt, ...) printf(LOG_COLOR_F"[Fatal] (%lu) [%s]: "fmt LOG_RESET_COLOR, logger_get_timestamp(), tag, ##__VA_ARGS__)
#else
#define LOG_FATAL(tag, fmt, ...)
#endif
/* For internal use only.