1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
24
* General logging class
39
#include "CSStrUtil.h"
45
//#define DEFAULT_LOG_BUFFER_SIZE 10
47
#define DEFAULT_LOG_BUFFER_SIZE 2000
51
* The global logging object.
53
CSLog CSL(stdout, CSLog::Warning);
55
void CSLog::getNow(char *buffer, size_t len)
61
if (ticks == (time_t) -1) {
64
fprintf(iStream, "*** ERROR (%d): While getting time\n", err);
65
cs_strcpy(len, buffer, "-- TIME? --");
68
localtime_r(&ticks, <ime);
69
strftime(buffer, len, "%y%m%d %H:%M:%S", <ime);
72
void CSLog::header(CSThread *self, const char *func, const char *file, int line, int level)
78
fprintf(iStream, "%s", buffer);
82
fprintf(iStream, " [Error] ");
85
fprintf(iStream, " [Warning] ");
88
fprintf(iStream, " [Trace] ");
92
fprintf(iStream, " [Note] ");
96
if (self && self->threadName && self->threadName->length() > 0)
97
fprintf(iStream, "%s: ", self->threadName->getCString());
99
cs_format_context(300, buffer, func, file, line);
101
cs_strcat(300, buffer, " ");
102
fprintf(iStream, "%s", buffer);
106
void CSLog::log(CSThread *self, const char *func, const char *file, int line, int level, const char* buffer)
111
if (level > iLogLevel)
116
if (iHeaderPending) {
117
iHeaderPending = false;
118
header(self, func, file, line, level);
120
/* Write until the next \n... */
121
if ((end_ptr = strchr((char*)buffer, '\n'))) {
122
len = end_ptr - buffer;
123
fwrite(buffer, len, 1, iStream);
124
fprintf(iStream, "\n");
126
iHeaderPending = true;
130
len = strlen(buffer);
131
fwrite(buffer, len, 1, iStream);
138
void CSLog::log(CSThread *self, int level, const char *buffer)
140
log(self, NULL, NULL, 0, level, buffer);
143
void CSLog::log(CSThread *self, int level, CSString& wstr)
145
log(self, level, wstr.getCString());
148
void CSLog::log(CSThread *self, int level, CSString* wstr)
150
log(self, level, wstr->getCString());
153
void CSLog::log(CSThread *self, int level, int v)
157
snprintf(buffer, 100, "%d", v);
158
log(self, level, buffer);
161
void CSLog::eol(CSThread *self, int level)
163
log(self, level, "\n");
166
void CSLog::logLine(CSThread *self, int level, const char *buffer)
169
log(self, level, buffer);
174
void CSLog::log_va(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, va_list ap)
176
char buffer[DEFAULT_LOG_BUFFER_SIZE];
177
char *log_string = NULL;
181
#if !defined(va_copy) || defined(OS_SOLARIS)
184
len = vsnprintf(buffer, DEFAULT_LOG_BUFFER_SIZE-1, fmt, ap);
185
if (len > DEFAULT_LOG_BUFFER_SIZE-1)
186
len = DEFAULT_LOG_BUFFER_SIZE-1;
190
/* Use the buffer, unless it is too small */
194
if (vsnprintf(buffer, DEFAULT_LOG_BUFFER_SIZE, fmt, ap) >= DEFAULT_LOG_BUFFER_SIZE) {
195
if (vasprintf(&log_string, fmt, ap2) == -1)
203
log(self, func, file, line, level, log_string);
205
if (log_string != buffer)
212
void CSLog::logf(CSThread *self, int level, const char *fmt, ...)
217
log_va(self, level, NULL, NULL, 0, fmt, ap);
221
void CSLog::logf(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, ...)
226
log_va(self, level, func, file, line, fmt, ap);