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