forked from mongoose-os-libs/homekit-adk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHAPLog.h
617 lines (565 loc) · 19.1 KB
/
HAPLog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// Copyright (c) 2015-2019 The HomeKit ADK Contributors
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// See [CONTRIBUTORS.md] for the list of HomeKit ADK project authors.
#ifndef HAP_LOG_H
#define HAP_LOG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "HAPBase.h"
#if __has_feature(nullability)
#pragma clang assume_nonnull begin
#endif
// Validate log level.
// 0 - No logs are emitted. Default.
// 1 - Logs with type Default, Error and Fault are emitted.
// 2 - Logs with type Info, Default, Error and Fault are emitted.
// 3 - Logs with type Debug, Info, Default, Error and Fault are emitted. All logs.
#ifndef HAP_LOG_LEVEL
int HAPPlatformLogLevel(void);
#define HAP_LOG_LEVEL HAPPlatformLogLevel()
#else
#if HAP_LOG_LEVEL < 0 || HAP_LOG_LEVEL > 3
#error "Invalid HAP_LOG_LEVEL."
#endif
#endif
// Validate flag for including sensitive information in logs.
#ifndef HAP_LOG_SENSITIVE
#define HAP_LOG_SENSITIVE (0)
#endif
#if HAP_LOG_SENSITIVE < 0 || HAP_LOG_SENSITIVE > 1
#error "Invalid HAP_LOG_SENSITIVE."
#endif
/**
* Log object.
*/
typedef struct {
/**
* Subsystem that's performing logging.
*/
const char* _Nullable subsystem;
/**
* A category within the specified subsystem.
*
* - If a category is defined, a subsystem must be specified as well.
*/
const char* _Nullable category;
} HAPLogObject;
/**
* Default log object.
*
* - Log messages are logged with NULL subsystem and category.
*/
extern const HAPLogObject kHAPLog_Default;
/**
* Logging levels.
*/
HAP_ENUM_BEGIN(uint8_t, HAPLogType) {
/**
* Messages logged at this level contain information that may be useful during development or while troubleshooting
* a specific problem.
*/
kHAPLogType_Debug,
/**
* Use this level to capture information that may be helpful, but isn't essential, for troubleshooting errors.
*/
kHAPLogType_Info,
/**
* Use this level to capture information about things that might result a failure.
*/
kHAPLogType_Default,
/**
* Error-level messages are intended for reporting component-level errors.
*/
kHAPLogType_Error,
/**
* Fault-level messages are intended for capturing system-level or multi-component errors only.
*/
kHAPLogType_Fault
} HAP_ENUM_END(uint8_t, HAPLogType);
/**
* Logs the contents of a buffer and a message at a specific logging level.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
* @param type A log type constant, indicating the level of logging to perform.
*/
#define HAPLogBufferWithType(log, bytes, numBytes, type, ...) \
do { \
switch (type) { \
case kHAPLogType_Debug: { \
HAPLogBufferDebug(log, bytes, numBytes, __VA_ARGS__); \
} break; \
case kHAPLogType_Info: { \
HAPLogBufferInfo(log, bytes, numBytes, __VA_ARGS__); \
} break; \
case kHAPLogType_Default: { \
HAPLogBuffer(log, bytes, numBytes, __VA_ARGS__); \
} break; \
case kHAPLogType_Error: { \
HAPLogBufferError(log, bytes, numBytes, __VA_ARGS__); \
} break; \
case kHAPLogType_Fault: { \
HAPLogBufferFault(log, bytes, numBytes, __VA_ARGS__); \
} break; \
} \
} while (0)
/**
* Logs the contents of a buffer and a default-level message.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogBuffer(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_LEVEL >= 1) { \
HAPLogBufferInternal(log, bytes, numBytes, __VA_ARGS__); \
} \
} while (0)
/**
* Logs the contents of a buffer and an info-level message.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogBufferInfo(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_LEVEL >= 2) { \
HAPLogBufferInfoInternal(log, bytes, numBytes, __VA_ARGS__); \
} \
} while (0)
/**
* Logs the contents of a buffer and a debug-level message.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogBufferDebug(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_LEVEL >= 3) { \
HAPLogBufferDebugInternal(log, bytes, numBytes, __VA_ARGS__); \
} \
} while (0)
/**
* Logs the contents of a buffer and an error-level message.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogBufferError(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_LEVEL >= 1) { \
HAPLogBufferErrorInternal(log, bytes, numBytes, __VA_ARGS__); \
} \
} while (0)
/**
* Logs the contents of a buffer and a fault-level message.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogBufferFault(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_LEVEL >= 1) { \
HAPLogBufferFaultInternal(log, bytes, numBytes, __VA_ARGS__); \
} \
} while (0)
/**
* Logs a message at a specific logging level.
*
* @param log Log object.
* @param type A log type constant, indicating the level of logging to perform.
*/
#define HAPLogWithType(log, type, ...) \
do { \
switch (type) { \
case kHAPLogType_Debug: { \
HAPLogDebug(log, __VA_ARGS__); \
} break; \
case kHAPLogType_Info: { \
HAPLogInfo(log, __VA_ARGS__); \
} break; \
case kHAPLogType_Default: { \
HAPLog(log, __VA_ARGS__); \
} break; \
case kHAPLogType_Error: { \
HAPLogError(log, __VA_ARGS__); \
} break; \
case kHAPLogType_Fault: { \
HAPLogFault(log, __VA_ARGS__); \
} break; \
} \
} while (0)
/**
* Logs a default-level message.
*
* @param log Log object.
*/
#define HAPLog(log, ...) \
do { \
if (HAP_LOG_LEVEL >= 1) { \
HAPLogInternal(log, __VA_ARGS__); \
} \
} while (0)
/**
* Logs an info-level message.
*
* @param log Log object.
*/
#define HAPLogInfo(log, ...) \
do { \
if (HAP_LOG_LEVEL >= 2) { \
HAPLogInfoInternal(log, __VA_ARGS__); \
} \
} while (0)
/**
* Logs a debug-level message.
*
* @param log Log object.
*/
#define HAPLogDebug(log, ...) \
do { \
if (HAP_LOG_LEVEL >= 3) { \
HAPLogDebugInternal(log, __VA_ARGS__); \
} \
} while (0)
/**
* Logs an error-level message.
*
* @param log Log object.
*/
#define HAPLogError(log, ...) \
do { \
if (HAP_LOG_LEVEL >= 1) { \
HAPLogErrorInternal(log, __VA_ARGS__); \
} \
} while (0)
/**
* Logs a fault-level message.
*
* @param log Log object.
*/
#define HAPLogFault(log, ...) \
do { \
if (HAP_LOG_LEVEL >= 1) { \
HAPLogFaultInternal(log, __VA_ARGS__); \
} \
} while (0)
/**
* Logs the contents of a buffer and a message at a specific logging level that may contain sensitive information.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
* @param type A log type constant, indicating the level of logging to perform.
*/
#define HAPLogSensitiveBufferWithType(log, bytes, numBytes, type, ...) \
do { \
switch (type) { \
case kHAPLogType_Debug: { \
HAPLogSensitiveBufferDebug(log, bytes, numBytes, __VA_ARGS__); \
} break; \
case kHAPLogType_Info: { \
HAPLogSensitiveBufferInfo(log, bytes, numBytes, __VA_ARGS__); \
} break; \
case kHAPLogType_Default: { \
HAPLogSensitiveBuffer(log, bytes, numBytes, __VA_ARGS__); \
} break; \
case kHAPLogType_Error: { \
HAPLogSensitiveBufferError(log, bytes, numBytes, __VA_ARGS__); \
} break; \
case kHAPLogType_Fault: { \
HAPLogSensitiveBufferFault(log, bytes, numBytes, __VA_ARGS__); \
} break; \
} \
} while (0)
/**
* Logs the contents of a buffer and a default-level message that may contain sensitive information.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogSensitiveBuffer(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogBuffer(log, bytes, numBytes, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLog(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs the contents of a buffer and an info-level message that may contain sensitive information.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogSensitiveBufferInfo(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogBufferInfo(log, bytes, numBytes, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLogInfo(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs the contents of a buffer and a debug-level message that may contain sensitive information.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogSensitiveBufferDebug(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogBufferDebug(log, bytes, numBytes, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLogDebug(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs the contents of a buffer and an error-level message that may contain sensitive information.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogSensitiveBufferError(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogBufferError(log, bytes, numBytes, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLogError(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs the contents of a buffer and a fault-level message that may contain sensitive information.
*
* @param log Log object.
* @param bytes Buffer containing data to log.
* @param numBytes Length of buffer.
*/
#define HAPLogSensitiveBufferFault(log, bytes, numBytes, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogBufferFault(log, bytes, numBytes, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLogFault(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs a message at a specific logging level that may contain sensitive information.
*
* @param log Log object.
* @param type A log type constant, indicating the level of logging to perform.
*/
#define HAPLogSensitiveWithType(log, type, ...) \
do { \
switch (type) { \
case kHAPLogType_Debug: { \
HAPLogSensitiveDebug(log, __VA_ARGS__); \
} break; \
case kHAPLogType_Info: { \
HAPLogSensitiveInfo(log, __VA_ARGS__); \
} break; \
case kHAPLogType_Default: { \
HAPLogSensitive(log, __VA_ARGS__); \
} break; \
case kHAPLogType_Error: { \
HAPLogSensitiveError(log, __VA_ARGS__); \
} break; \
case kHAPLogType_Fault: { \
HAPLogSensitiveFault(log, __VA_ARGS__); \
} break; \
} \
} while (0)
/**
* Logs a default-level message that may contain sensitive information.
*
* @param log Log object.
*/
#define HAPLogSensitive(log, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLog(log, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLog(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs an info-level message that may contain sensitive information.
*
* @param log Log object.
*/
#define HAPLogSensitiveInfo(log, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogInfo(log, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLogInfo(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs a debug-level message that may contain sensitive information.
*
* @param log Log object.
*/
#define HAPLogSensitiveDebug(log, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogDebug(log, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLogDebug(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs an error-level message that may contain sensitive information.
*
* @param log Log object.
*/
#define HAPLogSensitiveError(log, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogError(log, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLogError(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
/**
* Logs a fault-level message that may contain sensitive information.
*
* @param log Log object.
*/
#define HAPLogSensitiveFault(log, ...) \
do { \
if (HAP_LOG_SENSITIVE) { \
HAPLogFault(log, __VA_ARGS__); \
} else { \
HAP_DIAGNOSTIC_PUSH \
HAP_DIAGNOSTIC_IGNORED_CLANG("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_GCC("-Wformat-extra-args") \
HAP_DIAGNOSTIC_IGNORED_ARMCC(225) \
HAPLogFault(log, "<private> %s", __VA_ARGS__); \
HAP_DIAGNOSTIC_POP \
} \
} while (0)
//----------------------------------------------------------------------------------------------------------------------
// Internal functions. Do not use directly.
/**@cond */
HAP_PRINTFLIKE(4, 5)
void HAPLogBufferInternal(
const HAPLogObject* _Nullable log,
const void* _Nullable bufferBytes,
size_t numBufferBytes,
const char* format,
...);
HAP_DISALLOW_USE(HAPLogBufferInternal)
HAP_PRINTFLIKE(4, 5)
void HAPLogBufferInfoInternal(
const HAPLogObject* _Nullable log,
const void* _Nullable bufferBytes,
size_t numBufferBytes,
const char* format,
...);
HAP_DISALLOW_USE(HAPLogBufferInfoInternal)
HAP_PRINTFLIKE(4, 5)
void HAPLogBufferDebugInternal(
const HAPLogObject* _Nullable log,
const void* _Nullable bufferBytes,
size_t numBufferBytes,
const char* format,
...);
HAP_DISALLOW_USE(HAPLogBufferDebugInternal)
HAP_PRINTFLIKE(4, 5)
void HAPLogBufferErrorInternal(
const HAPLogObject* _Nullable log,
const void* _Nullable bufferBytes,
size_t numBufferBytes,
const char* format,
...);
HAP_DISALLOW_USE(HAPLogBufferErrorInternal)
HAP_PRINTFLIKE(4, 5)
void HAPLogBufferFaultInternal(
const HAPLogObject* _Nullable log,
const void* _Nullable bufferBytes,
size_t numBufferBytes,
const char* format,
...);
HAP_DISALLOW_USE(HAPLogBufferFaultInternal)
HAP_PRINTFLIKE(2, 3)
void HAPLogInternal(const HAPLogObject* _Nullable log, const char* format, ...);
HAP_DISALLOW_USE(HAPLogInternal)
HAP_PRINTFLIKE(2, 3)
void HAPLogInfoInternal(const HAPLogObject* _Nullable log, const char* format, ...);
HAP_DISALLOW_USE(HAPLogInfoInternal)
HAP_PRINTFLIKE(2, 3)
void HAPLogDebugInternal(const HAPLogObject* _Nullable log, const char* format, ...);
HAP_DISALLOW_USE(HAPLogDebugInternal)
HAP_PRINTFLIKE(2, 3)
void HAPLogErrorInternal(const HAPLogObject* _Nullable log, const char* format, ...);
HAP_DISALLOW_USE(HAPLogErrorInternal)
HAP_PRINTFLIKE(2, 3)
void HAPLogFaultInternal(const HAPLogObject* _Nullable log, const char* format, ...);
HAP_DISALLOW_USE(HAPLogFaultInternal)
/**@endcond */
#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif
#ifdef __cplusplus
}
#endif
#endif