Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Darwin / Logging.cpp
1 /* See Project chip LICENSE file for licensing information. */
2
3 #include <platform/logging/LogV.h>
4 #include <support/logging/Constants.h>
5
6 #include <core/CHIPConfig.h>
7 #include <support/logging/Constants.h>
8
9 #include <os/log.h>
10
11 #include <stdarg.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 namespace chip {
16 namespace Logging {
17 namespace Platform {
18
19 void LogV(const char * module, uint8_t category, const char * msg, va_list v)
20 {
21     char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
22     int32_t prefixLen = snprintf(formattedMsg, sizeof(formattedMsg), "CHIP: [%s] ", module);
23     if (prefixLen < 0)
24     {
25         // This should not happen
26         return;
27     }
28
29     if (static_cast<size_t>(prefixLen) >= sizeof(formattedMsg))
30     {
31         prefixLen = sizeof(formattedMsg) - 1;
32     }
33
34     vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - static_cast<size_t>(prefixLen), msg, v);
35
36     switch (category)
37     {
38     case kLogCategory_Error:
39         os_log_with_type(OS_LOG_DEFAULT, OS_LOG_TYPE_ERROR, "🔴 %{public}s", formattedMsg);
40 #if TARGET_OS_MAC && TARGET_OS_IPHONE == 0
41         fprintf(stdout, "\033[1;31m");
42 #endif
43         break;
44
45     case kLogCategory_Progress:
46         os_log_with_type(OS_LOG_DEFAULT, OS_LOG_TYPE_INFO, "🔵 %{public}s", formattedMsg);
47 #if TARGET_OS_MAC && TARGET_OS_IPHONE == 0
48         fprintf(stdout, "\033[0;32m");
49 #endif
50         break;
51
52     case kLogCategory_Detail:
53         os_log_with_type(OS_LOG_DEFAULT, OS_LOG_TYPE_DEBUG, "🟢 %{public}s", formattedMsg);
54 #if TARGET_OS_MAC && TARGET_OS_IPHONE == 0
55         fprintf(stdout, "\033[0;34m");
56 #endif
57         break;
58     }
59 #if TARGET_OS_MAC && TARGET_OS_IPHONE == 0
60     fprintf(stdout, "%s\033[0m\n", formattedMsg);
61 #endif
62 }
63
64 } // namespace Platform
65 } // namespace Logging
66 } // namespace chip