xref: /titanic_50/usr/src/cmd/cmd-inet/usr.lib/mdnsd/mDNSDebug.c (revision bfed486ad8de8b8ebc6345a8e10accae08bf2f45)
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.7  2006/08/14 23:24:56  cheshire
27 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
28 
29 Revision 1.6  2005/01/27 22:57:56  cheshire
30 Fix compile errors on gcc4
31 
32 Revision 1.5  2004/09/17 01:08:55  cheshire
33 Renamed mDNSClientAPI.h to mDNSEmbeddedAPI.h
34   The name "mDNSClientAPI.h" is misleading to new developers looking at this code. The interfaces
35   declared in that file are ONLY appropriate to single-address-space embedded applications.
36   For clients on general-purpose computers, the interfaces defined in dns_sd.h should be used.
37 
38 Revision 1.4  2004/06/11 22:36:51  cheshire
39 Fixes for compatibility with Windows
40 
41 Revision 1.3  2004/01/28 21:14:23  cheshire
42 Reconcile debug_mode and gDebugLogging into a single flag (mDNS_DebugMode)
43 
44 Revision 1.2  2003/12/09 01:30:40  rpantos
45 Fix usage of ARGS... macros to build properly on Windows.
46 
47 Revision 1.1  2003/12/08 21:11:42;  rpantos
48 Changes necessary to support mDNSResponder on Linux.
49 
50 */
51 
52 #pragma ident	"%Z%%M%	%I%	%E% SMI"
53 
54 #include "mDNSDebug.h"
55 
56 #include <stdio.h>
57 
58 #if defined(WIN32)
59 // Need to add Windows syslog support here
60 #define LOG_PID 0x01
61 #define LOG_CONS 0x02
62 #define LOG_PERROR 0x20
63 #define openlog(A,B,C) (void)(A); (void)(B)
64 #define syslog(A,B,C)
65 #define closelog()
66 #else
67 #include <syslog.h>
68 #endif
69 
70 #include "mDNSEmbeddedAPI.h"
71 
72 #if MDNS_DEBUGMSGS
73 mDNSexport int mDNS_DebugMode = mDNStrue;
74 #else
75 mDNSexport int mDNS_DebugMode = mDNSfalse;
76 #endif
77 
78 // Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows
79 // how to print special data types like IP addresses and length-prefixed domain names
80 #if MDNS_DEBUGMSGS
81 mDNSexport void debugf_(const char *format, ...)
82 	{
83 	unsigned char buffer[512];
84 	va_list ptr;
85 	va_start(ptr,format);
86 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
87 	va_end(ptr);
88 	fprintf(stderr,"%s\n", buffer);
89 	fflush(stderr);
90 	}
91 #endif
92 
93 #if MDNS_DEBUGMSGS > 1
94 mDNSexport void verbosedebugf_(const char *format, ...)
95 	{
96 	unsigned char buffer[512];
97 	va_list ptr;
98 	va_start(ptr,format);
99 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
100 	va_end(ptr);
101 	fprintf(stderr,"%s\n", buffer);
102 	fflush(stderr);
103 	}
104 #endif
105 
106 mDNSlocal void WriteLogMsg(const char *ident, const char *buffer, int logoptflags, int logpriority)
107 	{
108 	if (mDNS_DebugMode)	// In debug mode we write to stderr
109 		{
110 		fprintf(stderr,"%s\n", buffer);
111 		fflush(stderr);
112 		}
113 	else				// else, in production mode, we write to syslog
114 		{
115 		openlog(ident, LOG_PERROR | logoptflags, LOG_DAEMON);
116 		syslog(logpriority, "%s", buffer);
117 		closelog();
118 		}
119 	}
120 
121 // Log message with default "mDNSResponder" ident string at the start
122 mDNSexport void LogMsg(const char *format, ...)
123 	{
124 	char buffer[512];
125 	va_list ptr;
126 	va_start(ptr,format);
127 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
128 	va_end(ptr);
129 	WriteLogMsg("mDNSResponder", buffer, 0, LOG_ERR);
130 	}
131 
132 // Log message with specified ident string at the start
133 mDNSexport void LogMsgIdent(const char *ident, const char *format, ...)
134 	{
135 	char buffer[512];
136 	va_list ptr;
137 	va_start(ptr,format);
138 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
139 	va_end(ptr);
140 	WriteLogMsg(ident, buffer, ident && *ident ? LOG_PID : 0, LOG_INFO);
141 	}
142 
143 // Log message with no ident string at the start
144 mDNSexport void LogMsgNoIdent(const char *format, ...)
145 	{
146 	char buffer[512];
147 	va_list ptr;
148 	va_start(ptr,format);
149 	buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
150 	va_end(ptr);
151 	WriteLogMsg("", buffer, 0, LOG_INFO);
152 	}
153