M5Unified
Log_Class.cpp
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 #include "Log_Class.hpp"
5 
6 #if defined ( M5UNIFIED_PC_BUILD )
7 #include <iostream>
8 static constexpr const uint8_t log_colors_serial[] = { 98, 91, 93, 92, 96, 97, };
9 #else
10 static constexpr const uint8_t log_colors_serial[] = { 38, 31, 33, 32, 36, 37, };
11 #endif
12 static constexpr const uint8_t log_colors_display[] = { 0xFF, 0xE0, 0xFC, 0x18, 0x1F, 0x92, };
13 
14 namespace m5
15 {
16  constexpr const char Log_Class::str_crlf[3];
17 
18  const char* Log_Class::pathToFileName(const char* path)
19  {
20  size_t i = 0;
21  size_t pos = 0;
22  char * p = (char *)path;
23  while(*p)
24  {
25  i++;
26  if(*p == '/' || *p == '\\')
27  {
28  pos = i;
29  }
30  p++;
31  }
32  return path+pos;
33  }
34 
35  void Log_Class::operator() (esp_log_level_t level, const char* format, ...)
36  {
37  if (_level_maximum < level) { return; }
38  va_list args;
39  va_start(args, format);
40  output(level, true, format, args);
41  va_end(args);
42  }
43 
44  void Log_Class::printf(const char* format, ...)
45  {
46  va_list args;
47  va_start(args, format);
48  output(esp_log_level_t::ESP_LOG_NONE, false, format, args);
49  va_end(args);
50  }
51 
52  void Log_Class::update_level(void)
53  {
54  _level_maximum = std::max(std::max(_log_level[log_target_serial], _log_level[log_target_display]), _log_level[log_target_callback]);
55  }
56 
57  void Log_Class::output(esp_log_level_t level, bool suffix, const char* __restrict format, va_list arg)
58  {
59  char loc_buf[64];
60  char * str = loc_buf;
61  va_list copy;
62  va_copy(copy, arg);
63  int len = vsnprintf(str, sizeof(loc_buf), format, copy);
64  va_end(copy);
65  if (len < 0) { return; }
66  if ((size_t)len >= sizeof(loc_buf))
67  {
68  auto tmp = (char*) alloca(len + 1);
69  if (tmp)
70  {
71  str = tmp;
72  len = vsnprintf(str, len+1, format, arg);
73  }
74  }
75 
76  if (_log_level[log_target_serial] >= level)
77  {
78  if (level != ESP_LOG_NONE && _use_color[log_target_serial])
79  {
80 #if defined(M5UNIFIED_PC_BUILD)
81  ::printf("\033[0;%dm%s\033[0m", log_colors_serial[level], str);
82 #elif defined ( ARDUINO )
83  log_printf("\033[0;%dm%s\033[0m", log_colors_serial[level], str);
84 #else
85  esp_rom_printf("\033[0;%dm%s\033[0m", log_colors_serial[level], str);
86 #endif
87  }
88  else
89  {
90 #if defined(M5UNIFIED_PC_BUILD)
91  std::cout << str;
92 #elif defined ( ARDUINO )
93  log_printf(str);
94 #else
95  esp_rom_printf(str);
96 #endif
97  }
98  if (suffix && _suffix[log_target_serial])
99  {
100 #if defined(M5UNIFIED_PC_BUILD)
101  std::cout << _suffix[log_target_serial];
102 #elif defined ( ARDUINO )
103  log_printf(_suffix[log_target_serial]);
104 #else
105  esp_rom_printf(_suffix[log_target_serial]);
106 #endif
107  }
108 
109 #if defined(M5UNIFIED_PC_BUILD)
110  fflush(stdout);
111 #endif
112  }
113 
114  if (_display && _log_level[log_target_display] >= level)
115  {
116  if (level != ESP_LOG_NONE && _use_color[log_target_display])
117  {
118  auto style = _display->getTextStyle();
119  if (style.fore_rgb888 == style.back_rgb888)
120  {
121  _display->setTextColor(log_colors_display[level]);
122  }
123  else
124  {
125  _display->setTextColor(log_colors_display[level], m5gfx::color_convert<m5gfx::rgb332_t, m5gfx::rgb888_t>(style.back_rgb888));
126  }
127  _display->print(str);
128  _display->setTextStyle(style);
129  }
130  else
131  {
132  _display->print(str);
133  }
134  if (suffix && _suffix[log_target_display]) { _display->print(_suffix[log_target_display]); }
135  }
136 
137  if (_log_level[log_target_callback] >= level && _callback != nullptr)
138  {
139  _callback(level, _log_level[log_target_callback], str);
140  if (suffix && _suffix[log_target_callback]) { _callback(level, _log_level[log_target_callback], _suffix[log_target_callback]); }
141  }
142  }
143 
144  void Log_Class::setDisplay(M5GFX* target)
145  {
146  _display = target;
147  }
148 }
void setDisplay(M5GFX *target)
Definition: Log_Class.cpp:144
void operator()(esp_log_level_t level, const char *format,...)
Output log.
Definition: Log_Class.cpp:35
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
Definition: M5Unified.cpp:48
@ log_target_callback
Definition: Log_Class.hpp:44
@ log_target_display
Definition: Log_Class.hpp:43
@ log_target_serial
Definition: Log_Class.hpp:42