aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/llerrorbuffer.cpp
blob: 9de46161c2932f45fb1d64b2360ad4d020239443 (plain)
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
/** 
 * @file llerrorbuffer.cpp
 *
 * Copyright (c) 2002-2007, Linden Research, Inc.
 * 
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 */

#include "linden_common.h"

#include "llerrorbuffer.h"
#include "llfixedbuffer.h"

#include <fstream>
#include <string.h>

#if LL_WINDOWS
#	define WIN32_LEAN_AND_MEAN
#	include <winsock2.h>
#	include <windows.h>		// for OutputDebugString
#else
#	include <syslog.h>
#	include <stdio.h>
#endif

#include <time.h>

// In order to compile in Visual C++ 6.0, you must use std:: in the header, and then
// use the std namespace in the cpp.  Otherwise, you'll break the (very) fragile C++ parser.
using namespace std;

LLErrorBuffer::LLErrorBuffer()
:	streambuf(),
    mFile(NULL),
	mBuf(),
	mFixedBuf(NULL),
    mErrorActive(TRUE),
	mErrorTimestamp(TRUE),
    mFileActive(FALSE),
    mSyslogActive(TRUE),
	mWinDebugActive(TRUE),
	mElevatedRemote(FALSE),
	mIsUTC(TRUE),
	mLevel(DEBUG),
	mPriority(DEBUG)
{
	mFilename[0] = '\0';
#if LL_WINDOWS
	// by default, turn off timestamps in the debug log on windows
	mErrorTimestamp = FALSE;
#endif
}

LLErrorBuffer::~LLErrorBuffer()
{
    delete mFile;
	mFile = NULL;
}

#if !LL_WINDOWS
int LLErrorBuffer::ELevelToSyslogPriority(const ELevel l)
{
		switch(l)
		{
		case DEBUG:
				return LOG_DEBUG;
		case INFO:
			switch(mElevatedRemote)
			{
			case TRUE: return LOG_NOTICE;
			default: return LOG_INFO;
			}
		case WARN:
				return LOG_WARNING;
		case FATAL:
				return LOG_CRIT;
		default:
				return LOG_CRIT;
		}
		return LOG_CRIT;
}
#endif // LL_WINDOWS

BOOL LLErrorBuffer::setFile(const char *filename)
{
    if (mFile == NULL)
	{
		mFile = new llofstream();
	}
    if (mFile->is_open())
	{
		mFile->close();
	}
	if (filename == NULL)
	{
		llwarns << "Input filename is NULL!!" << llendl;
		return FALSE;
	}
    mFile->open(filename, llofstream::out | llofstream::app);	/* Flawfinder: ignore */
    if (mFile->is_open())
	{
		mFileActive = TRUE;
	}
    else
	{
		mFileActive = FALSE;
		delete mFile;
		mFile = NULL;
    }
	snprintf(mFilename, sizeof(mFilename), filename);	/* Flawfinder: ignore */
    return mFileActive;
}

void LLErrorBuffer::closeFile()
{
    if (mFile && mFile->is_open())
	{
		mFile->close();
		mFileActive = FALSE;
		delete mFile;
		mFile = NULL;
	}
}

const char * LLErrorBuffer::getFilename() const
{
	return mFilename;
}

void LLErrorBuffer::setUTCTimestamp(BOOL utc)
{
	mIsUTC = utc;
}

void LLErrorBuffer::enableError(BOOL active)
{
    mErrorActive = active;
}

void LLErrorBuffer::enableErrorTimestamp(BOOL active)
{
	mErrorTimestamp = active;
}

void LLErrorBuffer::enableFile(BOOL active)
{
    if (mFile != NULL)
    {
	if (mFile->is_open())
	    mFileActive = active;
    }
    else
	mFileActive = FALSE;
}

#if !LL_WINDOWS
void LLErrorBuffer::enableSyslog(BOOL active)
{
    mSyslogActive = active;
}
#endif // LL_WINDOWS

#if LL_WINDOWS
void LLErrorBuffer::enableWinDebug(BOOL active)
{
	mWinDebugActive = active;
}
#endif // LL_WINDOWS

int LLErrorBuffer::overflow(int c)
{
    if (EOF != c)
    {
			if ('\n' == c)
			{
					// If we're not supposed to print anything, don't, but
					// pretend that we did so taht the iostream doesn't think
					// there's been a failure
					if (mPriority < mLevel)
					{
							// Flush our message buffer
							mBuf = "";
							return 0;
					}
#if !LL_WINDOWS
					if (mSyslogActive)
					{
							int pri = ELevelToSyslogPriority(mPriority);
							syslog(pri, "%s", mBuf.c_str());
					}
#endif // LL_WINDOWS
					const S32 BUF_SIZE = 64;
					char time_str[BUF_SIZE];	/* Flawfinder: ignore */
					if (mFileActive || mErrorActive)
					{
							time_t now;
							time(&now);
							S32 chars;
							if(mIsUTC)
							{
								chars = (S32)strftime(time_str, BUF_SIZE, 
													  "%Y-%m-%dT%H:%M:%SZ",
													  gmtime(&now));
							}
							else
							{
								chars = (S32)strftime(time_str, BUF_SIZE, 
													  "%Y-%m-%dT%H:%M:%S %Z",
													  localtime(&now));
							}
							if (0 == chars)
							{
								strcpy(time_str, "time error");	/* Flawfinder: ignore */
							}
					}
					if (mFileActive)
					{
						*mFile << time_str << " " << mBuf << std::endl;
					}
					if (mErrorActive)
					{
						if (mErrorTimestamp)
						{
							fprintf(stderr, "%s %s\n", time_str, mBuf.c_str());
						}
						else
						{
							fprintf(stderr, "%s\n", mBuf.c_str());
						}

						// std::cerr goes into the void on the viewer
						//std::cerr << time_str << ' ' << mBuf << std::endl;
					}
					if (mFixedBuf)
					{
						mFixedBuf->addLine(mBuf.c_str());
					}
#if LL_WINDOWS
					if (mWinDebugActive)
					{
						llutf16string utf16str = wstring_to_utf16str(utf8str_to_wstring(mBuf));
						utf16str += '\n';
						OutputDebugString(utf16str.c_str());
					}
#endif // LL_WINDOWS
					// Is there a better way to truncate a string?
					mBuf.erase(0, mBuf.length()); // Hack, Linux doesn't implement clear()!
			}
			else
			{
					mBuf += c;
			}
	}
	return 0;
}

LLErrorBuffer::ELevel LLErrorBuffer::mergeLevel(const LLErrorBuffer::ELevel l)
{
		mLevel = llmin(mLevel, l);
		return mLevel;
}