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