M5Unified
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Log_Class.hpp
Go to the documentation of this file.
1 // Copyright (c) M5Stack. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 #ifndef __M5_Log_Class_H__
5 #define __M5_Log_Class_H__
6 
7 #include "m5unified_common.h"
8 
9 #if !defined ( M5UNIFIED_PC_BUILD )
10 #include <esp_log.h>
11 #endif
12 
13 #include <stdarg.h>
14 #include <functional>
15 
16 #include <M5GFX.h>
17 
19 #ifndef M5UNIFIED_LOG_FORMAT
20 #define M5UNIFIED_LOG_FORMAT(letter, format) "[%6u][" #letter "][%s:%u] %s(): " format, m5gfx::millis(), m5::Log_Class::pathToFileName(__FILE__), __LINE__, __FUNCTION__
21 #endif
22 
24 #define M5_LOGE(format, ...) M5.Log(ESP_LOG_ERROR , M5UNIFIED_LOG_FORMAT(E, format), ##__VA_ARGS__)
25 
27 #define M5_LOGW(format, ...) M5.Log(ESP_LOG_WARN , M5UNIFIED_LOG_FORMAT(W, format), ##__VA_ARGS__)
28 
30 #define M5_LOGI(format, ...) M5.Log(ESP_LOG_INFO , M5UNIFIED_LOG_FORMAT(I, format), ##__VA_ARGS__)
31 
33 #define M5_LOGD(format, ...) M5.Log(ESP_LOG_DEBUG , M5UNIFIED_LOG_FORMAT(D, format), ##__VA_ARGS__)
34 
36 #define M5_LOGV(format, ...) M5.Log(ESP_LOG_VERBOSE, M5UNIFIED_LOG_FORMAT(V, format), ##__VA_ARGS__)
37 
38 namespace m5
39 {
40  enum log_target_t : uint8_t
41  {
46  };
47 
48  class Log_Class
49  {
50  public:
52  void operator() (esp_log_level_t level, const char* format, ...);
53 
56  void printf(const char* format, ...);
57 
60  void print(const char* string) { return printf("%s", string); }
61 
64  void println(const char* string) { return printf("%s\n", string); }
65 
68  void println(void) { return printf(str_crlf); }
69 
71  void setEnableColor(log_target_t target, bool enable) { if (target < log_target_max) { _use_color[target] = enable; } }
72 
74  bool getEnableColor(log_target_t target) const { return _use_color[target]; }
75 
77  void setLogLevel(log_target_t target, esp_log_level_t level) { if (target < log_target_max) { _log_level[target] = level; update_level(); } }
78 
80  esp_log_level_t getLogLevel(log_target_t target) const { return _log_level[target]; }
81 
83  void setSuffix(log_target_t target, const char* suffix) { if (target < log_target_max) { _suffix[target] = suffix; } }
84 
87  void setCallback(std::function<void(esp_log_level_t log_level, bool use_color, const char* log_text)> function) { _callback = function; };
88 
91  void setDisplay(M5GFX* target);
92 
95  void setDisplay(M5GFX& target) { setDisplay(&target); }
96 
98  static const char* pathToFileName(const char * path);
99 
100  private:
101  M5GFX* _display = nullptr;
102 
103  static constexpr const char str_crlf[3] = "\r\n";
104  static constexpr const char *str_lf = &str_crlf[1];
105 
106  void output(esp_log_level_t level, bool suffix, const char* __restrict format, va_list arg);
107  void update_level(void);
108 
109  std::function<void(esp_log_level_t log_level, bool use_color, const char* log_text)> _callback;
110 
111 #if defined ( CORE_DEBUG_LEVEL )
112  esp_log_level_t _level_maximum = (esp_log_level_t)CORE_DEBUG_LEVEL;
113 #elif defined ( CONFIG_LOG_DEFAULT_LEVEL )
114  esp_log_level_t _level_maximum = (esp_log_level_t)CONFIG_LOG_DEFAULT_LEVEL;
115 #else
116  esp_log_level_t _level_maximum = esp_log_level_t::ESP_LOG_VERBOSE;
117 #endif
118  esp_log_level_t _log_level[log_target_max] = { _level_maximum, _level_maximum, _level_maximum };
119 
120  const char* _suffix[log_target_max] = { str_lf, str_lf, str_crlf };
121 
122  bool _use_color[log_target_max] = { true, true, true };
123  };
124 }
125 #endif
void setDisplay(M5GFX *target)
Definition: Log_Class.cpp:144
void setLogLevel(log_target_t target, esp_log_level_t level)
Set log level.
Definition: Log_Class.hpp:77
void println(void)
Definition: Log_Class.hpp:68
esp_log_level_t getLogLevel(log_target_t target) const
Get log level for serial output.
Definition: Log_Class.hpp:80
void println(const char *string)
Definition: Log_Class.hpp:64
void setEnableColor(log_target_t target, bool enable)
Set whether or not to change the color for each log level.
Definition: Log_Class.hpp:71
bool getEnableColor(log_target_t target) const
Get whether or not to change the color for each log level.
Definition: Log_Class.hpp:74
void print(const char *string)
Definition: Log_Class.hpp:60
void operator()(esp_log_level_t level, const char *format,...)
Output log.
Definition: Log_Class.cpp:35
void setSuffix(log_target_t target, const char *suffix)
Set the text to be added to the end of the log.
Definition: Log_Class.hpp:83
static const char * pathToFileName(const char *path)
not for use.
Definition: Log_Class.cpp:18
void printf(const char *format,...)
Definition: Log_Class.cpp:44
void setDisplay(M5GFX &target)
Definition: Log_Class.hpp:95
void setCallback(std::function< void(esp_log_level_t log_level, bool use_color, const char *log_text)> function)
Definition: Log_Class.hpp:87
Definition: M5Unified.cpp:48
log_target_t
Definition: Log_Class.hpp:41
@ log_target_max
Definition: Log_Class.hpp:45
@ log_target_callback
Definition: Log_Class.hpp:44
@ log_target_display
Definition: Log_Class.hpp:43
@ log_target_serial
Definition: Log_Class.hpp:42