File: CLog.cpp

package info (click to toggle)
synergy 1.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 4,664 kB
  • ctags: 5,482
  • sloc: cpp: 46,292; sh: 3,392; makefile: 938; ansic: 82
file content (293 lines) | stat: -rw-r--r-- 6,716 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * synergy -- mouse and keyboard sharing utility
 * Copyright (C) 2002 Chris Schoeneman
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file COPYING that should have accompanied this file.
 * 
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "CLog.h"
#include "CString.h"
#include "CStringUtil.h"
#include "LogOutputters.h"
#include "CArch.h"
#include "Version.h"
#include <cstdio>
#include <cstring>

// names of priorities
static const char*		g_priority[] = {
								"FATAL",
								"ERROR",
								"WARNING",
								"NOTE",
								"INFO",
								"DEBUG",
								"DEBUG1",
								"DEBUG2"
							};

// number of priorities
static const int		g_numPriority = (int)(sizeof(g_priority) /
											sizeof(g_priority[0]));

// the default priority
#if defined(NDEBUG)
static const int		g_defaultMaxPriority = 4;
#else
static const int		g_defaultMaxPriority = 5;
#endif

// length of longest string in g_priority
static const int		g_maxPriorityLength = 7;

// length of suffix string (": ")
static const int		g_prioritySuffixLength = 2;

// amount of padded required to fill in the priority prefix
static const int		g_priorityPad = g_maxPriorityLength +
										g_prioritySuffixLength;


//
// CLog
//

CLog*					CLog::s_log = NULL;

CLog::CLog()
{
	assert(s_log == NULL);

	// create mutex for multithread safe operation
	m_mutex            = ARCH->newMutex();

	// other initalization
	m_maxPriority      = g_defaultMaxPriority;
	m_maxNewlineLength = 0;
	insert(new CConsoleLogOutputter);
}

CLog::~CLog()
{
	// clean up
	for (COutputterList::iterator index  = m_outputters.begin();
								  index != m_outputters.end(); ++index) {
		delete *index;
	}
	for (COutputterList::iterator index  = m_alwaysOutputters.begin();
								  index != m_alwaysOutputters.end(); ++index) {
		delete *index;
	}
	ARCH->closeMutex(m_mutex);
	s_log = NULL;
}

CLog*
CLog::getInstance()
{
	// note -- not thread safe;  client must initialize log safely
	if (s_log == NULL) {
		s_log = new CLog;
	}
	return s_log;
}

void
CLog::print(const char* file, int line, const char* fmt, ...) const
{
	// check if fmt begins with a priority argument
	int priority = 4;
	if (fmt[0] == '%' && fmt[1] == 'z') {
		priority = fmt[2] - '\060';
		fmt += 3;
	}

	// done if below priority threshold
	if (priority > getFilter()) {
		return;
	}

	// compute prefix padding length
	char stack[1024];
	int pPad = g_priorityPad;
	if (file != NULL) {
		sprintf(stack, "%d", line);
		pPad += strlen(file) + 1 /* comma */ +
				strlen(stack) + 1 /* colon */ + 1 /* space */;
	}

	// compute suffix padding length
	int sPad = m_maxNewlineLength;

	// print to buffer, leaving space for a newline at the end and prefix
	// at the beginning.
	char* buffer = stack;
	int len      = (int)(sizeof(stack) / sizeof(stack[0]));
	while (true) {
		// try printing into the buffer
		va_list args;
		va_start(args, fmt);
		int n = ARCH->vsnprintf(buffer + pPad, len - pPad - sPad, fmt, args);
		va_end(args);

		// if the buffer wasn't big enough then make it bigger and try again
		if (n < 0 || n > (int)len) {
			if (buffer != stack) {
				delete[] buffer;
			}
			len   *= 2;
			buffer = new char[len];
		}

		// if the buffer was big enough then continue
		else {
			break;
		}
	}

	// print the prefix to the buffer.  leave space for priority label.
	char* message = buffer;
	if (file != NULL) {
		sprintf(buffer + g_priorityPad, "%s,%d:", file, line);
		buffer[pPad - 1] = ' ';

		// discard file and line if priority < 0
		if (priority < 0) {
			message += pPad - g_priorityPad;
		}
	}

	// output buffer
	output(priority, message);

	// clean up
	if (buffer != stack) {
		delete[] buffer;
	}
}

void
CLog::insert(ILogOutputter* outputter, bool alwaysAtHead)
{
	assert(outputter               != NULL);
	assert(outputter->getNewline() != NULL);

	CArchMutexLock lock(m_mutex);
	if (alwaysAtHead) {
		m_alwaysOutputters.push_front(outputter);
	}
	else {
		m_outputters.push_front(outputter);
	}
	int newlineLength = strlen(outputter->getNewline());
	if (newlineLength > m_maxNewlineLength) {
		m_maxNewlineLength = newlineLength;
	}
	outputter->open(kAppVersion);
	outputter->show(false);
}

void
CLog::remove(ILogOutputter* outputter)
{
	CArchMutexLock lock(m_mutex);
	m_outputters.remove(outputter);
	m_alwaysOutputters.remove(outputter);
}

void
CLog::pop_front(bool alwaysAtHead)
{
	CArchMutexLock lock(m_mutex);
	COutputterList* list = alwaysAtHead ? &m_alwaysOutputters : &m_outputters;
	if (!list->empty()) {
		delete list->front();
		list->pop_front();
	}
}

bool
CLog::setFilter(const char* maxPriority)
{
	if (maxPriority != NULL) {
		for (int i = 0; i < g_numPriority; ++i) {
			if (strcmp(maxPriority, g_priority[i]) == 0) {
				setFilter(i);
				return true;
			}
		}
		return false;
	}
	return true;
}

void
CLog::setFilter(int maxPriority)
{
	CArchMutexLock lock(m_mutex);
	m_maxPriority = maxPriority;
}

int
CLog::getFilter() const
{
	CArchMutexLock lock(m_mutex);
	return m_maxPriority;
}

void
CLog::output(int priority, char* msg) const
{
	assert(priority >= -1 && priority < g_numPriority);
	assert(msg != NULL);

	// insert priority label
	int n = -g_prioritySuffixLength;
	if (priority >= 0) {
		n = strlen(g_priority[priority]);
		strcpy(msg + g_maxPriorityLength - n, g_priority[priority]);
		msg[g_maxPriorityLength + 0] = ':';
		msg[g_maxPriorityLength + 1] = ' ';
		msg[g_maxPriorityLength + 1] = ' ';
	}

	// find end of message
	char* end = msg + g_priorityPad + strlen(msg + g_priorityPad);

	// write to each outputter
	CArchMutexLock lock(m_mutex);
	for (COutputterList::const_iterator index  = m_alwaysOutputters.begin();
										index != m_alwaysOutputters.end();
										++index) {
		// get outputter
		ILogOutputter* outputter = *index;
		
		// put an appropriate newline at the end
		strcpy(end, outputter->getNewline());

		// write message
		outputter->write(static_cast<ILogOutputter::ELevel>(priority),
							msg + g_maxPriorityLength - n);
	}
	for (COutputterList::const_iterator index  = m_outputters.begin();
										index != m_outputters.end(); ++index) {
		// get outputter
		ILogOutputter* outputter = *index;
		
		// put an appropriate newline at the end
		strcpy(end, outputter->getNewline());

		// write message and break out of loop if it returns false
		if (!outputter->write(static_cast<ILogOutputter::ELevel>(priority),
							msg + g_maxPriorityLength - n)) {
			break;
		}
	}
}