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)
112
if (level > iLogLevel)
117
if (iHeaderPending) {
118
iHeaderPending = false;
119
header(self, func, file, line, level);
121
/* Write until the next \n... */
122
if ((end_ptr = strchr((char*)buffer, '\n'))) {
123
len = end_ptr - buffer;
124
ret= fwrite(buffer, len, 1, iStream);
125
fprintf(iStream, "\n");
127
iHeaderPending = true;
131
len = strlen(buffer);
132
ret = fwrite(buffer, len, 1, iStream);
141
void CSLog::log(CSThread *self, int level, const char *buffer)
143
log(self, NULL, NULL, 0, level, buffer);
146
void CSLog::log(CSThread *self, int level, CSString& wstr)
148
log(self, level, wstr.getCString());
151
void CSLog::log(CSThread *self, int level, CSString* wstr)
153
log(self, level, wstr->getCString());
156
void CSLog::log(CSThread *self, int level, int v)
160
snprintf(buffer, 100, "%d", v);
161
log(self, level, buffer);
164
void CSLog::eol(CSThread *self, int level)
166
log(self, level, "\n");
169
void CSLog::logLine(CSThread *self, int level, const char *buffer)
172
log(self, level, buffer);
177
void CSLog::log_va(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, va_list ap)
179
char buffer[DEFAULT_LOG_BUFFER_SIZE];
180
char *log_string = NULL;
184
#if !defined(va_copy) || defined(OS_SOLARIS)
187
len = vsnprintf(buffer, DEFAULT_LOG_BUFFER_SIZE-1, fmt, ap);
188
if (len > DEFAULT_LOG_BUFFER_SIZE-1)
189
len = DEFAULT_LOG_BUFFER_SIZE-1;
193
/* Use the buffer, unless it is too small */
197
if (vsnprintf(buffer, DEFAULT_LOG_BUFFER_SIZE, fmt, ap) >= DEFAULT_LOG_BUFFER_SIZE) {
198
if (vasprintf(&log_string, fmt, ap2) == -1)
206
log(self, func, file, line, level, log_string);
208
if (log_string != buffer)
215
void CSLog::logf(CSThread *self, int level, const char *fmt, ...)
220
log_va(self, level, NULL, NULL, 0, fmt, ap);
224
void CSLog::logf(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, ...)
229
log_va(self, level, func, file, line, fmt, ap);