xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/mdnsd/mDNSDebug.c (revision 4904:cd464a980538)
1*4904Srs200217 /* -*- Mode: C; tab-width: 4 -*-
2*4904Srs200217  *
3*4904Srs200217  * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4*4904Srs200217  *
5*4904Srs200217  * Licensed under the Apache License, Version 2.0 (the "License");
6*4904Srs200217  * you may not use this file except in compliance with the License.
7*4904Srs200217  * You may obtain a copy of the License at
8*4904Srs200217  *
9*4904Srs200217  *     http://www.apache.org/licenses/LICENSE-2.0
10*4904Srs200217  *
11*4904Srs200217  * Unless required by applicable law or agreed to in writing, software
12*4904Srs200217  * distributed under the License is distributed on an "AS IS" BASIS,
13*4904Srs200217  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*4904Srs200217  * See the License for the specific language governing permissions and
15*4904Srs200217  * limitations under the License.
16*4904Srs200217 
17*4904Srs200217  	File:		mDNSDebug.c
18*4904Srs200217 
19*4904Srs200217  	Contains:	Implementation of debugging utilities. Requires a POSIX environment.
20*4904Srs200217 
21*4904Srs200217  	Version:	1.0
22*4904Srs200217 
23*4904Srs200217     Change History (most recent first):
24*4904Srs200217 
25*4904Srs200217 $Log: mDNSDebug.c,v $
26*4904Srs200217 Revision 1.7  2006/08/14 23:24:56  cheshire
27*4904Srs200217 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
28*4904Srs200217 
29*4904Srs200217 Revision 1.6  2005/01/27 22:57:56  cheshire
30*4904Srs200217 Fix compile errors on gcc4
31*4904Srs200217 
32*4904Srs200217 Revision 1.5  2004/09/17 01:08:55  cheshire
33*4904Srs200217 Renamed mDNSClientAPI.h to mDNSEmbeddedAPI.h
34*4904Srs200217   The name "mDNSClientAPI.h" is misleading to new developers looking at this code. The interfaces
35*4904Srs200217   declared in that file are ONLY appropriate to single-address-space embedded applications.
36*4904Srs200217   For clients on general-purpose computers, the interfaces defined in dns_sd.h should be used.
37*4904Srs200217 
38*4904Srs200217 Revision 1.4  2004/06/11 22:36:51  cheshire
39*4904Srs200217 Fixes for compatibility with Windows
40*4904Srs200217 
41*4904Srs200217 Revision 1.3  2004/01/28 21:14:23  cheshire
42*4904Srs200217 Reconcile debug_mode and gDebugLogging into a single flag (mDNS_DebugMode)
43*4904Srs200217 
44*4904Srs200217 Revision 1.2  2003/12/09 01:30:40  rpantos
45*4904Srs200217 Fix usage of ARGS... macros to build properly on Windows.
46*4904Srs200217 
47*4904Srs200217 Revision 1.1  2003/12/08 21:11:42;  rpantos
48*4904Srs200217 Changes necessary to support mDNSResponder on Linux.
49*4904Srs200217 
50*4904Srs200217 */
51*4904Srs200217 
52*4904Srs200217 #pragma ident	"%Z%%M%	%I%	%E% SMI"
53*4904Srs200217 
54*4904Srs200217 #include "mDNSDebug.h"
55*4904Srs200217 
56*4904Srs200217 #include <stdio.h>
57*4904Srs200217 
58*4904Srs200217 #if defined(WIN32)
59*4904Srs200217 // Need to add Windows syslog support here
60*4904Srs200217 #define LOG_PID 0x01
61*4904Srs200217 #define LOG_CONS 0x02
62*4904Srs200217 #define LOG_PERROR 0x20
63*4904Srs200217 #define openlog(A,B,C) (void)(A); (void)(B)
64*4904Srs200217 #define syslog(A,B,C)
65*4904Srs200217 #define closelog()
66*4904Srs200217 #else
67*4904Srs200217 #include <syslog.h>
68*4904Srs200217 #endif
69*4904Srs200217 
70*4904Srs200217 #include "mDNSEmbeddedAPI.h"
71*4904Srs200217 
72*4904Srs200217 #if MDNS_DEBUGMSGS
73*4904Srs200217 mDNSexport int mDNS_DebugMode = mDNStrue;
74*4904Srs200217 #else
75*4904Srs200217 mDNSexport int mDNS_DebugMode = mDNSfalse;
76*4904Srs200217 #endif
77*4904Srs200217 
78*4904Srs200217 // Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows
79*4904Srs200217 // how to print special data types like IP addresses and length-prefixed domain names
80*4904Srs200217 #if MDNS_DEBUGMSGS
debugf_(const char * format,...)81*4904Srs200217 mDNSexport void debugf_(const char *format, ...)
82*4904Srs200217 	{
83*4904Srs200217 	unsigned char buffer[512];
84*4904Srs200217 	va_list ptr;
85*4904Srs200217 	va_start(ptr,format);
86*4904Srs200217 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
87*4904Srs200217 	va_end(ptr);
88*4904Srs200217 	fprintf(stderr,"%s\n", buffer);
89*4904Srs200217 	fflush(stderr);
90*4904Srs200217 	}
91*4904Srs200217 #endif
92*4904Srs200217 
93*4904Srs200217 #if MDNS_DEBUGMSGS > 1
verbosedebugf_(const char * format,...)94*4904Srs200217 mDNSexport void verbosedebugf_(const char *format, ...)
95*4904Srs200217 	{
96*4904Srs200217 	unsigned char buffer[512];
97*4904Srs200217 	va_list ptr;
98*4904Srs200217 	va_start(ptr,format);
99*4904Srs200217 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
100*4904Srs200217 	va_end(ptr);
101*4904Srs200217 	fprintf(stderr,"%s\n", buffer);
102*4904Srs200217 	fflush(stderr);
103*4904Srs200217 	}
104*4904Srs200217 #endif
105*4904Srs200217 
WriteLogMsg(const char * ident,const char * buffer,int logoptflags,int logpriority)106*4904Srs200217 mDNSlocal void WriteLogMsg(const char *ident, const char *buffer, int logoptflags, int logpriority)
107*4904Srs200217 	{
108*4904Srs200217 	if (mDNS_DebugMode)	// In debug mode we write to stderr
109*4904Srs200217 		{
110*4904Srs200217 		fprintf(stderr,"%s\n", buffer);
111*4904Srs200217 		fflush(stderr);
112*4904Srs200217 		}
113*4904Srs200217 	else				// else, in production mode, we write to syslog
114*4904Srs200217 		{
115*4904Srs200217 		openlog(ident, LOG_PERROR | logoptflags, LOG_DAEMON);
116*4904Srs200217 		syslog(logpriority, "%s", buffer);
117*4904Srs200217 		closelog();
118*4904Srs200217 		}
119*4904Srs200217 	}
120*4904Srs200217 
121*4904Srs200217 // Log message with default "mDNSResponder" ident string at the start
LogMsg(const char * format,...)122*4904Srs200217 mDNSexport void LogMsg(const char *format, ...)
123*4904Srs200217 	{
124*4904Srs200217 	char buffer[512];
125*4904Srs200217 	va_list ptr;
126*4904Srs200217 	va_start(ptr,format);
127*4904Srs200217 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
128*4904Srs200217 	va_end(ptr);
129*4904Srs200217 	WriteLogMsg("mDNSResponder", buffer, 0, LOG_ERR);
130*4904Srs200217 	}
131*4904Srs200217 
132*4904Srs200217 // Log message with specified ident string at the start
LogMsgIdent(const char * ident,const char * format,...)133*4904Srs200217 mDNSexport void LogMsgIdent(const char *ident, const char *format, ...)
134*4904Srs200217 	{
135*4904Srs200217 	char buffer[512];
136*4904Srs200217 	va_list ptr;
137*4904Srs200217 	va_start(ptr,format);
138*4904Srs200217 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
139*4904Srs200217 	va_end(ptr);
140*4904Srs200217 	WriteLogMsg(ident, buffer, ident && *ident ? LOG_PID : 0, LOG_INFO);
141*4904Srs200217 	}
142*4904Srs200217 
143*4904Srs200217 // Log message with no ident string at the start
LogMsgNoIdent(const char * format,...)144*4904Srs200217 mDNSexport void LogMsgNoIdent(const char *format, ...)
145*4904Srs200217 	{
146*4904Srs200217 	char buffer[512];
147*4904Srs200217 	va_list ptr;
148*4904Srs200217 	va_start(ptr,format);
149*4904Srs200217 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
150*4904Srs200217 	va_end(ptr);
151*4904Srs200217 	WriteLogMsg("", buffer, 0, LOG_INFO);
152*4904Srs200217 	}
153