1 /*
2 * Copyright (c) 2003-2019 Apple Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18
19 #if defined(WIN32) || defined(EFI32) || defined(EFI64) || defined(EFIX64)
20 // Need to add Windows/EFI syslog support here
21 #define LOG_PID 0x01
22 #define LOG_CONS 0x02
23 #define LOG_PERROR 0x20
24 #else
25 #include <syslog.h>
26 #endif
27
28 #include "mDNSEmbeddedAPI.h"
29
30 mDNSexport int mDNS_LoggingEnabled = 0;
31 mDNSexport int mDNS_PacketLoggingEnabled = 0;
32 mDNSexport int mDNS_McastLoggingEnabled = 0;
33 mDNSexport int mDNS_McastTracingEnabled = 0;
34
35 #if MDNS_DEBUGMSGS
36 mDNSexport int mDNS_DebugMode = mDNStrue;
37 #else
38 mDNSexport int mDNS_DebugMode = mDNSfalse;
39 #endif
40
41 // Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows
42 // how to print special data types like IP addresses and length-prefixed domain names
43 #if MDNS_DEBUGMSGS > 1
verbosedebugf_(const char * format,...)44 mDNSexport void verbosedebugf_(const char *format, ...)
45 {
46 char buffer[512];
47 va_list args;
48 va_start(args, format);
49 buffer[mDNS_vsnprintf(buffer, sizeof(buffer), format, args)] = 0;
50 va_end(args);
51 mDNSPlatformWriteDebugMsg(buffer);
52 }
53 #endif
54
55 // Log message with default "mDNSResponder" ident string at the start
56 #if MDNSRESPONDER_SUPPORTS(APPLE, OS_LOG)
LogMsgWithLevelv(os_log_t category,os_log_type_t level,const char * format,va_list args)57 mDNSlocal void LogMsgWithLevelv(os_log_t category, os_log_type_t level, const char *format, va_list args)
58 {
59 char buffer[512];
60 mDNS_vsnprintf(buffer, (mDNSu32)sizeof(buffer), format, args);
61 os_log_with_type(category ? category : mDNSLogCategory_Default, level, "%{private}s", buffer);
62 }
63 #else
LogMsgWithLevelv(const char * category,mDNSLogLevel_t level,const char * format,va_list args)64 mDNSlocal void LogMsgWithLevelv(const char *category, mDNSLogLevel_t level, const char *format, va_list args)
65 {
66 char buffer[512];
67 char *dst = buffer;
68 const char *const lim = &buffer[512];
69 if (category) mDNS_snprintf_add(&dst, lim, "%s: ", category);
70 mDNS_vsnprintf(dst, (mDNSu32)(lim - dst), format, args);
71 mDNSPlatformWriteLogMsg(ProgramName, buffer, level);
72 }
73 #endif
74
75 #define LOG_HELPER_BODY(CATEGORY, LEVEL) \
76 { \
77 va_list args; \
78 va_start(args,format); \
79 LogMsgWithLevelv(CATEGORY, LEVEL, format, args); \
80 va_end(args); \
81 }
82
83 // see mDNSDebug.h
84 #if !MDNS_HAS_VA_ARG_MACROS
LogMsg_(const char * format,...)85 void LogMsg_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_INFO)
86 void LogOperation_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_INFO)
87 void LogSPS_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_INFO)
88 void LogInfo_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_INFO)
89 void LogDebug_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_DEBUG)
90 #endif
91
92 #if MDNS_DEBUGMSGS
93 void debugf_(const char *format, ...) LOG_HELPER_BODY(MDNS_LOG_DEBUG)
94 #endif
95
96 // Log message with default "mDNSResponder" ident string at the start
97 mDNSexport void LogMsgWithLevel(mDNSLogCategory_t category, mDNSLogLevel_t level, const char *format, ...)
98 LOG_HELPER_BODY(category, level)
99
100 mDNSexport void LogToFD(int fd, const char *format, ...)
101 {
102 va_list args;
103 va_start(args, format);
104 #if APPLE_OSX_mDNSResponder
105 char buffer[1024];
106 buffer[mDNS_vsnprintf(buffer, (mDNSu32)sizeof(buffer), format, args)] = '\0';
107 dprintf(fd, "%s\n", buffer);
108 #else
109 (void)fd;
110 LogMsgWithLevelv(NULL, MDNS_LOG_INFO, format, args);
111 #endif
112 va_end(args);
113 }
114