1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
3 |
|
---|
4 | #ifndef XLOGGING_H
|
---|
5 | #define XLOGGING_H
|
---|
6 |
|
---|
7 | #ifdef __cplusplus
|
---|
8 | #include <cstdlib>
|
---|
9 | #else
|
---|
10 | #include <stdlib.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "azure_macro_utils/macro_utils.h"
|
---|
14 |
|
---|
15 | #include "azure_c_shared_utility/agenttime.h"
|
---|
16 | #include "azure_c_shared_utility/optimize_size.h"
|
---|
17 |
|
---|
18 | #ifdef LOGERROR_CAPTURES_STACK_TRACES
|
---|
19 | #include "azure_c_shared_utility/logging_stacktrace.h"
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #if defined(ESP8266_RTOS)
|
---|
23 | #include "c_types.h"
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #if defined(ARDUINO_ARCH_ESP8266)
|
---|
27 | #include "esp8266/azcpgmspace.h"
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #ifdef __cplusplus
|
---|
31 | /* Some compilers do not want to play by the standard, specifically ARM CC */
|
---|
32 | #ifdef MBED_BUILD_TIMESTAMP
|
---|
33 | #include <stdio.h>
|
---|
34 | #else
|
---|
35 | #include <cstdio>
|
---|
36 | #endif
|
---|
37 | #else
|
---|
38 | #include <stdio.h>
|
---|
39 | #endif /* __cplusplus */
|
---|
40 |
|
---|
41 | #ifdef __cplusplus
|
---|
42 | extern "C" {
|
---|
43 | #endif /* __cplusplus */
|
---|
44 |
|
---|
45 | #ifdef TIZENRT
|
---|
46 | #undef LOG_INFO
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | typedef enum LOG_CATEGORY_TAG
|
---|
50 | {
|
---|
51 | AZ_LOG_ERROR,
|
---|
52 | AZ_LOG_INFO,
|
---|
53 | AZ_LOG_TRACE
|
---|
54 | } LOG_CATEGORY;
|
---|
55 |
|
---|
56 | #if defined _MSC_VER
|
---|
57 | #define FUNC_NAME __FUNCDNAME__
|
---|
58 | #else
|
---|
59 | #define FUNC_NAME __func__
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | typedef void(*LOGGER_LOG)(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...);
|
---|
63 | typedef void(*LOGGER_LOG_GETLASTERROR)(const char* file, const char* func, int line, const char* format, ...);
|
---|
64 |
|
---|
65 | #define TEMP_BUFFER_SIZE 1024
|
---|
66 | #define MESSAGE_BUFFER_SIZE 260
|
---|
67 |
|
---|
68 | #define LOG_NONE 0x00
|
---|
69 | #define LOG_LINE 0x01
|
---|
70 |
|
---|
71 | /*no logging is useful when time and fprintf are mocked*/
|
---|
72 | #ifdef NO_LOGGING
|
---|
73 | #define LOG(...)
|
---|
74 | #define LogInfo(...)
|
---|
75 | #define LogBinary(...)
|
---|
76 | #define LogError(...)
|
---|
77 | #define LogLastError(...)
|
---|
78 | #define xlogging_get_log_function() NULL
|
---|
79 | #define xlogging_set_log_function(...)
|
---|
80 | #define LogErrorWinHTTPWithGetLastErrorAsString(...)
|
---|
81 | #define UNUSED(x) (void)(x)
|
---|
82 | #elif (defined MINIMAL_LOGERROR)
|
---|
83 | #define LOG(...)
|
---|
84 | #define LogInfo(...)
|
---|
85 | #define LogBinary(...)
|
---|
86 | #define LogError(...) printf("error %s: line %d\n",__FILE__,__LINE__);
|
---|
87 | #define xlogging_get_log_function() NULL
|
---|
88 | #define xlogging_set_log_function(...)
|
---|
89 | #define LogErrorWinHTTPWithGetLastErrorAsString(...)
|
---|
90 | #define UNUSED(x) (void)(x)
|
---|
91 |
|
---|
92 | #elif defined(ESP8266_RTOS)
|
---|
93 | #define LogInfo(FORMAT, ...) do { \
|
---|
94 | static const char flash_str[] ICACHE_RODATA_ATTR STORE_ATTR = FORMAT; \
|
---|
95 | printf(flash_str, ##__VA_ARGS__); \
|
---|
96 | printf("\n");\
|
---|
97 | } while((void)0,0)
|
---|
98 |
|
---|
99 | #define LogError LogInfo
|
---|
100 | #define LOG(log_category, log_options, FORMAT, ...) { \
|
---|
101 | static const char flash_str[] ICACHE_RODATA_ATTR STORE_ATTR = (FORMAT); \
|
---|
102 | printf(flash_str, ##__VA_ARGS__); \
|
---|
103 | printf("\r\n"); \
|
---|
104 | }
|
---|
105 |
|
---|
106 | #else /* NOT ESP8266_RTOS */
|
---|
107 |
|
---|
108 | // In order to make sure that the compiler evaluates the arguments and issues an error if they do not conform to printf
|
---|
109 | // specifications, we call printf with the format and __VA_ARGS__. Since C && operator is shortcircuiting no actual runtime call to printf is performed.
|
---|
110 | #if defined _MSC_VER
|
---|
111 | #ifndef LOGERROR_CAPTURES_STACK_TRACES
|
---|
112 | // ignore warning C4127
|
---|
113 | #define LOG(log_category, log_options, format, ...) \
|
---|
114 | { \
|
---|
115 | (void)(0 && printf(format, __VA_ARGS__)); \
|
---|
116 | { \
|
---|
117 | LOGGER_LOG l = xlogging_get_log_function(); \
|
---|
118 | if (l != NULL) \
|
---|
119 | { \
|
---|
120 | l(log_category, __FILE__, FUNC_NAME, __LINE__, log_options, format, __VA_ARGS__); \
|
---|
121 | } \
|
---|
122 | } \
|
---|
123 | }
|
---|
124 | #else /*LOGERROR_CAPTURES_STACK_TRACES is defined*/
|
---|
125 | #define STACK_PRINT_FORMAT "\nStack:\n%s"
|
---|
126 |
|
---|
127 | #define LOG(log_category, log_options, format, ...) MU_C3(LOG,_,log_category)(log_category, log_options, format, __VA_ARGS__)
|
---|
128 |
|
---|
129 | #define LOG_AZ_LOG_ERROR(log_category, log_options, format, ...) \
|
---|
130 | { \
|
---|
131 | (void)(0 && printf(format, __VA_ARGS__)); \
|
---|
132 | { \
|
---|
133 | LOGGER_LOG l = xlogging_get_log_function(); \
|
---|
134 | if (l != NULL) \
|
---|
135 | { \
|
---|
136 | char* stackAsString = getStackAsString(); \
|
---|
137 | if (stackAsString == NULL) \
|
---|
138 | { \
|
---|
139 | l(log_category, __FILE__, FUNC_NAME, __LINE__, log_options, format, __VA_ARGS__); \
|
---|
140 | } \
|
---|
141 | else \
|
---|
142 | { \
|
---|
143 | size_t formatSize = strlen(format); \
|
---|
144 | char* formatWithStack = (char*)logging_malloc(formatSize + sizeof("STACK_PRINT_FORMAT")); \
|
---|
145 | if (formatWithStack == NULL) \
|
---|
146 | { \
|
---|
147 | l(log_category, __FILE__, FUNC_NAME, __LINE__, log_options, format, __VA_ARGS__); \
|
---|
148 | } \
|
---|
149 | else \
|
---|
150 | { \
|
---|
151 | (void)memcpy(formatWithStack, format, formatSize); \
|
---|
152 | (void)memcpy(formatWithStack + formatSize, STACK_PRINT_FORMAT, sizeof(STACK_PRINT_FORMAT)); \
|
---|
153 | l(log_category, __FILE__, FUNC_NAME, __LINE__, log_options, formatWithStack, __VA_ARGS__, stackAsString); \
|
---|
154 | logging_free(formatWithStack); \
|
---|
155 | } \
|
---|
156 | logging_free(stackAsString); \
|
---|
157 | } \
|
---|
158 | } \
|
---|
159 | } \
|
---|
160 | }
|
---|
161 |
|
---|
162 | #define LOG_AZ_LOG_TRACE LOG_AZ_LOG_INFO
|
---|
163 |
|
---|
164 | #define LOG_AZ_LOG_INFO(log_category, log_options, format, ...) \
|
---|
165 | { \
|
---|
166 | (void)(0 && printf(format, __VA_ARGS__)); \
|
---|
167 | { \
|
---|
168 | LOGGER_LOG l = xlogging_get_log_function(); \
|
---|
169 | if (l != NULL) \
|
---|
170 | { \
|
---|
171 | l(log_category, __FILE__, FUNC_NAME, __LINE__, log_options, format, __VA_ARGS__); \
|
---|
172 | } \
|
---|
173 | } \
|
---|
174 | }
|
---|
175 |
|
---|
176 | #endif /*LOGERROR_CAPTURES_STACK_TRACES*/
|
---|
177 | #else
|
---|
178 | #define LOG(log_category, log_options, format, ...) { (void)(0 && printf(format, ##__VA_ARGS__)); { LOGGER_LOG l = xlogging_get_log_function(); if (l != NULL) l(log_category, __FILE__, FUNC_NAME, __LINE__, log_options, format, ##__VA_ARGS__); } }
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | #if defined _MSC_VER
|
---|
182 | #define LogInfo(FORMAT, ...) do{LOG(AZ_LOG_INFO, LOG_LINE, FORMAT, __VA_ARGS__); }while((void)0,0)
|
---|
183 | #else
|
---|
184 | #define LogInfo(FORMAT, ...) do{LOG(AZ_LOG_INFO, LOG_LINE, FORMAT, ##__VA_ARGS__); }while((void)0,0)
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | #ifdef WIN32
|
---|
188 | extern void xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(int errorMessageID);
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | #if defined _MSC_VER
|
---|
192 |
|
---|
193 | extern void xlogging_set_log_function_GetLastError(LOGGER_LOG_GETLASTERROR log_function);
|
---|
194 | extern LOGGER_LOG_GETLASTERROR xlogging_get_log_function_GetLastError(void);
|
---|
195 | #define LogLastError(FORMAT, ...) do{ LOGGER_LOG_GETLASTERROR l = xlogging_get_log_function_GetLastError(); if(l!=NULL) l(__FILE__, FUNC_NAME, __LINE__, FORMAT, __VA_ARGS__); }while((void)0,0)
|
---|
196 |
|
---|
197 | #define LogError(FORMAT, ...) do{ LOG(AZ_LOG_ERROR, LOG_LINE, FORMAT, __VA_ARGS__); }while((void)0,0)
|
---|
198 | #define LogErrorWinHTTPWithGetLastErrorAsString(FORMAT, ...) do { \
|
---|
199 | int errorMessageID = GetLastError(); \
|
---|
200 | LogError(FORMAT, __VA_ARGS__); \
|
---|
201 | xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(errorMessageID); \
|
---|
202 | } while((void)0,0)
|
---|
203 | #else // _MSC_VER
|
---|
204 | #define LogError(FORMAT, ...) do{ LOG(AZ_LOG_ERROR, LOG_LINE, FORMAT, ##__VA_ARGS__); }while((void)0,0)
|
---|
205 |
|
---|
206 | #ifdef WIN32
|
---|
207 | // Included when compiling on Windows but not with MSVC, e.g. with MinGW.
|
---|
208 | #define LogErrorWinHTTPWithGetLastErrorAsString(FORMAT, ...) do { \
|
---|
209 | int errorMessageID = GetLastError(); \
|
---|
210 | LogError(FORMAT, ##__VA_ARGS__); \
|
---|
211 | xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(errorMessageID); \
|
---|
212 | } while((void)0,0)
|
---|
213 | #endif // WIN32
|
---|
214 |
|
---|
215 | #endif // _MSC_VER
|
---|
216 |
|
---|
217 | extern void LogBinary(const char* comment, const void* data, size_t size);
|
---|
218 |
|
---|
219 | extern void xlogging_set_log_function(LOGGER_LOG log_function);
|
---|
220 | extern LOGGER_LOG xlogging_get_log_function(void);
|
---|
221 |
|
---|
222 | #endif /* NOT ESP8266_RTOS */
|
---|
223 |
|
---|
224 | #ifdef __cplusplus
|
---|
225 | } // extern "C"
|
---|
226 | #endif /* __cplusplus */
|
---|
227 |
|
---|
228 | #endif /* XLOGGING_H */
|
---|