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