1548.2.1
by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin. |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 |
||
33 |
#include "CSLog.h" |
|
34 |
#include "CSMemory.h" |
|
35 |
#include "CSUTF8.h" |
|
36 |
#include "CSStrUtil.h" |
|
37 |
#include "CSThread.h" |
|
38 |
#include "CSGlobal.h" |
|
39 |
/*
|
|
40 |
* The global logging object.
|
|
41 |
*/
|
|
42 |
CSLog CSL(stdout, CSLog::Warning); |
|
43 |
||
44 |
void CSLog::getNow(char *buffer, size_t len) |
|
45 |
{
|
|
46 |
time_t ticks; |
|
47 |
struct tm ltime; |
|
48 |
||
49 |
ticks = time(NULL); |
|
50 |
if (ticks == (time_t) -1) { |
|
51 |
int err = errno; |
|
52 |
||
53 |
fprintf(iStream, "*** ERROR (%d): While getting time\n", err); |
|
54 |
cs_strcpy(len, buffer, "-- TIME? --"); |
|
55 |
return; |
|
56 |
}
|
|
57 |
localtime_r(&ticks, <ime); |
|
58 |
strftime(buffer, len, "%y%m%d %H:%M:%S", <ime); |
|
59 |
}
|
|
60 |
||
61 |
void CSLog::header(CSThread *self, const char *func, const char *file, int line, int level) |
|
62 |
{
|
|
63 |
char buffer[300]; |
|
64 |
||
65 |
getNow(buffer, 300); |
|
66 |
||
67 |
fprintf(iStream, "%s", buffer); |
|
68 |
||
69 |
switch (level) { |
|
70 |
case CSLog::Error: |
|
71 |
fprintf(iStream, " [Error] "); |
|
72 |
break; |
|
73 |
case CSLog::Warning: |
|
74 |
fprintf(iStream, " [Warning] "); |
|
75 |
break; |
|
76 |
case CSLog::Trace: |
|
77 |
fprintf(iStream, " [Trace] "); |
|
78 |
break; |
|
79 |
case CSLog::Protocol: |
|
80 |
default: |
|
81 |
fprintf(iStream, " [Note] "); |
|
82 |
break; |
|
83 |
}
|
|
84 |
||
85 |
if (self && self->threadName && self->threadName->length() > 0) |
|
86 |
fprintf(iStream, "%s: ", self->threadName->getCString()); |
|
87 |
||
88 |
cs_format_context(300, buffer, func, file, line); |
|
89 |
if (*buffer) { |
|
90 |
cs_strcat(300, buffer, " "); |
|
91 |
fprintf(iStream, "%s", buffer); |
|
92 |
}
|
|
93 |
}
|
|
94 |
||
95 |
void CSLog::log(CSThread *self, const char *func, const char *file, int line, int level, const char* buffer) |
|
96 |
{
|
|
97 |
const char *end_ptr; |
|
98 |
size_t len; |
|
99 |
||
100 |
if (level > iLogLevel) |
|
101 |
return; |
|
102 |
||
103 |
lock(); |
|
104 |
while (*buffer) { |
|
105 |
if (iHeaderPending) { |
|
106 |
iHeaderPending = false; |
|
107 |
header(self, func, file, line, level); |
|
108 |
}
|
|
109 |
/* Write until the next \n... */
|
|
1548.2.24
by Barry.Leslie at PrimeBase
Reorganized code while fixing some minor problems. |
110 |
if ((end_ptr = strchr((char*)buffer, '\n'))) { |
1548.2.1
by Barry.Leslie at PrimeBase
Added the PBMS daemon plugin. |
111 |
len = end_ptr - buffer; |
112 |
fwrite(buffer, len, 1, iStream); |
|
113 |
fprintf(iStream, "\n"); |
|
114 |
fflush(iStream); |
|
115 |
iHeaderPending = true; |
|
116 |
len++; |
|
117 |
}
|
|
118 |
else { |
|
119 |
len = strlen(buffer); |
|
120 |
fwrite(buffer, len, 1, iStream); |
|
121 |
}
|
|
122 |
buffer += len; |
|
123 |
}
|
|
124 |
unlock(); |
|
125 |
}
|
|
126 |
||
127 |
void CSLog::log(CSThread *self, int level, const char *buffer) |
|
128 |
{
|
|
129 |
log(self, NULL, NULL, 0, level, buffer); |
|
130 |
}
|
|
131 |
||
132 |
void CSLog::log(CSThread *self, int level, CSString& wstr) |
|
133 |
{
|
|
134 |
log(self, level, wstr.getCString()); |
|
135 |
}
|
|
136 |
||
137 |
void CSLog::log(CSThread *self, int level, CSString* wstr) |
|
138 |
{
|
|
139 |
log(self, level, wstr->getCString()); |
|
140 |
}
|
|
141 |
||
142 |
void CSLog::log(CSThread *self, int level, int v) |
|
143 |
{
|
|
144 |
char buffer[100]; |
|
145 |
||
146 |
snprintf(buffer, 100, "%d", v); |
|
147 |
log(self, level, buffer); |
|
148 |
}
|
|
149 |
||
150 |
void CSLog::eol(CSThread *self, int level) |
|
151 |
{
|
|
152 |
log(self, level, "\n"); |
|
153 |
}
|
|
154 |
||
155 |
void CSLog::logLine(CSThread *self, int level, const char *buffer) |
|
156 |
{
|
|
157 |
lock(); |
|
158 |
log(self, level, buffer); |
|
159 |
eol(self, level); |
|
160 |
unlock(); |
|
161 |
}
|
|
162 |
||
163 |
||
164 |