1 /* -*- Mode: C; tab-width: 4 -*- 2 * 3 * Copyright (c) 2003 Apple Computer, 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 File: mDNSDebug.c 18 19 Contains: Implementation of debugging utilities. Requires a POSIX environment. 20 21 Version: 1.0 22 23 Change History (most recent first): 24 25 Log: mDNSDebug.c,v $ 26 Revision 1.16 2009/04/11 01:43:28 jessic2 27 <rdar://problem/4426780> Daemon: Should be able to turn on LogOperation dynamically 28 29 Revision 1.15 2009/04/11 00:20:26 jessic2 30 <rdar://problem/4426780> Daemon: Should be able to turn on LogOperation dynamically 31 32 Revision 1.14 2008/11/26 00:01:08 cheshire 33 Get rid of "Unknown" tag in SIGINFO output on Leopard 34 35 Revision 1.13 2007/12/01 00:40:30 cheshire 36 Fixes from Bob Bradley for building on EFI 37 38 Revision 1.12 2007/10/01 19:06:19 cheshire 39 Defined symbolic constant MDNS_LOG_INITIAL_LEVEL to set the logging level we start out at 40 41 Revision 1.11 2007/07/27 20:19:56 cheshire 42 For now, comment out unused log levels MDNS_LOG_ERROR, MDNS_LOG_WARN, MDNS_LOG_INFO, MDNS_LOG_DEBUG 43 44 Revision 1.10 2007/06/15 21:54:51 cheshire 45 <rdar://problem/4883206> Add packet logging to help debugging private browsing over TLS 46 47 Revision 1.9 2007/04/05 19:52:32 cheshire 48 Display correct ident in syslog messages (i.e. in dnsextd, ProgramName is not "mDNSResponder") 49 50 Revision 1.8 2007/01/20 01:43:27 cheshire 51 <rdar://problem/4058383> Should not write log messages to /dev/console 52 53 Revision 1.7 2006/08/14 23:24:56 cheshire 54 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0 55 56 Revision 1.6 2005/01/27 22:57:56 cheshire 57 Fix compile errors on gcc4 58 59 Revision 1.5 2004/09/17 01:08:55 cheshire 60 Renamed mDNSClientAPI.h to mDNSEmbeddedAPI.h 61 The name "mDNSClientAPI.h" is misleading to new developers looking at this code. The interfaces 62 declared in that file are ONLY appropriate to single-address-space embedded applications. 63 For clients on general-purpose computers, the interfaces defined in dns_sd.h should be used. 64 65 Revision 1.4 2004/06/11 22:36:51 cheshire 66 Fixes for compatibility with Windows 67 68 Revision 1.3 2004/01/28 21:14:23 cheshire 69 Reconcile debug_mode and gDebugLogging into a single flag (mDNS_DebugMode) 70 71 Revision 1.2 2003/12/09 01:30:40 rpantos 72 Fix usage of ARGS... macros to build properly on Windows. 73 74 Revision 1.1 2003/12/08 21:11:42; rpantos 75 Changes necessary to support mDNSResponder on Linux. 76 77 */ 78 79 #include "mDNSDebug.h" 80 81 #include <stdio.h> 82 83 #if defined(WIN32) || defined(EFI32) || defined(EFI64) || defined(EFIX64) 84 // Need to add Windows/EFI syslog support here 85 #define LOG_PID 0x01 86 #define LOG_CONS 0x02 87 #define LOG_PERROR 0x20 88 #else 89 #include <syslog.h> 90 #endif 91 92 #include "mDNSEmbeddedAPI.h" 93 94 mDNSexport int mDNS_LoggingEnabled = 0; 95 mDNSexport int mDNS_PacketLoggingEnabled = 0; 96 97 #if MDNS_DEBUGMSGS 98 mDNSexport int mDNS_DebugMode = mDNStrue; 99 #else 100 mDNSexport int mDNS_DebugMode = mDNSfalse; 101 #endif 102 103 // Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows 104 // how to print special data types like IP addresses and length-prefixed domain names 105 #if MDNS_DEBUGMSGS > 1 106 mDNSexport void verbosedebugf_(const char *format, ...) 107 { 108 char buffer[512]; 109 va_list ptr; 110 va_start(ptr,format); 111 buffer[mDNS_vsnprintf(buffer, sizeof(buffer), format, ptr)] = 0; 112 va_end(ptr); 113 mDNSPlatformWriteDebugMsg(buffer); 114 } 115 #endif 116 117 // Log message with default "mDNSResponder" ident string at the start 118 mDNSlocal void LogMsgWithLevelv(mDNSLogLevel_t logLevel, const char *format, va_list ptr) 119 { 120 char buffer[512]; 121 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0; 122 mDNSPlatformWriteLogMsg(ProgramName, buffer, logLevel); 123 } 124 125 #define LOG_HELPER_BODY(L) \ 126 { \ 127 va_list ptr; \ 128 va_start(ptr,format); \ 129 LogMsgWithLevelv(L, format, ptr); \ 130 va_end(ptr); \ 131 } 132 133 // see mDNSDebug.h 134 #if !MDNS_HAS_VA_ARG_MACROS 135 void debug_noop(const char *format, ...) { (void) format; } 136 void LogMsg_(const char *format, ...) LOG_HELPER_BODY(MDNS_LOG_MSG) 137 void LogOperation_(const char *format, ...) LOG_HELPER_BODY(MDNS_LOG_OPERATION) 138 void LogSPS_(const char *format, ...) LOG_HELPER_BODY(MDNS_LOG_SPS) 139 void LogInfo_(const char *format, ...) LOG_HELPER_BODY(MDNS_LOG_INFO) 140 #endif 141 142 #if MDNS_DEBUGMSGS 143 void debugf_(const char *format, ...) LOG_HELPER_BODY(MDNS_LOG_DEBUG) 144 #endif 145 146 // Log message with default "mDNSResponder" ident string at the start 147 mDNSexport void LogMsgWithLevel(mDNSLogLevel_t logLevel, const char *format, ...) 148 LOG_HELPER_BODY(logLevel) 149