diff options
author | Nat Goodspeed <nat@lindenlab.com> | 2019-10-31 12:39:31 -0400 |
---|---|---|
committer | Nat Goodspeed <nat@lindenlab.com> | 2020-03-25 19:06:12 -0400 |
commit | ec2bd40d3e318baf6f22ee7a7ccbc57cb071af40 (patch) | |
tree | ecfa474cf777dcc65a0d2f9f9c0b73b53c0f66cc /indra/llcommon/llerror.cpp | |
parent | 5f1140c03c9677753bd4a0ebe60b48e37d28669e (diff) |
DRTVWR-476: Encapsulate dup()/dup2() fd saving as LLTempRedirect.
Diffstat (limited to 'indra/llcommon/llerror.cpp')
-rw-r--r-- | indra/llcommon/llerror.cpp | 48 |
1 files changed, 10 insertions, 38 deletions
diff --git a/indra/llcommon/llerror.cpp b/indra/llcommon/llerror.cpp index 457965b1fd..2ae2cb6cbc 100644 --- a/indra/llcommon/llerror.cpp +++ b/indra/llcommon/llerror.cpp @@ -39,8 +39,6 @@ #if !LL_WINDOWS # include <syslog.h> # include <unistd.h> -#else -# include <io.h> #endif // !LL_WINDOWS #include <vector> #include "string.h" @@ -54,20 +52,7 @@ #include "llsingleton.h" #include "llstl.h" #include "lltimer.h" - -#if LL_WINDOWS -#define fhclose _close -#define fhdup _dup -#define fhdup2 _dup2 -#define fhfdopen _fdopen -#define fhfileno _fileno -#else -#define fhclose ::close -#define fhdup ::dup -#define fhdup2 ::dup2 -#define fhfdopen ::fdopen -#define fhfileno ::fileno -#endif +#include "lltempredirect.h" namespace LLError { @@ -80,7 +65,6 @@ namespace LLError LLSINGLETON(Settings); public: SettingsConfigPtr getSettingsConfig(); - ~Settings(); void reset(); SettingsStoragePtr saveAndReset(); @@ -90,7 +74,7 @@ namespace LLError private: SettingsConfigPtr mSettingsConfig; - int mDupStderr; + LLTempRedirect mRedirect; }; } // namespace LLError @@ -162,8 +146,7 @@ namespace { public: RecordToFile(const std::string& filename): mName(filename), - mFile(LLFile::fopen(filename, "a")), - mSavedStderr(LLError::Settings::instance().getDupStderr()) + mFile(LLFile::fopen(filename, "a")) { if (!mFile) { @@ -174,16 +157,13 @@ namespace { // We use a number of classic-C libraries, some of which write // log output to stderr. The trouble with that is that unless // you launch the viewer from a console, stderr output is - // lost. Redirect STDERR_FILENO to write into this log file. - fhdup2(fhfileno(mFile), fhfileno(stderr)); + // lost. Redirect stderr to write into this log file. + mRedirect = LLTempRedirect(mFile, stderr); } } ~RecordToFile() { - // restore stderr to its original fileno so any subsequent output - // to stderr goes to original stream - fhdup2(mSavedStderr, fhfileno(stderr)); mFile.close(); } @@ -214,7 +194,7 @@ namespace { private: const std::string mName; LLUniqueFile mFile; - int mSavedStderr; + LLTempRedirect mRedirect; }; @@ -225,7 +205,7 @@ namespace { mUseANSI(checkANSI()), // use duplicate stderr file handle so THIS output isn't affected // by our internal redirection of all (other) stderr output - mStderr(fhfdopen(LLError::Settings::instance().getDupStderr(), "a")) + mStderr(llfd::open(LLError::Settings::instance().getDupStderr(), "a")) { this->showMultiline(true); } @@ -276,7 +256,7 @@ namespace { // Check whether it's okay to use ANSI; if stderr is // a tty then we assume yes. Can be turned off with // the LL_NO_ANSI_COLOR env var. - return (0 != isatty(fhfileno(stderr))) && + return (0 != isatty(fileno(stderr))) && (NULL == getenv("LL_NO_ANSI_COLOR")); #endif // LL_LINUX return false; @@ -572,16 +552,8 @@ namespace LLError Settings::Settings(): mSettingsConfig(new SettingsConfig()), // duplicate stderr file handle right away - mDupStderr(fhdup(fhfileno(stderr))) - { - } - - Settings::~Settings() + mRedirect(NULL, stderr) { - // restore original stderr - fhdup2(mDupStderr, fhfileno(stderr)); - // and close the duplicate - fhclose(mDupStderr); } SettingsConfigPtr Settings::getSettingsConfig() @@ -611,7 +583,7 @@ namespace LLError int Settings::getDupStderr() const { - return mDupStderr; + return mRedirect.getOriginalTarget(); } bool is_available() |