[388] | 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 | #include "azure_c_shared_utility/agenttime.h"
|
---|
| 8 | #include "azure_c_shared_utility/optimize_size.h"
|
---|
| 9 |
|
---|
| 10 | #if defined(ESP8266_RTOS)
|
---|
| 11 | #include "c_types.h"
|
---|
| 12 | #endif
|
---|
| 13 |
|
---|
| 14 | #if defined(ARDUINO_ARCH_ESP8266)
|
---|
| 15 | #include "esp8266/azcpgmspace.h"
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
| 18 | #ifdef __cplusplus
|
---|
| 19 | /* Some compilers do not want to play by the standard, specifically ARM CC */
|
---|
| 20 | #ifdef MBED_BUILD_TIMESTAMP
|
---|
| 21 | #include <stdio.h>
|
---|
| 22 | #else
|
---|
| 23 | #include <cstdio>
|
---|
| 24 | #endif
|
---|
| 25 | extern "C" {
|
---|
| 26 | #else
|
---|
| 27 | #include <stdio.h>
|
---|
| 28 | #endif /* __cplusplus */
|
---|
| 29 |
|
---|
| 30 | #ifdef TIZENRT
|
---|
| 31 | #undef LOG_INFO
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 | typedef enum LOG_CATEGORY_TAG
|
---|
| 35 | {
|
---|
| 36 | AZ_LOG_ERROR,
|
---|
| 37 | AZ_LOG_INFO,
|
---|
| 38 | AZ_LOG_TRACE
|
---|
| 39 | } LOG_CATEGORY;
|
---|
| 40 |
|
---|
| 41 | #if defined _MSC_VER
|
---|
| 42 | #define FUNC_NAME __FUNCDNAME__
|
---|
| 43 | #else
|
---|
| 44 | #define FUNC_NAME __func__
|
---|
| 45 | #endif
|
---|
| 46 |
|
---|
| 47 | typedef void(*LOGGER_LOG)(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...);
|
---|
| 48 | typedef void(*LOGGER_LOG_GETLASTERROR)(const char* file, const char* func, int line, const char* format, ...);
|
---|
| 49 |
|
---|
| 50 | #define TEMP_BUFFER_SIZE 1024
|
---|
| 51 | #define MESSAGE_BUFFER_SIZE 260
|
---|
| 52 |
|
---|
| 53 | #define LOG_NONE 0x00
|
---|
| 54 | #define LOG_LINE 0x01
|
---|
| 55 |
|
---|
| 56 | /*no logging is useful when time and fprintf are mocked*/
|
---|
| 57 | #ifdef NO_LOGGING
|
---|
| 58 | #define LOG(...)
|
---|
| 59 | #define LogInfo(...)
|
---|
| 60 | #define LogBinary(...)
|
---|
| 61 | #define LogError(...)
|
---|
| 62 | #define xlogging_get_log_function() NULL
|
---|
| 63 | #define xlogging_set_log_function(...)
|
---|
| 64 | #define LogErrorWinHTTPWithGetLastErrorAsString(...)
|
---|
| 65 | #define UNUSED(x) (void)(x)
|
---|
| 66 | #elif (defined MINIMAL_LOGERROR)
|
---|
| 67 | #define LOG(...)
|
---|
| 68 | #define LogInfo(...)
|
---|
| 69 | #define LogBinary(...)
|
---|
| 70 | #define LogError(...) printf("error %s: line %d\n",__FILE__,__LINE__);
|
---|
| 71 | #define xlogging_get_log_function() NULL
|
---|
| 72 | #define xlogging_set_log_function(...)
|
---|
| 73 | #define LogErrorWinHTTPWithGetLastErrorAsString(...)
|
---|
| 74 | #define UNUSED(x) (void)(x)
|
---|
| 75 |
|
---|
| 76 | #elif defined(ESP8266_RTOS)
|
---|
| 77 | #define LogInfo(FORMAT, ...) do { \
|
---|
| 78 | static const char flash_str[] ICACHE_RODATA_ATTR STORE_ATTR = FORMAT; \
|
---|
| 79 | printf(flash_str, ##__VA_ARGS__); \
|
---|
| 80 | printf("\n");\
|
---|
| 81 | } while((void)0,0)
|
---|
| 82 |
|
---|
| 83 | #define LogError LogInfo
|
---|
| 84 | #define LOG(log_category, log_options, FORMAT, ...) { \
|
---|
| 85 | static const char flash_str[] ICACHE_RODATA_ATTR STORE_ATTR = (FORMAT); \
|
---|
| 86 | printf(flash_str, ##__VA_ARGS__); \
|
---|
| 87 | printf("\r\n"); \
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | #else /* NOT ESP8266_RTOS */
|
---|
| 91 |
|
---|
| 92 | // In order to make sure that the compiler evaluates the arguments and issues an error if they do not conform to printf
|
---|
| 93 | // specifications, we call printf with the format and __VA_ARGS__ but the call is behind an if (0) so that it does
|
---|
| 94 | // not actually get executed at runtime
|
---|
| 95 | #if defined _MSC_VER
|
---|
| 96 | // ignore warning C4127
|
---|
| 97 | #define LOG(log_category, log_options, format, ...) \
|
---|
| 98 | { \
|
---|
| 99 | __pragma(warning(suppress: 4127)) \
|
---|
| 100 | if (0) \
|
---|
| 101 | { \
|
---|
| 102 | (void)printf(format, __VA_ARGS__); \
|
---|
| 103 | } \
|
---|
| 104 | { \
|
---|
| 105 | LOGGER_LOG l = xlogging_get_log_function(); \
|
---|
| 106 | if (l != NULL) \
|
---|
| 107 | { \
|
---|
| 108 | l(log_category, __FILE__, FUNC_NAME, __LINE__, log_options, format, __VA_ARGS__); \
|
---|
| 109 | } \
|
---|
| 110 | } \
|
---|
| 111 | }
|
---|
| 112 | #else
|
---|
| 113 | #define LOG(log_category, log_options, format, ...) { if (0) { (void)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__); } }
|
---|
| 114 | #endif
|
---|
| 115 |
|
---|
| 116 | #if defined _MSC_VER
|
---|
| 117 | #define LogInfo(FORMAT, ...) do{LOG(AZ_LOG_INFO, LOG_LINE, FORMAT, __VA_ARGS__); }while((void)0,0)
|
---|
| 118 | #else
|
---|
| 119 | #define LogInfo(FORMAT, ...) do{LOG(AZ_LOG_INFO, LOG_LINE, FORMAT, ##__VA_ARGS__); }while((void)0,0)
|
---|
| 120 | #endif
|
---|
| 121 |
|
---|
| 122 | #ifdef WIN32
|
---|
| 123 | extern void xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(int errorMessageID);
|
---|
| 124 | #endif
|
---|
| 125 |
|
---|
| 126 | #if defined _MSC_VER
|
---|
| 127 |
|
---|
| 128 | extern void xlogging_set_log_function_GetLastError(LOGGER_LOG_GETLASTERROR log_function);
|
---|
| 129 | extern LOGGER_LOG_GETLASTERROR xlogging_get_log_function_GetLastError(void);
|
---|
| 130 | #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)
|
---|
| 131 |
|
---|
| 132 | #define LogError(FORMAT, ...) do{ LOG(AZ_LOG_ERROR, LOG_LINE, FORMAT, __VA_ARGS__); }while((void)0,0)
|
---|
| 133 | #define LogErrorWinHTTPWithGetLastErrorAsString(FORMAT, ...) do { \
|
---|
| 134 | int errorMessageID = GetLastError(); \
|
---|
| 135 | LogError(FORMAT, __VA_ARGS__); \
|
---|
| 136 | xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(errorMessageID); \
|
---|
| 137 | } while((void)0,0)
|
---|
| 138 | #else // _MSC_VER
|
---|
| 139 | #define LogError(FORMAT, ...) do{ LOG(AZ_LOG_ERROR, LOG_LINE, FORMAT, ##__VA_ARGS__); }while((void)0,0)
|
---|
| 140 |
|
---|
| 141 | #ifdef WIN32
|
---|
| 142 | // Included when compiling on Windows but not with MSVC, e.g. with MinGW.
|
---|
| 143 | #define LogErrorWinHTTPWithGetLastErrorAsString(FORMAT, ...) do { \
|
---|
| 144 | int errorMessageID = GetLastError(); \
|
---|
| 145 | LogError(FORMAT, ##__VA_ARGS__); \
|
---|
| 146 | xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(errorMessageID); \
|
---|
| 147 | } while((void)0,0)
|
---|
| 148 | #endif // WIN32
|
---|
| 149 |
|
---|
| 150 | #endif // _MSC_VER
|
---|
| 151 |
|
---|
| 152 | extern void LogBinary(const char* comment, const void* data, size_t size);
|
---|
| 153 |
|
---|
| 154 | extern void xlogging_set_log_function(LOGGER_LOG log_function);
|
---|
| 155 | extern LOGGER_LOG xlogging_get_log_function(void);
|
---|
| 156 |
|
---|
| 157 | #endif /* NOT ESP8266_RTOS */
|
---|
| 158 |
|
---|
| 159 | #ifdef __cplusplus
|
---|
| 160 | } // extern "C"
|
---|
| 161 | #endif /* __cplusplus */
|
---|
| 162 |
|
---|
| 163 | #endif /* XLOGGING_H */
|
---|