mitteLib
Loading...
Searching...
No Matches
log.hpp
Go to the documentation of this file.
1//
2// Created by spak on 03/09/24.
3//
4
5#ifndef LOG_HPP
6#define LOG_HPP
7
8#ifdef ESP_PLATFORM
9
10#include <esp_log.h>
11
12#define LOGE ESP_LOGE
13#define LOGW ESP_LOGW
14#define LOGI ESP_LOGI
15#define LOGD ESP_LOGD
16#define LOGV ESP_LOGV
17
18#else
19
20#include <chrono>
21#include <cstdio>
22
23namespace mlab {
24 [[nodiscard]] std::uint32_t log_timestamp();
25
26 enum struct log_level : int {
27 none,
28 error,
29 warn,
30 info,
31 debug,
33 };
34}// namespace mlab
35
36#define MLAB_LOG_COLOR_E "\033[0;31m"
37#define MLAB_LOG_COLOR_W "\033[0;33m"
38#define MLAB_LOG_COLOR_I "\033[0;32m"
39#define MLAB_LOG_COLOR_D
40#define MLAB_LOG_COLOR_V
41#define MLAB_LOG_RESET_COLOR "\033[0m"
42#define MLAB_LOG_FORMAT(letter, format) MLAB_LOG_COLOR_##letter #letter " (%u) %s: " format MLAB_LOG_RESET_COLOR "\n"
43
44#ifndef LOG_LOCAL_LEVEL
45#define LOG_LOCAL_LEVEL mlab::log_level::info
46#endif
47
48
49#define LOG_LEVEL(level, tag, format, letter, ...) \
50 do { \
51 if (static_cast<int>(LOG_LOCAL_LEVEL) >= static_cast<int>(level)) \
52 std::printf((MLAB_LOG_FORMAT(letter, format)), ::mlab::log_timestamp(), tag, ##__VA_ARGS__); \
53 } while (false)
54
55#define LOGE(tag, format, ...) LOG_LEVEL(mlab::log_level::error, tag, format, E, ##__VA_ARGS__)
56#define LOGW(tag, format, ...) LOG_LEVEL(mlab::log_level::warn, tag, format, W, ##__VA_ARGS__)
57#define LOGI(tag, format, ...) LOG_LEVEL(mlab::log_level::info, tag, format, I, ##__VA_ARGS__)
58#define LOGD(tag, format, ...) LOG_LEVEL(mlab::log_level::debug, tag, format, D, ##__VA_ARGS__)
59#define LOGV(tag, format, ...) LOG_LEVEL(mlab::log_level::verbose, tag, format, V, ##__VA_ARGS__)
60
61#endif
62
63#endif//LOG_HPP
Definition log.cpp:8
std::uint32_t log_timestamp()
Definition log.cpp:11
log_level
Definition log.hpp:26