summaryrefslogtreecommitdiffstats
path: root/src/libs/zbxlog/log.c
blob: 0be91741a8514fb50018beb94b798623182989ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* 
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/


#include "common.h"
#include "log.h"

#include "mutexs.h"
#include "threads.h"

static	char log_filename[MAX_STRING_LEN];

static	int log_type = LOG_TYPE_UNDEFINED;
static	int log_level = LOG_LEVEL_DEBUG;

static ZBX_MUTEX log_file_access;

#if defined(_WINDOWS)

#include "messages.h"

#include "service.h"

static HANDLE system_log_handle = INVALID_HANDLE_VALUE;

#endif /* _WINDOWS */

#if !defined(_WINDOWS)

void redirect_std(const char *filename)
{
	close(0);	close(1);	close(2);

	open("/dev/null", O_RDONLY);    /* stdin */

	if(!filename || !*filename)
	{
		open("/dev/null", O_RDWR);      /* stdout */
		open("/dev/null", O_RDWR);      /* stderr */
	}
	else
	{
		fopen(filename, "a+");   /* stdout */
		fopen(filename, "a+");   /* stderr */
	}
}

#endif /* not _WINDOWS */

int zabbix_open_log(int type, int level, const char *filename)
{
	FILE *log_file = NULL;


	log_level = level;

	if(LOG_LEVEL_EMPTY == level)
	{
		return	SUCCEED;
	}

	if(LOG_TYPE_FILE == type && NULL == filename)
	{
		type = LOG_TYPE_SYSLOG;
	}

	if(LOG_TYPE_SYSLOG == type)
	{
		log_type = LOG_TYPE_SYSLOG;

#if defined(_WINDOWS)

		system_log_handle = RegisterEventSource(NULL, ZABBIX_EVENT_SOURCE);

#else /* not _WINDOWS */

        	openlog("zabbix_suckerd", LOG_PID, LOG_USER);
        	setlogmask(LOG_UPTO(LOG_WARNING));

#endif /* _WINDOWS */
	}

	else if(LOG_TYPE_FILE == type)
	{
		if(strlen(filename) >= MAX_STRING_LEN)
		{
			zbx_error("To large path for logfile.");
			return	FAIL;
		}

		if(ZBX_MUTEX_ERROR == zbx_mutex_create(&log_file_access, ZBX_MUTEX_LOG))
		{
			zbx_error("Unable to create mutex for log file");
			return	FAIL;
		}
		
		if(NULL == (log_file = fopen(filename,"a+")))
		{
			zbx_error("Unable to open log file [%s] [%s]", filename, strerror(errno));
			return	FAIL;
		}

		log_type = LOG_TYPE_FILE;
		strscpy(log_filename,filename);
		zbx_fclose(log_file);
	}
	else
	{
		/* Not supported logging type */

		if(ZBX_MUTEX_ERROR == zbx_mutex_create(&log_file_access, ZBX_MUTEX_LOG))
		{
			zbx_error("Unable to create mutex for log file");
			return	FAIL;
		}

		zbx_error("Not supported loggin type [%d]", type);
		return	FAIL;
	}

	return	SUCCEED;
}

void zabbix_close_log(void)
{
	if(LOG_TYPE_SYSLOG == log_type)
	{
#if defined(_WINDOWS)

		if(system_log_handle) 
			DeregisterEventSource(system_log_handle);

#else /* not _WINDOWS */

		closelog();

#endif /* _WINDOWS */
	}
	else if(log_type == LOG_TYPE_FILE)
	{
		zbx_mutex_destroy(&log_file_access);
	}
	else
	{
		/* Not supported loggin type */
		zbx_mutex_destroy(&log_file_access);
	}
}

void zabbix_set_log_level(int level)
{
	log_level = level;
}

void zabbix_log(int level, const char *fmt, ...)
{
#ifdef TEST
	time_t	t;
	struct	tm	*tm;
	va_list ap;
	
		t=time(NULL);
		tm=localtime(&t);
		printf("%.6d:%.4d%.2d%.2d:%.2d%.2d%.2d ",(int)getpid(),tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
		va_start(ap,fmt);
		vprintf(fmt,ap);
		va_end(ap);

		printf("\n");
#else /* TEST */
	
	FILE *log_file = NULL;

	char	message[MAX_BUF_LEN];

	time_t		t;
	struct	tm	*tm;
	va_list		args;

	struct	stat	buf;

	static size_t	old_size = 0;

	char	filename_old[MAX_STRING_LEN];
#if defined(_WINDOWS)

	WORD	wType;
	char	thread_id[20];
	char	*(strings[]) = {thread_id, message, NULL};
	
#endif /* _WINDOWS */

	if( (level != LOG_LEVEL_INFORMATION) && ((level > log_level) || (LOG_LEVEL_EMPTY == level)) )
	{
		return;
	}

	if(LOG_TYPE_FILE == log_type)
	{
		zbx_mutex_lock(&log_file_access);
		
		log_file = fopen(log_filename,"a+");

		if(NULL != log_file)
		{
			t = time(NULL);
			tm = localtime(&t);

			fprintf(log_file,
				"%6lu:%.4d%.2d%.2d:%.2d%.2d%.2d ",
				(unsigned long)zbx_get_thread_id(),
				tm->tm_year+1900,
				tm->tm_mon+1,
				tm->tm_mday,
				tm->tm_hour,
				tm->tm_min,
				tm->tm_sec
				);

			va_start(args,fmt);


			vfprintf(log_file,fmt, args);

			va_end(args);

			fprintf(log_file,"\n");
			zbx_fclose(log_file);

			if(stat(log_filename,&buf) == 0)
			{
				if(buf.st_size > MAX_LOG_FILE_LEN)
				{
					strscpy(filename_old,log_filename);
					zbx_strlcat(filename_old,".old",MAX_STRING_LEN);
					remove(filename_old);
					if(rename(log_filename,filename_old) != 0)
					{
						zbx_error("Can't rename log file [%s] to [%s] [%s]", log_filename, filename_old, strerror(errno));
					}
				}

				if(old_size > buf.st_size)
				{
					redirect_std(log_filename);
				}

				old_size = buf.st_size;
			}
		}

		zbx_mutex_unlock(&log_file_access);

		return;
	}
	
	memset(message, 0, sizeof(message));
	va_start(args, fmt);
	vsnprintf(message, sizeof(message)-1, fmt, args);
	va_end(args);

	if(LOG_TYPE_SYSLOG == log_type)
	{
#if defined(_WINDOWS)
		t = time(NULL);
		tm = localtime(&t);

		memset(thread_id, 0, sizeof(thread_id));
		zbx_snprintf(thread_id, sizeof(thread_id),"[%lu]: ",(unsigned long)zbx_get_thread_id());

		switch(level)
		{
			case LOG_LEVEL_CRIT:
			case LOG_LEVEL_ERR:
				wType = EVENTLOG_ERROR_TYPE;
				break;
			case LOG_LEVEL_WARNING:	
				wType = EVENTLOG_WARNING_TYPE;
				break;
			default:
				wType = EVENTLOG_INFORMATION_TYPE;
				break;
		}
		ReportEvent(
			system_log_handle, 
			wType, 
			0, 
			MSG_ZABBIX_MESSAGE, 
			NULL, 
			sizeof(*strings)-1, 
			0, 
			strings, 
			NULL);

#else /* not _WINDOWS */

		syslog(LOG_DEBUG, "%s", message);
		
#endif /* _WINDOWS */
	}
	else
	{
		zbx_mutex_lock(&log_file_access);
		
		switch(level)
		{
			case LOG_LEVEL_CRIT:
				zbx_error("ERROR: %s", message);
				break;
			case LOG_LEVEL_ERR:
				zbx_error("Error: %s", message);
				break;
			case LOG_LEVEL_WARNING:	
				zbx_error("Warning: %s", message);
				break;
			case LOG_LEVEL_DEBUG:	
				zbx_error("DEBUG: %s", message);
				break;
			default:
				zbx_error("%s", message);
				break;
		}
		
		zbx_mutex_unlock(&log_file_access);
	}	
	
#endif /* TEST */

}

/*
 * Get system error string by call to FormatMessage
 */
#define ZBX_MESSAGE_BUF_SIZE	1024

char *strerror_from_system(unsigned long error)
{
#if defined(_WINDOWS)

	static char buffer[ZBX_MESSAGE_BUF_SIZE];  /* !!! Attention static !!! not thread safely - Win32*/

	memset(buffer, 0, sizeof(buffer));

	if(FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM, 
		NULL, 
		error,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
		buffer, 
		sizeof(buffer), 
		NULL) == 0)
	{
		zbx_snprintf(buffer, sizeof(buffer), "3. MSG 0x%08X - Unable to find message text [0x%X]", error , GetLastError());
	}

	return buffer;

#else /* not _WINDOWS */

	return strerror(errno);

#endif /* _WINDOWS */
}

/*
 * Get system error string by call to FormatMessage
 */

char *strerror_from_module(unsigned long error, const char *module)
{
#if defined(_WINDOWS)

	static char buffer[ZBX_MESSAGE_BUF_SIZE]; /* !!! Attention static !!! not thread safely - Win32*/
	char *strings[2];

	memset(strings,0,sizeof(char *)*2);
	memset(buffer, 0, sizeof(buffer));

	if (FormatMessage(
		FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
		module ? GetModuleHandle(module) : NULL,
		error,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
		(LPTSTR)buffer,
		sizeof(buffer),
		strings) == 0)
	{
		zbx_snprintf(buffer, sizeof(buffer), "3. MSG 0x%08X - Unable to find message text [%s]", error , strerror_from_system(GetLastError()));
	}

	return (char *)buffer;

#else /* not _WINDOWS */

	return strerror(errno);

#endif /* _WINDOWS */

}