]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/mDNSDebug.c
mDNSResponder-164.tar.gz
[apple/mdnsresponder.git] / mDNSShared / mDNSDebug.c
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.12 2007/10/01 19:06:19 cheshire
27 Defined symbolic constant MDNS_LOG_INITIAL_LEVEL to set the logging level we start out at
28
29 Revision 1.11 2007/07/27 20:19:56 cheshire
30 For now, comment out unused log levels MDNS_LOG_ERROR, MDNS_LOG_WARN, MDNS_LOG_INFO, MDNS_LOG_DEBUG
31
32 Revision 1.10 2007/06/15 21:54:51 cheshire
33 <rdar://problem/4883206> Add packet logging to help debugging private browsing over TLS
34
35 Revision 1.9 2007/04/05 19:52:32 cheshire
36 Display correct ident in syslog messages (i.e. in dnsextd, ProgramName is not "mDNSResponder")
37
38 Revision 1.8 2007/01/20 01:43:27 cheshire
39 <rdar://problem/4058383> Should not write log messages to /dev/console
40
41 Revision 1.7 2006/08/14 23:24:56 cheshire
42 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
43
44 Revision 1.6 2005/01/27 22:57:56 cheshire
45 Fix compile errors on gcc4
46
47 Revision 1.5 2004/09/17 01:08:55 cheshire
48 Renamed mDNSClientAPI.h to mDNSEmbeddedAPI.h
49 The name "mDNSClientAPI.h" is misleading to new developers looking at this code. The interfaces
50 declared in that file are ONLY appropriate to single-address-space embedded applications.
51 For clients on general-purpose computers, the interfaces defined in dns_sd.h should be used.
52
53 Revision 1.4 2004/06/11 22:36:51 cheshire
54 Fixes for compatibility with Windows
55
56 Revision 1.3 2004/01/28 21:14:23 cheshire
57 Reconcile debug_mode and gDebugLogging into a single flag (mDNS_DebugMode)
58
59 Revision 1.2 2003/12/09 01:30:40 rpantos
60 Fix usage of ARGS... macros to build properly on Windows.
61
62 Revision 1.1 2003/12/08 21:11:42; rpantos
63 Changes necessary to support mDNSResponder on Linux.
64
65 */
66
67 #include "mDNSDebug.h"
68
69 #include <stdio.h>
70
71 #if defined(WIN32)
72 // Need to add Windows syslog support here
73 #define LOG_PID 0x01
74 #define LOG_CONS 0x02
75 #define LOG_PERROR 0x20
76 #define openlog(A,B,C) (void)(A); (void)(B)
77 #define syslog(A,B,C)
78 #define closelog()
79 #else
80 #include <syslog.h>
81 #endif
82
83 #include "mDNSEmbeddedAPI.h"
84
85 mDNSexport LogLevel_t mDNS_LogLevel = MDNS_LOG_INITIAL_LEVEL;
86
87 #if MDNS_DEBUGMSGS
88 mDNSexport int mDNS_DebugMode = mDNStrue;
89 #else
90 mDNSexport int mDNS_DebugMode = mDNSfalse;
91 #endif
92
93 // Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows
94 // how to print special data types like IP addresses and length-prefixed domain names
95 #if MDNS_DEBUGMSGS
96 mDNSexport void debugf_(const char *format, ...)
97 {
98 unsigned char buffer[512];
99 va_list ptr;
100 va_start(ptr,format);
101 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
102 va_end(ptr);
103 fprintf(stderr,"%s\n", buffer);
104 fflush(stderr);
105 }
106 #endif
107
108 #if MDNS_DEBUGMSGS > 1
109 mDNSexport void verbosedebugf_(const char *format, ...)
110 {
111 unsigned char buffer[512];
112 va_list ptr;
113 va_start(ptr,format);
114 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
115 va_end(ptr);
116 fprintf(stderr,"%s\n", buffer);
117 fflush(stderr);
118 }
119 #endif
120
121 mDNSlocal void WriteLogMsg(const char *ident, const char *buffer, int logoptflags)
122 {
123 if (mDNS_DebugMode) // In debug mode we write to stderr
124 {
125 fprintf(stderr,"%s\n", buffer);
126 fflush(stderr);
127 }
128 else // else, in production mode, we write to syslog
129 {
130 openlog(ident, LOG_CONS | logoptflags, LOG_DAEMON);
131 syslog(LOG_ERR, "%s", buffer);
132 closelog();
133 }
134 }
135
136 // Log message with default "mDNSResponder" ident string at the start
137 mDNSexport void LogMsg(const char *format, ...)
138 {
139 char buffer[512];
140 va_list ptr;
141 va_start(ptr,format);
142 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
143 va_end(ptr);
144 WriteLogMsg(ProgramName, buffer, 0);
145 }
146
147 // Log message with specified ident string at the start
148 mDNSexport void LogMsgIdent(const char *ident, const char *format, ...)
149 {
150 char buffer[512];
151 va_list ptr;
152 va_start(ptr,format);
153 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
154 va_end(ptr);
155 WriteLogMsg(ident, buffer, ident && *ident ? LOG_PID : 0);
156 }
157
158 // Log message with no ident string at the start
159 mDNSexport void LogMsgNoIdent(const char *format, ...)
160 {
161 char buffer[512];
162 va_list ptr;
163 va_start(ptr,format);
164 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
165 va_end(ptr);
166 WriteLogMsg("", buffer, 0);
167 }
168
169 mDNSlocal const char *CStringForLogLevel(LogLevel_t level)
170 {
171 switch (level)
172 {
173 case MDNS_LOG_NONE: return "MDNS_LOG_NONE";
174 // case MDNS_LOG_ERROR: return "MDNS_LOG_ERROR";
175 // case MDNS_LOG_WARN: return "MDNS_LOG_WARN";
176 // case MDNS_LOG_INFO: return "MDNS_LOG_INFO";
177 // case MDNS_LOG_DEBUG: return "MDNS_LOG_DEBUG";
178 case MDNS_LOG_VERBOSE_DEBUG: return "MDNS_LOG_VERBOSE_DEBUG";
179 default: return "MDNS_LOG_UNKNOWN";
180 }
181 }
182
183 mDNSexport void SigLogLevel(void)
184 {
185 if (mDNS_LogLevel < MDNS_LOG_VERBOSE_DEBUG) mDNS_LogLevel++;
186 else mDNS_LogLevel = MDNS_LOG_NONE;
187 LogMsg("Log Level Changed to %s", CStringForLogLevel(mDNS_LogLevel));
188 }