llerror.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:29k
- /**
- * @file llerror.cpp
- * @date December 2006
- * @brief error message system
- *
- * $LicenseInfo:firstyear=2006&license=viewergpl$
- *
- * Copyright (c) 2006-2010, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * 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://secondlifegrid.net/programs/open_source/licensing/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://secondlifegrid.net/programs/open_source/licensing/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.
- * $/LicenseInfo$
- */
- #include "linden_common.h"
- #include "llerror.h"
- #include "llerrorcontrol.h"
- #include <cctype>
- #ifdef __GNUC__
- # include <cxxabi.h>
- #endif // __GNUC__
- #include <sstream>
- #if !LL_WINDOWS
- # include <syslog.h>
- # include <unistd.h>
- #endif // !LL_WINDOWS
- #include <vector>
- #include "llapp.h"
- #include "llapr.h"
- #include "llfile.h"
- #include "lllivefile.h"
- #include "llsd.h"
- #include "llsdserialize.h"
- #include "llstl.h"
- #include "lltimer.h"
- namespace {
- #if !LL_WINDOWS
- class RecordToSyslog : public LLError::Recorder
- {
- public:
- RecordToSyslog(const std::string& identity)
- : mIdentity(identity)
- {
- openlog(mIdentity.c_str(), LOG_CONS|LOG_PID, LOG_LOCAL0);
- // we need to set the string from a local copy of the string
- // since apparanetly openlog expects the const char* to remain
- // valid even after it returns (presumably until closelog)
- }
-
- ~RecordToSyslog()
- {
- closelog();
- }
-
- virtual void recordMessage(LLError::ELevel level,
- const std::string& message)
- {
- int syslogPriority = LOG_CRIT;
- switch (level) {
- case LLError::LEVEL_DEBUG: syslogPriority = LOG_DEBUG; break;
- case LLError::LEVEL_INFO: syslogPriority = LOG_INFO; break;
- case LLError::LEVEL_WARN: syslogPriority = LOG_WARNING; break;
- case LLError::LEVEL_ERROR: syslogPriority = LOG_CRIT; break;
- default: syslogPriority = LOG_CRIT;
- }
-
- syslog(syslogPriority, "%s", message.c_str());
- }
- private:
- std::string mIdentity;
- };
- #endif
- class RecordToFile : public LLError::Recorder
- {
- public:
- RecordToFile(const std::string& filename)
- {
- mFile.open(filename, llofstream::out | llofstream::app);
- if (!mFile)
- {
- llinfos << "Error setting log file to " << filename << llendl;
- }
- }
-
- ~RecordToFile()
- {
- mFile.close();
- }
-
- bool okay() { return mFile; }
-
- virtual bool wantsTime() { return true; }
-
- virtual void recordMessage(LLError::ELevel level,
- const std::string& message)
- {
- mFile << message << std::endl;
- // mFile.flush();
- // *FIX: should we do this?
- }
-
- private:
- llofstream mFile;
- };
-
-
- class RecordToStderr : public LLError::Recorder
- {
- public:
- RecordToStderr(bool timestamp) : mTimestamp(timestamp), mUseANSI(ANSI_PROBE) { }
- virtual bool wantsTime() { return mTimestamp; }
-
- virtual void recordMessage(LLError::ELevel level,
- const std::string& message)
- {
- if (ANSI_PROBE == mUseANSI)
- mUseANSI = (checkANSI() ? ANSI_YES : ANSI_NO);
- if (ANSI_YES == mUseANSI)
- {
- // Default all message levels to bold so we can distinguish our own messages from those dumped by subprocesses and libraries.
- colorANSI("1"); // bold
- switch (level) {
- case LLError::LEVEL_ERROR:
- colorANSI("31"); // red
- break;
- case LLError::LEVEL_WARN:
- colorANSI("34"); // blue
- break;
- case LLError::LEVEL_DEBUG:
- colorANSI("35"); // magenta
- break;
- default:
- break;
- }
- }
- fprintf(stderr, "%sn", message.c_str());
- if (ANSI_YES == mUseANSI) colorANSI("0"); // reset
- }
-
- private:
- bool mTimestamp;
- enum ANSIState {ANSI_PROBE, ANSI_YES, ANSI_NO};
- ANSIState mUseANSI;
- void colorANSI(const std::string color)
- {
- // ANSI color code escape sequence
- fprintf(stderr, "