IoTeX client
client_helper.h
1#ifndef CLIENT_HELPERS_H
2#define CLIENT_HELPERS_H
3
4#include "extern/cpplogger/cpplogger.h"
5#include "IoTeXResultCodes.h"
6#include <cstring>
7#include <string>
8#include <vector>
9
10// clang-format off
11
12/**************************************************************************/
13/* Log level macros */
14/**************************************************************************/
15
16#define IOTEX_LOG_LEVEL_TRACE cpplogger::LogLevel::TRACE
17#define IOTEX_LOG_LEVEL_DEBUG cpplogger::LogLevel::DEBUG
18#define IOTEX_LOG_LEVEL_INFO cpplogger::LogLevel::INFO
19#define IOTEX_LOG_LEVEL_WARNING cpplogger::LogLevel::WARNING
20#define IOTEX_LOG_LEVEL_ERROR cpplogger::LogLevel::ERROR
21#define IOTEX_LOG_LEVEL_NONE cpplogger::LogLevel::NONE
22
23#define IOTEX_DEFAULT_LOG_LEVEL IOTEX_LOG_LEVEL_NONE
24
25/**************************************************************************/
26/* Set default log level */
27/**************************************************************************/
28
29#ifndef IOTEX_LOG_LEVEL
30 #define IOTEX_LOG_LEVEL IOTEX_DEFAULT_LOG_LEVEL
31#endif
32
33/**************************************************************************/
34/* Conditinally include board specific files and add board specific flags */
35/**************************************************************************/
36
37#if defined(ARDUINO) || defined(ESP8266) || defined(ESP32) || defined(__SAMD21G18A__)
38 #include <Arduino.h>
39 #define IotexString String // Use Arduino String intead of std::string in Arduino
40#else
41 #define IotexString std::string
42 #ifndef OS
43 #define OS
44 #endif
45#endif
46
47/**************************************************************************/
48/* Enable log by default */
49/**************************************************************************/
50
51#ifndef IOTEX_DISABLE_LOG
52 #define IOTEX_ENABLE_LOG
53#endif
54
55/**************************************************************************/
56/* Remove log statements from compiled binary if IOTEX_ENABLE_LOG not defined */
57/**************************************************************************/
58
59#ifndef IOTEX_ENABLE_LOG
60 #define IOTEX_DEBUG(module, ...)
61 #define IOTEX_INFO(module, ...)
62 #define IOTEX_WARNING(module, ...)
63 #define IOTEX_TRACE(module, ...)
64 #define IOTEX_ERROR(module, ...)
65 #define IOTEX_DEBUG_F(module, ...)
66 #define IOTEX_INFO_F(module, ...)
67 #define IOTEX_WARNING_F(module, ...)
68 #define IOTEX_TRACE_F(module, ...)
69 #define IOTEX_ERROR_F(module, ...)
70 #define IOTEX_DEBUG_BUF(module, ...)
71 #define IOTEX_INFO_BUF(module, ...)
72 #define IOTEX_WARNING_BUF(module, ...)
73 #define IOTEX_TRACE_BUF(module, ...)
74 #define IOTEX_ERROR_BUF(module, ...)
75#endif
76
77/**************************************************************************/
78/* Helper macros for logging a message with different levels */
79/**************************************************************************/
80
81#ifdef IOTEX_ENABLE_LOG
82 #define IOTEX_DEBUG(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::DEBUG, module, __VA_ARGS__)
83 #define IOTEX_INFO(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::INFO, module, __VA_ARGS__)
84 #define IOTEX_WARNING(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::WARNING, module, __VA_ARGS__)
85 #define IOTEX_TRACE(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::TRACE, module, __VA_ARGS__)
86 #define IOTEX_ERROR(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::ERROR, module, __VA_ARGS__)
87#endif
88
89/**************************************************************************/
90/* Helper macros for logging a hex dump of a buffer with different levels */
91/**************************************************************************/
92
93#ifdef IOTEX_ENABLE_LOG
94 #define IOTEX_DEBUG_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::DEBUG, module, buf, size)
95 #define IOTEX_INFO_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::INFO, module, buf, size)
96 #define IOTEX_WARNING_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::WARNING, module, buf, size)
97 #define IOTEX_TRACE_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::TRACE, module, buf, size)
98 #define IOTEX_ERROR_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::ERROR, module, buf, size)
99#endif
100
101/**************************************************************************/
102/* Helper macros for logging a flash string with different levels (for ESP) */
103/**************************************************************************/
104
105#ifdef IOTEX_ENABLE_LOG
106 #if !defined(ESP8266) && !defined(ESP32)
107 #define IOTEX_FLASH_STRING_SUPPORT false
108 #else
109 #define IOTEX_FLASH_STRING_SUPPORT true
110 #endif
111
112 #if !IOTEX_FLASH_STRING_SUPPORT
113 #define IOTEX_DEBUG_F(module, ...) IOTEX_DEBUG(module, __VA_ARGS__)
114 #define IOTEX_INFO_F(module, ...) IOTEX_INFO(module, __VA_ARGS__)
115 #define IOTEX_WARNING_F(module, ...) IOTEX_WARNING(module, __VA_ARGS__)
116 #define IOTEX_TRACE_F(module, ...) IOTEX_TRACE(module, __VA_ARGS__)
117 #define IOTEX_ERROR_F(module, ...) IOTEX_ERROR(module, __VA_ARGS__)
118 #else
119 #define IOTEX_DEBUG_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
120 #define IOTEX_INFO_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
121 #define IOTEX_WARNING_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
122 #define IOTEX_TRACE_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
123 #define IOTEX_ERROR_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
124 #endif
125#endif
126
127// clang-format on
128
129namespace iotex
130{
136{
137 USER,
138 GENERAL,
139 HTTP,
140 CONTRACT,
141 VALUES_COUNT
142};
143
149extern const std::string& userLogModule;
150
155extern const std::string logModuleNamesLookupTable[(int)LogModules::VALUES_COUNT];
156
162{
163 NONE,
164 ERROR,
165 WARNING,
166 INFO,
167 DEBUG,
168 TRACE
169};
170
176{
177 public:
178 Helpers();
179 cpplogger::Logger logger;
180
187 void vectorToHexString(std::vector<uint8_t>& data, IotexString& out);
188
195 const char* GetResultString(ResultCode code);
196
203
211 void setModuleLogLevel(const std::string& module, IotexLogLevel level);
212
219 void endianSwap(uint8_t* pData, uint64_t size);
220};
221} // namespace iotex
222
227extern iotex::Helpers IotexHelpers;
228
229#endif
Class that contains helper methods.
Definition: client_helper.h:176
void vectorToHexString(std::vector< uint8_t > &data, IotexString &out)
Gets the hex string representation of a vector of bytes.
Definition: client_helper.cpp:55
void setModuleLogLevel(const std::string &module, IotexLogLevel level)
Set the Mlog level for a module. Note that the global lov level can override a module log level.
Definition: client_helper.cpp:76
const char * GetResultString(ResultCode code)
Gets the result strinf value of a ResultCode.
Definition: client_helper.cpp:66
void endianSwap(uint8_t *pData, uint64_t size)
Swaps the endianness of a byte array.
Definition: client_helper.cpp:81
void setGlobalLogLevel(IotexLogLevel level)
Sets the log level globally. Overrides log level modules if they are of a higher level.
Definition: client_helper.cpp:71
Definition: abi.h:12
const std::string & userLogModule
General log module name exposed to the user, so the logger can be used by user code if needed.
Definition: client_helper.cpp:32
IotexLogLevel
Enum representing all the log levels.
Definition: client_helper.h:162
LogModules
Enum that contains the different logging modules of the library.
Definition: client_helper.h:136
const std::string logModuleNamesLookupTable[(int) LogModules::VALUES_COUNT]
Lookup table for log module names. This should be only used by the internal library code.
Definition: client_helper.cpp:22