summaryrefslogtreecommitdiffstats
path: root/src/log.c
blob: fc86643f71f4a4aa32de74ad14dd7b60b67f69d8 (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
/*	Asterisk Manager Proxy
	Copyright (c) 2005-2006 David C. Troy <dave@popvox.com>

	This program is free software, distributed under the terms of
	the GNU General Public License.

	log.c
	Log & debug routines
*/

#include "astmanproxy.h"

#define DATEFORMAT		"%b %e %T"

extern FILE *proxylog;
extern int debug;
extern pthread_mutex_t loglock;
extern pthread_mutex_t debuglock;

void debugmsg (const char *fmt, ...)
{
	va_list ap;

	time_t t;
	struct tm tm;
	char date[80];

	if (!debug)
		return;

	time(&t);
	localtime_r(&t, &tm);
	strftime(date, sizeof(date), DATEFORMAT, &tm);

	pthread_mutex_lock(&debuglock);
	va_start(ap, fmt);
	printf("%s: ", date);
	vprintf(fmt, ap);
	printf("\n");
	va_end(ap);
	pthread_mutex_unlock(&debuglock);
}


void logmsg (const char *fmt, ...)
{
	va_list ap;

	time_t t;
	struct tm tm;
	char date[80];

	time(&t);
	localtime_r(&t, &tm);
	strftime(date, sizeof(date), DATEFORMAT, &tm);

	if (proxylog) {
		pthread_mutex_lock(&loglock);
		va_start(ap, fmt);
		fprintf(proxylog, "%s: ", date);
		vfprintf(proxylog, fmt, ap);
		fprintf(proxylog, "\n");
		va_end(ap);
		fflush(proxylog);
		pthread_mutex_unlock(&loglock);
	}
}