1 /* $Id$ */
2 /*
3 ** Copyright (C) 2002-2009 Sourcefire, Inc.
4 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License Version 2 as
8 ** published by the Free Software Foundation.  You may not use, modify or
9 ** distribute this program under any other version of the GNU General
10 ** Public License.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <syslog.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <wchar.h>
32 
33 #include "debug.h"
34 
35 #include "barnyard2.h"
36 
37 
38 
39 #ifdef DEBUG
40 int debuglevel = DEBUG_ALL;
41 char *DebugMessageFile = NULL;
42 int DebugMessageLine = 0;
43 
44 extern Barnyard2Config *barnyard2_conf;
45 
DebugThis(int level)46 int DebugThis(int level)
47 {
48     if (!(level & GetDebugLevel()))
49         return 0;
50 
51     return 1;
52 }
53 
GetDebugLevel(void)54 int GetDebugLevel(void)
55 {
56     static int debug_init = 0;
57     static unsigned int debug_level = 0;
58 
59     // declared here for compatibility with older compilers
60     // not initialized here cuz the next step is done once
61     const char* key;
62 
63     if (debug_init)
64         return debug_level;
65 
66     key = getenv(DEBUG_VARIABLE);
67 
68     if ( key )
69         debug_level = strtoul(key, NULL, 0);
70     else
71         debug_level = 0;
72 
73     debug_init = 1;
74     return debug_level;
75 }
76 
DebugMessageFunc(int level,char * fmt,...)77 void DebugMessageFunc(int level, char *fmt, ...)
78 {
79     va_list ap;
80 
81     if (!(level & GetDebugLevel()))
82         return;
83 
84     va_start(ap, fmt);
85 
86     if ((barnyard2_conf != NULL) && BcDaemonMode())
87     {
88         char buf[STD_BUF];
89         int buf_len = sizeof(buf);
90         char *buf_ptr = buf;
91 
92         buf[buf_len - 1] = '\0';
93 
94         /* filename and line number information */
95         if (DebugMessageFile != NULL)
96         {
97             snprintf(buf, buf_len - 1, "%s:%d: ",
98                     DebugMessageFile, DebugMessageLine);
99             buf_ptr += strlen(buf);
100             buf_len -= strlen(buf);
101         }
102 
103         vsnprintf(buf_ptr, buf_len - 1, fmt, ap);
104         syslog(LOG_DAEMON | LOG_DEBUG, "%s", buf);
105     }
106     else
107     {
108         if (DebugMessageFile != NULL)
109             printf("%s:%d: ", DebugMessageFile, DebugMessageLine);
110         vprintf(fmt, ap);
111     }
112 
113     va_end(ap);
114 }
115 
116 #ifdef HAVE_WCHAR_H
DebugWideMessageFunc(int level,wchar_t * fmt,...)117 void DebugWideMessageFunc(int level, wchar_t *fmt, ...)
118 {
119     va_list ap;
120     wchar_t buf[STD_BUF+1];
121 
122 
123     if (!(level & GetDebugLevel()))
124     {
125         return;
126     }
127     buf[STD_BUF]= (wchar_t)0;
128 
129     /* filename and line number information */
130     if (DebugMessageFile != NULL)
131         printf("%s:%d: ", DebugMessageFile, DebugMessageLine);
132 
133     va_start(ap, fmt);
134 
135     if(BcDaemonMode())
136     {
137 #if  defined(WIN32) && (defined(__USE_ISOC95) || defined(__USE_UNIX98))
138         _vsnwprintf(buf, STD_BUF, fmt, ap);
139 #else
140 #if  defined(HAVE_VSWPRINTF) && (defined(__USE_ISOC95) || defined(__USE_UNIX98))
141 
142         vswprintf(buf, STD_BUF, fmt, ap);
143 #endif
144 #endif
145         //syslog(LOG_DAEMON | LOG_DEBUG, "%s", buf);
146     }
147     else
148     {
149 #if   defined(HAVE_WPRINTF) && (defined(__USE_ISOC95) || defined(__USE_UNIX98))
150         vwprintf(fmt, ap);
151 #endif
152     }
153 
154     va_end(ap);
155 }
156 #endif
157 #else
DebugMessageFunc(int level,char * fmt,...)158 void DebugMessageFunc(int level, char *fmt, ...)
159 {
160 }
161 #ifdef HAVE_WCHAR_H
DebugWideMessageFunc(int level,wchar_t * fmt,...)162 void DebugWideMessageFunc(int level, wchar_t *fmt, ...)
163 {
164 }
165 #endif
166 #endif /* DEBUG */
167