~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSLog.cc

  • Committer: Patrick Crews
  • Date: 2011-01-29 14:17:35 UTC
  • mto: (2126.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2127.
  • Revision ID: gleebix@gmail.com-20110129141735-3y2658vt5ur0a33o
Fixes to make test-dbqp

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 * Original author: Paul McCullagh (H&G2JCtL)
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-05-21
 
23
 *
 
24
 * General logging class
 
25
 *
 
26
 */
 
27
 
 
28
#include "CSConfig.h"
 
29
 
 
30
#include <string.h>
 
31
#include <time.h>
 
32
#include <stdarg.h>
 
33
#include <stdio.h>
 
34
#include <stdlib.h>
 
35
 
 
36
#include "CSLog.h"
 
37
#include "CSMemory.h"
 
38
#include "CSUTF8.h"
 
39
#include "CSStrUtil.h"
 
40
#include "CSThread.h"
 
41
#include "CSGlobal.h"
 
42
 
 
43
 
 
44
//#ifdef DEBUG
 
45
//#define DEFAULT_LOG_BUFFER_SIZE                       10
 
46
//#else
 
47
#define DEFAULT_LOG_BUFFER_SIZE                 2000
 
48
//#endif
 
49
 
 
50
/*
 
51
 * The global logging object.
 
52
 */
 
53
CSLog CSL(stdout, CSLog::Warning);
 
54
 
 
55
void CSLog::getNow(char *buffer, size_t len)
 
56
{
 
57
        time_t          ticks;
 
58
        struct tm       ltime;
 
59
 
 
60
        ticks = time(NULL);
 
61
        if (ticks == (time_t) -1) {
 
62
                int err = errno;
 
63
 
 
64
                fprintf(iStream, "*** ERROR (%d): While getting time\n", err);
 
65
                cs_strcpy(len, buffer, "-- TIME? --");
 
66
                return;
 
67
        }
 
68
        localtime_r(&ticks, &ltime);
 
69
        strftime(buffer, len, "%y%m%d %H:%M:%S", &ltime);
 
70
}
 
71
 
 
72
void CSLog::header(CSThread *self, const char *func, const char *file, int line, int level)
 
73
{
 
74
        char buffer[300];
 
75
 
 
76
        getNow(buffer, 300);
 
77
        
 
78
        fprintf(iStream, "%s", buffer);
 
79
 
 
80
        switch (level) {
 
81
                case CSLog::Error:
 
82
                        fprintf(iStream, " [Error] ");
 
83
                        break;
 
84
                case CSLog::Warning:
 
85
                        fprintf(iStream, " [Warning] ");
 
86
                        break;
 
87
                case CSLog::Trace:
 
88
                        fprintf(iStream, " [Trace] ");
 
89
                        break;
 
90
                case CSLog::Protocol:
 
91
                default:
 
92
                        fprintf(iStream, " [Note] ");
 
93
                        break;
 
94
        }
 
95
 
 
96
        if (self && self->threadName && self->threadName->length() > 0)
 
97
                fprintf(iStream, "%s: ", self->threadName->getCString());
 
98
 
 
99
        cs_format_context(300, buffer, func, file, line);
 
100
        if (*buffer) {
 
101
                cs_strcat(300, buffer, " ");
 
102
                fprintf(iStream, "%s", buffer);
 
103
        }
 
104
}
 
105
 
 
106
void CSLog::log(CSThread *self, const char *func, const char *file, int line, int level, const char* buffer)
 
107
{
 
108
        const char      *end_ptr;
 
109
        size_t          len;
 
110
        size_t ret;
 
111
 
 
112
        if (level > iLogLevel)
 
113
                return;
 
114
 
 
115
        lock();
 
116
        while (*buffer) {
 
117
                if (iHeaderPending) {
 
118
                        iHeaderPending = false;
 
119
                        header(self, func, file, line, level);
 
120
                }
 
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");
 
126
                        fflush(iStream);
 
127
                        iHeaderPending = true;
 
128
                        len++;
 
129
                }
 
130
                else {
 
131
                        len = strlen(buffer);
 
132
                        ret = fwrite(buffer, len, 1, iStream);
 
133
 
 
134
                }
 
135
                buffer += len;
 
136
        }
 
137
        unlock();
 
138
        (void)ret;
 
139
}
 
140
 
 
141
void CSLog::log(CSThread *self, int level, const char *buffer)
 
142
{
 
143
        log(self, NULL, NULL, 0, level, buffer);
 
144
}
 
145
 
 
146
void CSLog::log(CSThread *self, int level, CSString& wstr)
 
147
{
 
148
        log(self, level, wstr.getCString());
 
149
}
 
150
 
 
151
void CSLog::log(CSThread *self, int level, CSString* wstr)
 
152
{
 
153
        log(self, level, wstr->getCString());
 
154
}
 
155
 
 
156
void CSLog::log(CSThread *self, int level, int v)
 
157
{
 
158
        char buffer[100];
 
159
 
 
160
        snprintf(buffer, 100, "%d", v);
 
161
        log(self, level, buffer);
 
162
}
 
163
 
 
164
void CSLog::eol(CSThread *self, int level)
 
165
{
 
166
        log(self, level, "\n");
 
167
}
 
168
 
 
169
void CSLog::logLine(CSThread *self, int level, const char *buffer)
 
170
{
 
171
        lock();
 
172
        log(self, level, buffer);
 
173
        eol(self, level);
 
174
        unlock();
 
175
}
 
176
 
 
177
void CSLog::log_va(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, va_list ap)
 
178
{
 
179
        char buffer[DEFAULT_LOG_BUFFER_SIZE];
 
180
        char *log_string = NULL;
 
181
 
 
182
        lock();
 
183
 
 
184
#if !defined(va_copy) || defined(OS_SOLARIS)
 
185
        int len;
 
186
 
 
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;
 
190
        buffer[len] = 0;
 
191
        log_string = buffer;
 
192
#else
 
193
        /* Use the buffer, unless it is too small */
 
194
        va_list ap2;
 
195
 
 
196
        va_copy(ap2, ap);
 
197
        if (vsnprintf(buffer, DEFAULT_LOG_BUFFER_SIZE, fmt, ap) >= DEFAULT_LOG_BUFFER_SIZE) {
 
198
                if (vasprintf(&log_string, fmt, ap2) == -1)
 
199
                        log_string = NULL;
 
200
        }
 
201
        else
 
202
                log_string = buffer;
 
203
#endif
 
204
 
 
205
        if (log_string) {
 
206
                log(self, func, file, line, level, log_string);
 
207
 
 
208
                if (log_string != buffer)
 
209
                        free(log_string);
 
210
        }
 
211
 
 
212
        unlock();
 
213
}
 
214
 
 
215
void CSLog::logf(CSThread *self, int level, const char *fmt, ...)
 
216
{
 
217
        va_list ap;
 
218
 
 
219
        va_start(ap, fmt);
 
220
        log_va(self, level, NULL, NULL, 0, fmt, ap);
 
221
        va_end(ap);
 
222
}
 
223
 
 
224
void CSLog::logf(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, ...)
 
225
{
 
226
        va_list ap;
 
227
 
 
228
        va_start(ap, fmt);
 
229
        log_va(self, level, func, file, line, fmt, ap);
 
230
        va_end(ap);
 
231
}
 
232