]> git.saurik.com Git - apple/mdnsresponder.git/blame - mDNSShared/mDNSDebug.c
mDNSResponder-1310.80.1.tar.gz
[apple/mdnsresponder.git] / mDNSShared / mDNSDebug.c
CommitLineData
f0cc3e7b
A
1/*
2 * Copyright (c) 2003-2019 Apple Inc. All rights reserved.
8e92c31c 3 *
67c8f8a1
A
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
83fb1e36 7 *
67c8f8a1 8 * http://www.apache.org/licenses/LICENSE-2.0
83fb1e36 9 *
67c8f8a1
A
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
8e92c31c 14 * limitations under the License.
263eeeab 15 */
8e92c31c 16
8e92c31c 17#include <stdio.h>
7f0064bd 18
030b743d
A
19#if defined(WIN32) || defined(EFI32) || defined(EFI64) || defined(EFIX64)
20// Need to add Windows/EFI syslog support here
7f0064bd
A
21#define LOG_PID 0x01
22#define LOG_CONS 0x02
23#define LOG_PERROR 0x20
7f0064bd 24#else
8e92c31c 25#include <syslog.h>
7f0064bd 26#endif
8e92c31c 27
7f0064bd 28#include "mDNSEmbeddedAPI.h"
8e92c31c 29
51601d48 30mDNSexport int mDNS_LoggingEnabled = 0;
32bb7e43 31mDNSexport int mDNS_PacketLoggingEnabled = 0;
51601d48
A
32mDNSexport int mDNS_McastLoggingEnabled = 0;
33mDNSexport int mDNS_McastTracingEnabled = 0;
67c8f8a1 34
8e92c31c
A
35#if MDNS_DEBUGMSGS
36mDNSexport int mDNS_DebugMode = mDNStrue;
37#else
38mDNSexport int mDNS_DebugMode = mDNSfalse;
39#endif
40
41// Note, this uses mDNS_vsnprintf instead of standard "vsnprintf", because mDNS_vsnprintf knows
42// how to print special data types like IP addresses and length-prefixed domain names
8e92c31c
A
43#if MDNS_DEBUGMSGS > 1
44mDNSexport void verbosedebugf_(const char *format, ...)
83fb1e36
A
45{
46 char buffer[512];
f0cc3e7b
A
47 va_list args;
48 va_start(args, format);
49 buffer[mDNS_vsnprintf(buffer, sizeof(buffer), format, args)] = 0;
50 va_end(args);
83fb1e36
A
51 mDNSPlatformWriteDebugMsg(buffer);
52}
8e92c31c
A
53#endif
54
7f0064bd 55// Log message with default "mDNSResponder" ident string at the start
f0cc3e7b
A
56#if MDNSRESPONDER_SUPPORTS(APPLE, OS_LOG)
57mDNSlocal void LogMsgWithLevelv(os_log_t category, os_log_type_t level, const char *format, va_list args)
58{
59 char buffer[512];
60 mDNS_vsnprintf(buffer, (mDNSu32)sizeof(buffer), format, args);
61 os_log_with_type(category ? category : mDNSLogCategory_Default, level, "%{private}s", buffer);
62}
63#else
64mDNSlocal void LogMsgWithLevelv(const char *category, mDNSLogLevel_t level, const char *format, va_list args)
83fb1e36
A
65{
66 char buffer[512];
f0cc3e7b
A
67 char *dst = buffer;
68 const char *const lim = &buffer[512];
69 if (category) mDNS_snprintf_add(&dst, lim, "%s: ", category);
70 mDNS_vsnprintf(dst, (mDNSu32)(lim - dst), format, args);
71 mDNSPlatformWriteLogMsg(ProgramName, buffer, level);
83fb1e36 72}
f0cc3e7b 73#endif
8e92c31c 74
f0cc3e7b 75#define LOG_HELPER_BODY(CATEGORY, LEVEL) \
83fb1e36 76 { \
f0cc3e7b
A
77 va_list args; \
78 va_start(args,format); \
79 LogMsgWithLevelv(CATEGORY, LEVEL, format, args); \
80 va_end(args); \
83fb1e36 81 }
8e92c31c 82
32bb7e43
A
83// see mDNSDebug.h
84#if !MDNS_HAS_VA_ARG_MACROS
f0cc3e7b
A
85void LogMsg_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_INFO)
86void LogOperation_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_INFO)
87void LogSPS_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_INFO)
88void LogInfo_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_INFO)
89void LogDebug_(const char *format, ...) LOG_HELPER_BODY(NULL, MDNS_LOG_DEBUG)
32bb7e43 90#endif
67c8f8a1 91
32bb7e43 92#if MDNS_DEBUGMSGS
263eeeab 93void debugf_(const char *format, ...) LOG_HELPER_BODY(MDNS_LOG_DEBUG)
32bb7e43 94#endif
67c8f8a1 95
32bb7e43 96// Log message with default "mDNSResponder" ident string at the start
f0cc3e7b
A
97mDNSexport void LogMsgWithLevel(mDNSLogCategory_t category, mDNSLogLevel_t level, const char *format, ...)
98LOG_HELPER_BODY(category, level)
99
100mDNSexport void LogToFD(int fd, const char *format, ...)
101{
102 va_list args;
103 va_start(args, format);
104#if APPLE_OSX_mDNSResponder
105 char buffer[1024];
106 buffer[mDNS_vsnprintf(buffer, (mDNSu32)sizeof(buffer), format, args)] = '\0';
107 dprintf(fd, "%s\n", buffer);
108#else
109 (void)fd;
110 LogMsgWithLevelv(NULL, MDNS_LOG_INFO, format, args);
111#endif
112 va_end(args);
113}