~drizzle-trunk/drizzle/development

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
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Original author: Paul McCullagh (H&G2JCtL)
 * Continued development: Barry Leslie
 *
 * 2007-05-21
 *
 * General logging class
 *
 */

#include "CSConfig.h"

#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

#include "CSLog.h"
#include "CSMemory.h"
#include "CSUTF8.h"
#include "CSStrUtil.h"
#include "CSThread.h"
#include "CSGlobal.h"


//#ifdef DEBUG
//#define DEFAULT_LOG_BUFFER_SIZE			10
//#else
#define DEFAULT_LOG_BUFFER_SIZE			2000
//#endif

/*
 * The global logging object.
 */
CSLog CSL(stdout, CSLog::Warning);

void CSLog::getNow(char *buffer, size_t len)
{
	time_t		ticks;
	struct tm	ltime;

	ticks = time(NULL);
	if (ticks == (time_t) -1) {
		int err = errno;

		fprintf(iStream, "*** ERROR (%d): While getting time\n", err);
		cs_strcpy(len, buffer, "-- TIME? --");
		return;
	}
	localtime_r(&ticks, &ltime);
	strftime(buffer, len, "%y%m%d %H:%M:%S", &ltime);
}

void CSLog::header(CSThread *self, const char *func, const char *file, int line, int level)
{
	char buffer[300];

	getNow(buffer, 300);
	
	fprintf(iStream, "%s", buffer);

	switch (level) {
		case CSLog::Error:
			fprintf(iStream, " [Error] ");
			break;
		case CSLog::Warning:
			fprintf(iStream, " [Warning] ");
			break;
		case CSLog::Trace:
			fprintf(iStream, " [Trace] ");
			break;
		case CSLog::Protocol:
		default:
			fprintf(iStream, " [Note] ");
			break;
	}

	if (self && self->threadName && self->threadName->length() > 0)
		fprintf(iStream, "%s: ", self->threadName->getCString());

	cs_format_context(300, buffer, func, file, line);
	if (*buffer) {
		cs_strcat(300, buffer, " ");
		fprintf(iStream, "%s", buffer);
	}
}

void CSLog::log(CSThread *self, const char *func, const char *file, int line, int level, const char* buffer)
{
	const char	*end_ptr;
	size_t		len;

	if (level > iLogLevel)
		return;

	lock();
	while (*buffer) {
		if (iHeaderPending) {
			iHeaderPending = false;
			header(self, func, file, line, level);
		}
		/* Write until the next \n... */
		if ((end_ptr = strchr((char*)buffer, '\n'))) {
			len = end_ptr - buffer;
			fwrite(buffer, len, 1, iStream);
			fprintf(iStream, "\n");
			fflush(iStream);
			iHeaderPending = true;
			len++;
		}
		else {
			len = strlen(buffer);
			fwrite(buffer, len, 1, iStream);
		}
		buffer += len;
	}
	unlock();
}

void CSLog::log(CSThread *self, int level, const char *buffer)
{
	log(self, NULL, NULL, 0, level, buffer);
}

void CSLog::log(CSThread *self, int level, CSString& wstr)
{
	log(self, level, wstr.getCString());
}

void CSLog::log(CSThread *self, int level, CSString* wstr)
{
	log(self, level, wstr->getCString());
}

void CSLog::log(CSThread *self, int level, int v)
{
	char buffer[100];

	snprintf(buffer, 100, "%d", v);
	log(self, level, buffer);
}

void CSLog::eol(CSThread *self, int level)
{
	log(self, level, "\n");
}

void CSLog::logLine(CSThread *self, int level, const char *buffer)
{
	lock();
	log(self, level, buffer);
	eol(self, level);
	unlock();
}

void CSLog::log_va(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, va_list ap)
{
	char buffer[DEFAULT_LOG_BUFFER_SIZE];
	char *log_string = NULL;

	lock();

#if !defined(va_copy) || defined(OS_SOLARIS)
	int len;

	len = vsnprintf(buffer, DEFAULT_LOG_BUFFER_SIZE-1, fmt, ap);
	if (len > DEFAULT_LOG_BUFFER_SIZE-1)
		len = DEFAULT_LOG_BUFFER_SIZE-1;
	buffer[len] = 0;
	log_string = buffer;
#else
	/* Use the buffer, unless it is too small */
	va_list ap2;

	va_copy(ap2, ap);
	if (vsnprintf(buffer, DEFAULT_LOG_BUFFER_SIZE, fmt, ap) >= DEFAULT_LOG_BUFFER_SIZE) {
		if (vasprintf(&log_string, fmt, ap2) == -1)
			log_string = NULL;
	}
	else
		log_string = buffer;
#endif

	if (log_string) {
		log(self, func, file, line, level, log_string);

		if (log_string != buffer)
			free(log_string);
	}

	unlock();
}

void CSLog::logf(CSThread *self, int level, const char *fmt, ...)
{
	va_list	ap;

	va_start(ap, fmt);
	log_va(self, level, NULL, NULL, 0, fmt, ap);
	va_end(ap);
}

void CSLog::logf(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, ...)
{
	va_list	ap;

	va_start(ap, fmt);
	log_va(self, level, func, file, line, fmt, ap);
	va_end(ap);
}