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