IoTeX client
client_helper 2.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/* Set default log level */
14/**************************************************************************/
15
16#define IOTEX_DEFAULT_LOG_LEVEL cpplogger::LogLevel::TRACE
17#ifndef IOTEX_LOG_LEVEL
18 #define IOTEX_LOG_LEVEL IOTEX_DEFAULT_LOG_LEVEL
19#endif
20
21/**************************************************************************/
22/* Conditinally include board specific files and add board specific flags */
23/**************************************************************************/
24
25#if defined(ARDUINO) || defined(ESP8266) || defined(ESP32) || defined(__SAMD21G18A__)
26 #include <Arduino.h>
27 #define IotexString String // Use Arduino String intead of std::string in Arduino
28#else
29 #define IotexString std::string
30 #ifndef OS
31 #define OS
32 #endif
33#endif
34
35/**************************************************************************/
36/* Helper macros for logging a message with different levels */
37/**************************************************************************/
38#define IOTEX_DEBUG(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::DEBUG, module, __VA_ARGS__)
39#define IOTEX_INFO(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::INFO, module, __VA_ARGS__)
40#define IOTEX_WARNING(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::WARNING, module, __VA_ARGS__)
41#define IOTEX_TRACE(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::TRACE, module, __VA_ARGS__)
42#define IOTEX_ERROR(module, ...) IotexHelpers.logger.LOG_MSG(cpplogger::LogLevel::ERROR, module, __VA_ARGS__)
43
44/**************************************************************************/
45/* Helper macros for logging a hex dump of a buffer with different levels */
46/**************************************************************************/
47
48#define IOTEX_DEBUG_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::DEBUG, module, buf, size)
49#define IOTEX_INFO_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::INFO, module, buf, size)
50#define IOTEX_WARNING_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::WARNING, module, buf, size)
51#define IOTEX_TRACE_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::TRACE, module, buf, size)
52#define IOTEX_ERROR_BUF(module, buf, size) IotexHelpers.logger.LOG_BUF(cpplogger::LogLevel::ERROR, module, buf, size)
53
54/**************************************************************************/
55/* Helper macros for logging a flash string with different levels (for ESP) */
56/**************************************************************************/
57
58#if !defined(ESP8266) && !defined(ESP32)
59 #define IOTEX_FLASH_STRING_SUPPORT false
60#else
61 #define IOTEX_FLASH_STRING_SUPPORT true
62#endif
63
64#if !IOTEX_FLASH_STRING_SUPPORT
65 #define IOTEX_DEBUG_F(module, ...) IOTEX_DEBUG(module, __VA_ARGS__)
66 #define IOTEX_INFO_F(module, ...) IOTEX_INFO(module, __VA_ARGS__)
67 #define IOTEX_WARNING_F(module, ...) IOTEX_WARNING(module, __VA_ARGS__)
68 #define IOTEX_TRACE_F(module, ...) IOTEX_TRACE(module, __VA_ARGS__)
69 #define IOTEX_ERROR_F(module, ...) IOTEX_ERROR(module, __VA_ARGS__)
70#else
71 #define IOTEX_DEBUG_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
72 #define IOTEX_INFO_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
73 #define IOTEX_WARNING_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
74 #define IOTEX_TRACE_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
75 #define IOTEX_ERROR_F(module, msg) IotexHelpers.logger.LOG_PROGMEM_STRING(cpplogger::LogLevel::DEBUG, module, msg)
76#endif
77// clang-format on
78
79namespace iotex
80{
86{
87 GENERAL,
88 HTTP,
89 CONTRACT,
90 VALES_COUNT
91};
92
98extern const std::string& generalLogModule;
99
104extern const std::string logModuleNamesLookupTable[(int)LogModules::VALES_COUNT];
105
111{
112 NONE,
113 ERROR,
114 WARNING,
115 INFO,
116 DEBUG,
117 TRACE
118};
119
125{
126 public:
127 Helpers();
128 cpplogger::Logger logger;
129
136 void vectorToHexString(std::vector<uint8_t>& data, IotexString& out);
137
144 const char* GetResultString(ResultCode code);
145
152
160 void setModuleLogLevel(const std::string& module, IotexLogLevel level);
161};
162} // namespace iotex
163
168extern iotex::Helpers IotexHelpers;
169
170#endif
Class that contains helper methods.
Definition: client_helper 2.h:125
void vectorToHexString(std::vector< uint8_t > &data, IotexString &out)
Gets the hex string representation of a vector of bytes.
Definition: client_helper 2.cpp:52
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 2.cpp:73
const char * GetResultString(ResultCode code)
Gets the result strinf value of a ResultCode.
Definition: client_helper 2.cpp:63
void setGlobalLogLevel(IotexLogLevel level)
Sets the log level globally. Overrides log level modules if they are of a higher level.
Definition: client_helper 2.cpp:68
Definition: abi.h:12
const std::string logModuleNamesLookupTable[(int) LogModules::VALES_COUNT]
Lookup table for log module names. This should be only used by the internal library code.
Definition: client_helper.h:104
IotexLogLevel
Enum representing all the log levels.
Definition: client_helper 2.h:111
LogModules
Enum that contains the different logging modules of the library.
Definition: client_helper 2.h:86
const std::string & generalLogModule
General log module name exposed to the user, so the logger can be used by user code if needed.
Definition: client_helper.h:98