xref: /illumos-gate/usr/src/contrib/mDNSResponder/mDNSShared/mDNSDebug.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 2003-2015 Apple Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "mDNSDebug.h"
19 
20 #include <stdio.h>
21 
22 #if defined(WIN32) || defined(EFI32) || defined(EFI64) || defined(EFIX64)
23 // Need to add Windows/EFI syslog support here
24 #define LOG_PID 0x01
25 #define LOG_CONS 0x02
26 #define LOG_PERROR 0x20
27 #else
28 #include <syslog.h>
29 #endif
30 
31 #include "mDNSEmbeddedAPI.h"
32 
33 mDNSexport int mDNS_LoggingEnabled       = 0;
34 mDNSexport int mDNS_PacketLoggingEnabled = 0;
35 mDNSexport int mDNS_McastLoggingEnabled  = 0;
36 mDNSexport int mDNS_McastTracingEnabled  = 0;
37 
38 #if MDNS_DEBUGMSGS
39 mDNSexport int mDNS_DebugMode = mDNStrue;
40 #else
41 mDNSexport int mDNS_DebugMode = mDNSfalse;
42 #endif
43 
44 // Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows
45 // how to print special data types like IP addresses and length-prefixed domain names
46 #if MDNS_DEBUGMSGS > 1
47 mDNSexport void verbosedebugf_(const char *format, ...)
48 {
49     char buffer[512];
50     va_list ptr;
51     va_start(ptr,format);
52     buffer[mDNS_vsnprintf(buffer, sizeof(buffer), format, ptr)] = 0;
53     va_end(ptr);
54     mDNSPlatformWriteDebugMsg(buffer);
55 }
56 #endif
57 
58 // Log message with default "mDNSResponder" ident string at the start
59 mDNSlocal void LogMsgWithLevelv(mDNSLogLevel_t logLevel, const char *format, va_list ptr)
60 {
61     char buffer[512];
62     buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
63     mDNSPlatformWriteLogMsg(ProgramName, buffer, logLevel);
64 }
65 
66 #define LOG_HELPER_BODY(L) \
67     { \
68         va_list ptr; \
69         va_start(ptr,format); \
70         LogMsgWithLevelv(L, format, ptr); \
71         va_end(ptr); \
72     }
73 
74 // see mDNSDebug.h
75 #if !MDNS_HAS_VA_ARG_MACROS
76 void LogMsg_(const char *format, ...)       LOG_HELPER_BODY(MDNS_LOG_MSG)
77 void LogOperation_(const char *format, ...) LOG_HELPER_BODY(MDNS_LOG_OPERATION)
78 void LogSPS_(const char *format, ...)       LOG_HELPER_BODY(MDNS_LOG_SPS)
79 void LogInfo_(const char *format, ...)      LOG_HELPER_BODY(MDNS_LOG_INFO)
80 #endif
81 
82 #if MDNS_DEBUGMSGS
83 void debugf_(const char *format, ...)       LOG_HELPER_BODY(MDNS_LOG_DEBUG)
84 #endif
85 
86 // Log message with default "mDNSResponder" ident string at the start
87 mDNSexport void LogMsgWithLevel(mDNSLogLevel_t logLevel, const char *format, ...)
88 LOG_HELPER_BODY(logLevel)
89