]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/mDNSDebug.c
mDNSResponder-107.4.tar.gz
[apple/mdnsresponder.git] / mDNSShared / mDNSDebug.c
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22
23 File: mDNSDebug.c
24
25 Contains: Implementation of debugging utilities. Requires a POSIX environment.
26
27 Version: 1.0
28
29 Change History (most recent first):
30
31 $Log: mDNSDebug.c,v $
32 Revision 1.6 2005/01/27 22:57:56 cheshire
33 Fix compile errors on gcc4
34
35 Revision 1.5 2004/09/17 01:08:55 cheshire
36 Renamed mDNSClientAPI.h to mDNSEmbeddedAPI.h
37 The name "mDNSClientAPI.h" is misleading to new developers looking at this code. The interfaces
38 declared in that file are ONLY appropriate to single-address-space embedded applications.
39 For clients on general-purpose computers, the interfaces defined in dns_sd.h should be used.
40
41 Revision 1.4 2004/06/11 22:36:51 cheshire
42 Fixes for compatibility with Windows
43
44 Revision 1.3 2004/01/28 21:14:23 cheshire
45 Reconcile debug_mode and gDebugLogging into a single flag (mDNS_DebugMode)
46
47 Revision 1.2 2003/12/09 01:30:40 rpantos
48 Fix usage of ARGS... macros to build properly on Windows.
49
50 Revision 1.1 2003/12/08 21:11:42; rpantos
51 Changes necessary to support mDNSResponder on Linux.
52
53 */
54
55 #include "mDNSDebug.h"
56
57 #include <stdio.h>
58
59 #if defined(WIN32)
60 // Need to add Windows syslog support here
61 #define LOG_PID 0x01
62 #define LOG_CONS 0x02
63 #define LOG_PERROR 0x20
64 #define openlog(A,B,C) (void)(A); (void)(B)
65 #define syslog(A,B,C)
66 #define closelog()
67 #else
68 #include <syslog.h>
69 #endif
70
71 #include "mDNSEmbeddedAPI.h"
72
73 #if MDNS_DEBUGMSGS
74 mDNSexport int mDNS_DebugMode = mDNStrue;
75 #else
76 mDNSexport int mDNS_DebugMode = mDNSfalse;
77 #endif
78
79 // Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows
80 // how to print special data types like IP addresses and length-prefixed domain names
81 #if MDNS_DEBUGMSGS
82 mDNSexport void debugf_(const char *format, ...)
83 {
84 unsigned char buffer[512];
85 va_list ptr;
86 va_start(ptr,format);
87 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
88 va_end(ptr);
89 fprintf(stderr,"%s\n", buffer);
90 fflush(stderr);
91 }
92 #endif
93
94 #if MDNS_DEBUGMSGS > 1
95 mDNSexport void verbosedebugf_(const char *format, ...)
96 {
97 unsigned char buffer[512];
98 va_list ptr;
99 va_start(ptr,format);
100 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
101 va_end(ptr);
102 fprintf(stderr,"%s\n", buffer);
103 fflush(stderr);
104 }
105 #endif
106
107 mDNSlocal void WriteLogMsg(const char *ident, const char *buffer, int logoptflags)
108 {
109 if (mDNS_DebugMode) // In debug mode we write to stderr
110 {
111 fprintf(stderr,"%s\n", buffer);
112 fflush(stderr);
113 }
114 else // else, in production mode, we write to syslog
115 {
116 openlog(ident, LOG_CONS | LOG_PERROR | logoptflags, LOG_DAEMON);
117 syslog(LOG_ERR, "%s", buffer);
118 closelog();
119 }
120 }
121
122 // Log message with default "mDNSResponder" ident string at the start
123 mDNSexport void LogMsg(const char *format, ...)
124 {
125 char buffer[512];
126 va_list ptr;
127 va_start(ptr,format);
128 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
129 va_end(ptr);
130 WriteLogMsg("mDNSResponder", buffer, 0);
131 }
132
133 // Log message with specified ident string at the start
134 mDNSexport void LogMsgIdent(const char *ident, const char *format, ...)
135 {
136 char buffer[512];
137 va_list ptr;
138 va_start(ptr,format);
139 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
140 va_end(ptr);
141 WriteLogMsg(ident, buffer, ident && *ident ? LOG_PID : 0);
142 }
143
144 // Log message with no ident string at the start
145 mDNSexport void LogMsgNoIdent(const char *format, ...)
146 {
147 char buffer[512];
148 va_list ptr;
149 va_start(ptr,format);
150 buffer[mDNS_vsnprintf((char *)buffer, sizeof(buffer), format, ptr)] = 0;
151 va_end(ptr);
152 WriteLogMsg("", buffer, 0);
153 }