Shrink the logger

* Move _write function to the log.c
* Change the way to setup the logger
* Update the readme
* Add terminal commands
This commit is contained in:
2023-10-31 21:14:02 +01:00
parent c9df2dee99
commit 11eba9dd84
4 changed files with 130 additions and 181 deletions

View File

@@ -52,38 +52,56 @@ You can control the verbosity of the logging output by setting a global log leve
This log level filters out messages with a lower priority.
### Custom Log Levels
Additionally, you have the flexibility to override the log level for individual tags (but not lower than the global log level).
This can be archived by calling the following function:
before you include `log.h`, you can define custom log levels by defining the following macros:
```c
logger_set_log_level(LOG_TAG, LOG_LEVEL);
// All log messages will be printed
#define LOGGER_LEVEL_ALL
#include "log.h"
```
```c
// Info and higher priority messages will be printed
#define LOGGER_LEVEL_INFO
#include "log.h"
```
```c
// Only warnings and errors will be printed
#define LOGGER_LEVEL_WARN
#include "log.h"
```
```c
// Only log messages with level ERROR will be printed
#define LOGGER_LEVEL_ERROR
#include "log.h"
```
If the log level for a tag is not set, the global log level is used.
### Colorful Log Messages
For improved readability, log messages can be printed in color by configuring `LOGGER_USE_COLOR` to `1` in `log.h`.
For improved readability, log messages can be printed in color by configuring `LOGGER_USE_COLOR` to `1` in `log.h` or before you include `log.h`. \
Default is `0`
### Log Output Format
Each log entry is formatted to include the following information:
- Log level (D for Debug, I for Info, W for Warning, E for Error)
- Log level ([Debug], [Info], [Warn], [Error])
- Timestamp (in milliseconds since boot)
- Tag
- The log message
For instance, a log entry may look like this:
`I (2009) LTDC: This is a log message`
`[Info] (2009) [LTDC]: This is a log message`
### Example
```c
#define LOGGER_LEVEL_INFO
#include "log.h"
// Don't use a define for the tag, as the pointer to the tag is used
static const char *TAG = "main";
int main(void) {
// Set log level for tag "main" to LOG_LEVEL_INFO
logger_set_log_level(TAG, LOG_LEVEL_INFO);
LOG_DEBUG(TAG, "This message will not be printed");
LOG_INFO(TAG, "This message will be printed");
@@ -99,10 +117,10 @@ int main(void) {
```
Result:
```
I (2009) main: This message will be printed
W (2026) main: This message will be printed
E (2033) main: This message will be printed
I (2040) main: Iteration 0 of 3
I (2047) main: Iteration 1 of 3
I (2054) main: Iteration 2 of 3
[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
```