-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation for ESP-IDF is untested.
- Loading branch information
Showing
3 changed files
with
175 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @file logger.hpp | ||
* @author Dávid Benko ([email protected]) | ||
* @brief Logger for Kvik | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
namespace kvik | ||
{ | ||
enum class LogLevel : uint_fast8_t | ||
{ | ||
DEBUG = 0, | ||
INFO = 1, | ||
WARN = 2, | ||
ERROR = 3, | ||
OFF = 255, | ||
}; | ||
|
||
/** | ||
* @brief Global log level | ||
* | ||
* On ESP-IDF it's ignored (logging is handled by standard ESP-IDF logger). | ||
*/ | ||
extern LogLevel logLevel; | ||
|
||
/** | ||
* @brief Logging handler function | ||
* @param msgLevel Message log level | ||
* @param logTag Logging tag of this file | ||
* @param fmt Formatting string | ||
* @param ... Variadic arguments in `printf`-like style | ||
*/ | ||
void logFunc(LogLevel msgLevel, const char *logTag, const char *fmt, ...); | ||
|
||
#define KVIK_LOGD(fmt, ...) logFunc(LogLevel::DEBUG, KVIK_LOG_TAG, fmt, ##__VA_ARGS__) | ||
#define KVIK_LOGI(fmt, ...) logFunc(LogLevel::INFO, KVIK_LOG_TAG, fmt, ##__VA_ARGS__) | ||
#define KVIK_LOGW(fmt, ...) logFunc(LogLevel::WARN, KVIK_LOG_TAG, fmt, ##__VA_ARGS__) | ||
#define KVIK_LOGE(fmt, ...) logFunc(LogLevel::ERROR, KVIK_LOG_TAG, fmt, ##__VA_ARGS__) | ||
} // namespace kvik |
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,51 @@ | ||
/** | ||
* @file logger.cpp | ||
* @author Dávid Benko ([email protected]) | ||
* @brief Logger for Kvik | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <cstdarg> | ||
#include <cstdio> | ||
|
||
#include "esp_log.h" | ||
|
||
#include "kvik/logger.hpp" | ||
|
||
namespace kvik | ||
{ | ||
// Kvik's local log level is ignored | ||
// Instead, use standard ESP-IDF log configuration | ||
LogLevel logLevel = LogLevel::DEBUG; | ||
|
||
void logFunc(LogLevel msgLevel, const char *logTag, const char *fmt, ...) | ||
{ | ||
esp_log_level_t espMsgLevel; | ||
|
||
switch (msgLevel) | ||
{ | ||
case LogLevel::DEBUG: | ||
espMsgLevel = ESP_LOG_DEBUG; | ||
break; | ||
case LogLevel::INFO: | ||
espMsgLevel = ESP_LOG_INFO; | ||
break; | ||
case LogLevel::WARN: | ||
espMsgLevel = ESP_LOG_WARN; | ||
break; | ||
case LogLevel::ERROR: | ||
espMsgLevel = ESP_LOG_ERROR; | ||
break; | ||
default: | ||
espMsgLevel = ESP_LOG_NONE; | ||
break; | ||
} | ||
|
||
va_list args; | ||
va_start(args, fmt); | ||
esp_log_writev(espMsgLevel, logTag, fmt, args); | ||
va_end(args); | ||
} | ||
} // namespace kvik |
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,79 @@ | ||
/** | ||
* @file logger.cpp | ||
* @author Dávid Benko ([email protected]) | ||
* @brief Logger for Kvik | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <cstdarg> | ||
#include <cstdio> | ||
|
||
#include "kvik/logger.hpp" | ||
|
||
namespace kvik | ||
{ | ||
// Set default log level | ||
LogLevel logLevel = LogLevel::INFO; | ||
|
||
void logFunc(LogLevel msgLevel, const char *logTag, const char *fmt, ...) | ||
{ | ||
if (logLevel > msgLevel) | ||
{ | ||
// Skip too verbose messages | ||
return; | ||
} | ||
|
||
#if KVIK_LOG_NO_COLORS | ||
switch (msgLevel) | ||
{ | ||
case LogLevel::DEBUG: | ||
fprintf(stderr, "[D] %s: ", logTag); | ||
break; | ||
case LogLevel::INFO: | ||
fprintf(stderr, "[I] %s: ", logTag); | ||
break; | ||
case LogLevel::WARN: | ||
fprintf(stderr, "[W] %s: ", logTag); | ||
break; | ||
case LogLevel::ERROR: | ||
fprintf(stderr, "[E] %s: ", logTag); | ||
break; | ||
default: | ||
fprintf(stderr, "[?] %s: ", logTag); | ||
break; | ||
} | ||
#else | ||
switch (msgLevel) | ||
{ | ||
case LogLevel::DEBUG: | ||
fprintf(stderr, "\033[0;34m[D] %s: ", logTag); | ||
break; | ||
case LogLevel::INFO: | ||
fprintf(stderr, "\033[0;36m[I] %s: ", logTag); | ||
break; | ||
case LogLevel::WARN: | ||
fprintf(stderr, "\033[0;33m[W] %s: ", logTag); | ||
break; | ||
case LogLevel::ERROR: | ||
fprintf(stderr, "\033[0;31m[E] %s: ", logTag); | ||
break; | ||
default: | ||
fprintf(stderr, "\033[0m[?] %s: ", logTag); | ||
break; | ||
} | ||
#endif | ||
|
||
va_list args; | ||
va_start(args, fmt); | ||
vfprintf(stderr, fmt, args); | ||
va_end(args); | ||
|
||
#if KVIK_LOG_NO_COLORS | ||
fprintf(stderr, "\n"); | ||
#else | ||
fprintf(stderr, "\033[0m\n"); | ||
#endif | ||
} | ||
} // namespace kvik |