~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2010-06-02 22:35:45 UTC
  • mto: This revision was merged to the branch mainline in revision 1586.
  • Revision ID: mordred@inaugust.com-20100602223545-q8ekf9b40a85nwuf
Rearragned unittests into a single exe because of how we need to link it
(thanks lifeless)
Link with server symbols without needing to build a library.
Added an additional atomics test which tests whatever version of the atomics
lib the running platform would actually use.

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
 
 
111
 
        if (level > iLogLevel)
112
 
                return;
113
 
 
114
 
        lock();
115
 
        while (*buffer) {
116
 
                if (iHeaderPending) {
117
 
                        iHeaderPending = false;
118
 
                        header(self, func, file, line, level);
119
 
                }
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");
125
 
                        fflush(iStream);
126
 
                        iHeaderPending = true;
127
 
                        len++;
128
 
                }
129
 
                else {
130
 
                        len = strlen(buffer);
131
 
                        fwrite(buffer, len, 1, iStream);
132
 
                }
133
 
                buffer += len;
134
 
        }
135
 
        unlock();
136
 
}
137
 
 
138
 
void CSLog::log(CSThread *self, int level, const char *buffer)
139
 
{
140
 
        log(self, NULL, NULL, 0, level, buffer);
141
 
}
142
 
 
143
 
void CSLog::log(CSThread *self, int level, CSString& wstr)
144
 
{
145
 
        log(self, level, wstr.getCString());
146
 
}
147
 
 
148
 
void CSLog::log(CSThread *self, int level, CSString* wstr)
149
 
{
150
 
        log(self, level, wstr->getCString());
151
 
}
152
 
 
153
 
void CSLog::log(CSThread *self, int level, int v)
154
 
{
155
 
        char buffer[100];
156
 
 
157
 
        snprintf(buffer, 100, "%d", v);
158
 
        log(self, level, buffer);
159
 
}
160
 
 
161
 
void CSLog::eol(CSThread *self, int level)
162
 
{
163
 
        log(self, level, "\n");
164
 
}
165
 
 
166
 
void CSLog::logLine(CSThread *self, int level, const char *buffer)
167
 
{
168
 
        lock();
169
 
        log(self, level, buffer);
170
 
        eol(self, level);
171
 
        unlock();
172
 
}
173
 
 
174
 
void CSLog::log_va(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, va_list ap)
175
 
{
176
 
        char buffer[DEFAULT_LOG_BUFFER_SIZE];
177
 
        char *log_string = NULL;
178
 
 
179
 
        lock();
180
 
 
181
 
#if !defined(va_copy) || defined(OS_SOLARIS)
182
 
        int len;
183
 
 
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;
187
 
        buffer[len] = 0;
188
 
        log_string = buffer;
189
 
#else
190
 
        /* Use the buffer, unless it is too small */
191
 
        va_list ap2;
192
 
 
193
 
        va_copy(ap2, ap);
194
 
        if (vsnprintf(buffer, DEFAULT_LOG_BUFFER_SIZE, fmt, ap) >= DEFAULT_LOG_BUFFER_SIZE) {
195
 
                if (vasprintf(&log_string, fmt, ap2) == -1)
196
 
                        log_string = NULL;
197
 
        }
198
 
        else
199
 
                log_string = buffer;
200
 
#endif
201
 
 
202
 
        if (log_string) {
203
 
                log(self, func, file, line, level, log_string);
204
 
 
205
 
                if (log_string != buffer)
206
 
                        free(log_string);
207
 
        }
208
 
 
209
 
        unlock();
210
 
}
211
 
 
212
 
void CSLog::logf(CSThread *self, int level, const char *fmt, ...)
213
 
{
214
 
        va_list ap;
215
 
 
216
 
        va_start(ap, fmt);
217
 
        log_va(self, level, NULL, NULL, 0, fmt, ap);
218
 
        va_end(ap);
219
 
}
220
 
 
221
 
void CSLog::logf(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, ...)
222
 
{
223
 
        va_list ap;
224
 
 
225
 
        va_start(ap, fmt);
226
 
        log_va(self, level, func, file, line, fmt, ap);
227
 
        va_end(ap);
228
 
}
229