Imported Upstream version 2.5.1
[ossec-hids.git] / src / shared / debug_op.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13 #include "headers/shared.h"
14
15
16 int dbg_flag = 0;
17 int chroot_flag = 0;
18 int daemon_flag = 0;
19
20 #ifdef WIN32
21 void WinSetError();
22 #endif
23
24 /* For internal logs */
25 #ifndef LOGFILE
26   #ifndef WIN32
27     #define LOGFILE   "/logs/ossec.log"
28   #else
29     #define LOGFILE "ossec.log"
30   #endif
31 #endif
32
33
34 /* _log function */
35 void _log(const char * msg,va_list args)
36 {
37     time_t tm;
38     struct tm *p;
39
40     /* For the stderr print */
41     va_list args2;
42
43     FILE *fp;
44     
45     tm = time(NULL);
46     p = localtime(&tm);
47
48     /* Duplicating args */
49     va_copy(args2, args);
50     
51
52     /* If under chroot, log directly to /logs/ossec.log */
53     if(chroot_flag == 1)
54     {
55         fp = fopen(LOGFILE, "a");
56     }
57     else
58     {
59         char _logfile[256];
60         #ifndef WIN32
61         snprintf(_logfile, 256, "%s%s", DEFAULTDIR, LOGFILE);
62         #else
63         snprintf(_logfile, 256, "%s", LOGFILE);
64         #endif
65         fp = fopen(_logfile, "a");
66     }
67
68     /* Maybe log to syslog if the log file is not available. */
69     if(fp)
70     {
71         (void)fprintf(fp,"%d/%02d/%02d %02d:%02d:%02d ",
72                       p->tm_year+1900,p->tm_mon+1, 
73                       p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
74         (void)vfprintf(fp, msg, args);
75         #ifdef WIN32
76         (void)fprintf(fp, "\r\n");
77         #else
78         (void)fprintf(fp, "\n");
79         #endif
80         fflush(fp);
81         fclose(fp);
82     }
83
84
85     /* Only if not in daemon mode */
86     if(daemon_flag == 0)
87     {
88         /* Print to stderr */           
89         (void)fprintf(stderr,"%d/%02d/%02d %02d:%02d:%02d ",
90                       p->tm_year+1900,p->tm_mon+1 ,p->tm_mday,
91                       p->tm_hour,p->tm_min,p->tm_sec);
92         (void)vfprintf(stderr, msg, args2);
93         #ifdef WIN32
94         (void)fprintf(stderr, "\r\n");
95         #else
96         (void)fprintf(stderr, "\n");
97         #endif
98     }
99
100
101     /* args2 must be ended here */
102     va_end(args2);
103 }
104
105
106 void debug1(const char * msg,...)
107 {
108     if(dbg_flag >= 1)
109     {
110         va_list args;
111         va_start(args, msg);
112
113         _log(msg, args);
114
115         va_end(args);
116     }
117 }
118
119 void debug2(const char * msg,...)
120 {
121     if(dbg_flag >= 2)
122     {
123         va_list args;
124         va_start(args, msg);
125         _log(msg, args);
126         va_end(args);
127     }
128 }
129
130 void merror(const char * msg,... )
131 {
132     va_list args;
133     va_start(args, msg);
134     _log(msg, args);
135     va_end(args);
136 }
137
138 void verbose(const char * msg,... )
139 {
140     va_list args;
141     va_start(args, msg);
142     _log(msg, args);
143     va_end(args);
144 }
145
146 /* Only logs to a file */
147 void log2file(const char * msg,... )
148 {
149     int dbg_tmp;
150     va_list args;
151     va_start(args, msg);
152
153     /* We set daemon flag to 1, so nothing is printed to the terminal */
154     dbg_tmp = daemon_flag;
155     daemon_flag = 1;
156     _log(msg, args);
157
158     daemon_flag = dbg_tmp;
159     
160     va_end(args);
161 }
162
163 void ErrorExit(const char *msg, ...)
164 {
165     va_list args;
166    
167     #ifdef WIN32 
168         /* If not MA */
169         #ifndef MA
170         WinSetError();
171         #endif
172     #endif
173
174     va_start(args, msg);
175     _log(msg, args);
176     va_end(args);
177
178     exit(1);
179 }
180
181
182 void nowChroot()
183 {
184     chroot_flag = 1;
185 }
186
187
188 void nowDaemon()
189 {
190     daemon_flag = 1;
191 }
192
193 void print_out(const char *msg, ...)
194 {
195     va_list args;
196     va_start(args, msg);
197
198     /* Print to stderr */
199     (void)vfprintf(stderr, msg, args);
200     
201     #ifdef WIN32
202     (void)fprintf(stderr, "\r\n");
203     #else
204     (void)fprintf(stderr, "\n");
205     #endif
206     
207     va_end(args);
208 }
209
210
211 void nowDebug()
212 {
213     dbg_flag++;
214 }
215
216 int isChroot()
217 {
218     return(chroot_flag);
219 }
220
221 /* EOF */