IoT managed integrations End Device SDK
Loading...
Searching...
No Matches
iotmi_device_logging_stack.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Amazon.com, Inc. or its affiliates. All rights reserved.
3 *
4 * AMAZON PROPRIETARY/CONFIDENTIAL
5 *
6 * You may not use this file except in compliance with the terms and
7 * conditions set forth in the accompanying LICENSE.txt file.
8 *
9 * THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS. AMAZON SPECIFICALLY
10 * DISCLAIMS, WITH RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS,
11 * IMPLIED, OR STATUTORY, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
12 * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
13 */
14
21#ifndef IOTMI_DEVICE_LOGGING_STACK_H_
22#define IOTMI_DEVICE_LOGGING_STACK_H_
23
24/* Include header for logging level macros. */
26
27/* Standard Include. */
28#include <stdint.h>
29#include <stdio.h>
30
31/* Include for clock information. */
32#include "iotmi_pal_Clock.h"
33#include "iotmi_pal_Log.h"
34
35/* Include for SDK version. */
36#include "iotmi_device_Version.h"
37
38#if (IOTMI_USE_MANAGED_INTEGRATIONS_DEVICE_LOG == 1)
39#include <stddef.h>
40
41#include "log/c_wrapper/iotshd_log_c_wrapper.h"
42#endif
43
54/* Check if LIBRARY_LOG_NAME macro has been defined. */
55#if !defined(LIBRARY_LOG_NAME)
56#error "Please define LIBRARY_LOG_NAME for the library."
57#endif
58
59/* Metadata information to prepend to every log message. */
60#ifndef LOG_METADATA_FORMAT
61#define LOG_METADATA_FORMAT \
62 "[%u][%s][%s]"
63#endif
64
65#ifndef LOG_METADATA_ARGS
66#define LOG_METADATA_ARGS \
67 iotmiPal_clockGetTimeMs(), IOTMI_DEVICE_SDK_VERSION, \
68 LIBRARY_LOG_NAME
71#endif
72
73#if (IOTMI_USE_MANAGED_INTEGRATIONS_DEVICE_LOG == 1)
74/* Extern default log handler. */
75extern LoggerHandle globalManagedIntegrationsLoggerHandle;
76
77/* Use macro to remove double braskets. */
78#define managedIntegrationsLoggerError(...) \
79 do { \
80 if (globalManagedIntegrationsLoggerHandle != NULL) { \
81 iotshdlog_c_logger_handle_log_err(globalManagedIntegrationsLoggerHandle, \
82 __VA_ARGS__); \
83 } else { \
84 iotmiPal_logError(__VA_ARGS__); \
85 } \
86 } while (0)
87#define managedIntegrationsLoggerWarn(...) \
88 do { \
89 if (globalManagedIntegrationsLoggerHandle != NULL) { \
90 iotshdlog_c_logger_handle_log_warn( \
91 globalManagedIntegrationsLoggerHandle, __VA_ARGS__); \
92 } else { \
93 iotmiPal_logWarn(__VA_ARGS__); \
94 } \
95 } while (0)
96#define managedIntegrationsLoggerInfo(...) \
97 do { \
98 if (globalManagedIntegrationsLoggerHandle != NULL) { \
99 iotshdlog_c_logger_handle_log_info( \
100 globalManagedIntegrationsLoggerHandle, __VA_ARGS__); \
101 } else { \
102 iotmiPal_logInfo(__VA_ARGS__); \
103 } \
104 } while (0)
105#define managedIntegrationsLoggerDebug(...) \
106 do { \
107 if (globalManagedIntegrationsLoggerHandle != NULL) { \
108 iotshdlog_c_logger_handle_log_debug( \
109 globalManagedIntegrationsLoggerHandle, __VA_ARGS__); \
110 } else { \
111 iotmiPal_logDebug(__VA_ARGS__); \
112 } \
113 } while (0)
114
115#define iotmiDev_logError managedIntegrationsLoggerError
116#define iotmiDev_logWarn managedIntegrationsLoggerWarn
117#define iotmiDev_logInfo managedIntegrationsLoggerInfo
118#define iotmiDev_logDebug managedIntegrationsLoggerDebug
119#else /* if ( IOTMI_USE_MANAGED_INTEGRATIONS_DEVICE_LOG == 1 ) */
120#define iotmiDev_logError iotmiPal_logError
121#define iotmiDev_logWarn iotmiPal_logWarn
122#define iotmiDev_logInfo iotmiPal_logInfo
123#define iotmiDev_logDebug iotmiPal_logDebug
124#endif /* if ( IOTMI_USE_MANAGED_INTEGRATIONS_DEVICE_LOG == 1 ) */
125
126#define COMPILE_TIME_ERROR(msg) \
127 void compile_time_error(void) { (void)sizeof(char[1 - 2 * !!(msg)]); }
128#define InternalLog_Unsupported(...) \
129 COMPILE_TIME_ERROR("Unsupported log format")
130
131/* Error log formatter. */
132#define InternalLogError_1(format) \
133 iotmiDev_logError(LOG_METADATA_FORMAT "[ERROR] " format "\r\n", \
134 LOG_METADATA_ARGS)
135#define InternalLogError_Multiple(format, ...) \
136 iotmiDev_logError(LOG_METADATA_FORMAT "[ERROR] " format "\r\n", \
137 LOG_METADATA_ARGS, __VA_ARGS__)
138
139#define InternalLogErrorFormatter(...) \
140 InternalLogErrorMacroSelect( \
141 __VA_ARGS__, InternalLogError_Multiple, InternalLogError_Multiple, \
142 InternalLogError_Multiple, InternalLogError_Multiple, \
143 InternalLogError_Multiple, InternalLogError_Multiple, \
144 InternalLogError_Multiple, InternalLogError_Multiple, \
145 InternalLogError_Multiple, InternalLogError_1, \
146 InternalLog_Unsupported)(__VA_ARGS__)
147#define InternalLogErrorMacroSelect(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
148 N, ...) \
149 N
150
151/* Warn log formatter. */
152#define InternalLogWarn_1(format) \
153 iotmiDev_logWarn(LOG_METADATA_FORMAT "[WARN] " format "\r\n", \
154 LOG_METADATA_ARGS)
155#define InternalLogWarn_Multiple(format, ...) \
156 iotmiDev_logWarn(LOG_METADATA_FORMAT "[WARN] " format "\r\n", \
157 LOG_METADATA_ARGS, __VA_ARGS__)
158
159#define InternalLogWarnFormatter(...) \
160 InternalLogWarnMacroSelect( \
161 __VA_ARGS__, InternalLogWarn_Multiple, InternalLogWarn_Multiple, \
162 InternalLogWarn_Multiple, InternalLogWarn_Multiple, \
163 InternalLogWarn_Multiple, InternalLogWarn_Multiple, \
164 InternalLogWarn_Multiple, InternalLogWarn_Multiple, \
165 InternalLogWarn_Multiple, InternalLogWarn_1, \
166 InternalLog_Unsupported)(__VA_ARGS__)
167#define InternalLogWarnMacroSelect(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, \
168 ...) \
169 N
170
171/* Info log formatter. */
172#define InternalLogInfo_1(format) \
173 iotmiDev_logInfo(LOG_METADATA_FORMAT "[INFO] " format "\r\n", \
174 LOG_METADATA_ARGS)
175#define InternalLogInfo_Multiple(format, ...) \
176 iotmiDev_logInfo(LOG_METADATA_FORMAT "[INFO] " format "\r\n", \
177 LOG_METADATA_ARGS, __VA_ARGS__)
178
179#define InternalLogInfoFormatter(...) \
180 InternalLogInfoMacroSelect( \
181 __VA_ARGS__, InternalLogInfo_Multiple, InternalLogInfo_Multiple, \
182 InternalLogInfo_Multiple, InternalLogInfo_Multiple, \
183 InternalLogInfo_Multiple, InternalLogInfo_Multiple, \
184 InternalLogInfo_Multiple, InternalLogInfo_Multiple, \
185 InternalLogInfo_Multiple, InternalLogInfo_1, \
186 InternalLog_Unsupported)(__VA_ARGS__)
187#define InternalLogInfoMacroSelect(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, \
188 ...) \
189 N
190
191/* Debug log formatter. */
192#define InternalLogDebug_1(format) \
193 iotmiDev_logDebug(LOG_METADATA_FORMAT "[DEBUG] " format "\r\n", \
194 LOG_METADATA_ARGS)
195#define InternalLogDebug_Multiple(format, ...) \
196 iotmiDev_logDebug(LOG_METADATA_FORMAT "[DEBUG] " format "\r\n", \
197 LOG_METADATA_ARGS, __VA_ARGS__)
198
199#define InternalLogDebugFormatter(...) \
200 InternalLogDebugMacroSelect( \
201 __VA_ARGS__, InternalLogDebug_Multiple, InternalLogDebug_Multiple, \
202 InternalLogDebug_Multiple, InternalLogDebug_Multiple, \
203 InternalLogDebug_Multiple, InternalLogDebug_Multiple, \
204 InternalLogDebug_Multiple, InternalLogDebug_Multiple, \
205 InternalLogDebug_Multiple, InternalLogDebug_1, \
206 InternalLog_Unsupported)(__VA_ARGS__)
207#define InternalLogDebugMacroSelect(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
208 N, ...) \
209 N
210
216/* Check that LIBRARY_LOG_LEVEL is defined and has a valid value. */
217#if !defined(LIBRARY_LOG_LEVEL) || \
218 ((LIBRARY_LOG_LEVEL != LOG_NONE) && (LIBRARY_LOG_LEVEL != LOG_ERROR) && \
219 (LIBRARY_LOG_LEVEL != LOG_WARN) && (LIBRARY_LOG_LEVEL != LOG_INFO) && \
220 (LIBRARY_LOG_LEVEL != LOG_DEBUG))
221#error \
222 "Please define LIBRARY_LOG_LEVEL as either LOG_NONE, LOG_ERROR, LOG_WARN, LOG_INFO, or LOG_DEBUG."
223#else
224#if LIBRARY_LOG_LEVEL == LOG_DEBUG
225/* All log level messages will logged. */
226#define LogError(message) InternalLogErrorFormatter message
227#define LogWarn(message) InternalLogWarnFormatter message
228#define LogInfo(message) InternalLogInfoFormatter message
229#define LogDebug(message) InternalLogDebugFormatter message
230#elif LIBRARY_LOG_LEVEL == LOG_INFO
231/* Only INFO, WARNING, ERROR, and ALWAYS messages will be logged. */
232#define LogError(message) InternalLogErrorFormatter message
233#define LogWarn(message) InternalLogWarnFormatter message
234#define LogInfo(message) InternalLogInfoFormatter message
235#define LogDebug(message)
236
237#elif LIBRARY_LOG_LEVEL == LOG_WARN
238/* Only WARNING, ERROR, and ALWAYS messages will be logged. */
239#define LogError(message) InternalLogErrorFormatter message
240#define LogWarn(message) InternalLogWarnFormatter message
241#define LogInfo(message)
242#define LogDebug(message)
243
244#elif LIBRARY_LOG_LEVEL == LOG_ERROR
245/* Only ERROR and ALWAYS messages will be logged. */
246#define LogError(message) InternalLogErrorFormatter message
247#define LogWarn(message)
248#define LogInfo(message)
249#define LogDebug(message)
250
251#else /* if LIBRARY_LOG_LEVEL == LOG_NONE */
252
253#define LogError(message)
254#define LogWarn(message)
255#define LogInfo(message)
256#define LogDebug(message)
257
258#endif /* if LIBRARY_LOG_LEVEL == LOG_NONE */
259#endif /* if !defined( LIBRARY_LOG_LEVEL ) || ( ( LIBRARY_LOG_LEVEL != \
260 LOG_NONE ) && ( LIBRARY_LOG_LEVEL != LOG_ERROR ) && ( \
261 LIBRARY_LOG_LEVEL != LOG_WARN ) && ( LIBRARY_LOG_LEVEL != LOG_INFO ) \
262 && ( LIBRARY_LOG_LEVEL != LOG_DEBUG ) ) */
263
264#endif /* ifndef IOTMI_DEVICE_LOGGING_STACK_H_ */
Defines the configuration constants for all logging verbosity levels.
This file defines time-related functions platform abstraction interface.
This file defines log functions platform abstraction interface.