diff options
| author | Nat Goodspeed <nat@lindenlab.com> | 2019-11-12 16:59:08 -0500 | 
|---|---|---|
| committer | Nat Goodspeed <nat@lindenlab.com> | 2020-03-25 19:06:13 -0400 | 
| commit | 99d4ddc6687fff0fb93b16192c8f713766874bfe (patch) | |
| tree | 546190d364f90dc30800c3c3cc7a021a8c182eab /indra/llcommon | |
| parent | d94e4613cace2e1f0215126b11c5c84375337d44 (diff) | |
DRTVWR-476: Back out e913c05d43b6: unroll stderr redirection.
Diffstat (limited to 'indra/llcommon')
| -rw-r--r-- | indra/llcommon/llerror.cpp | 118 | 
1 files changed, 39 insertions, 79 deletions
| diff --git a/indra/llcommon/llerror.cpp b/indra/llcommon/llerror.cpp index c0e1e443c4..188b76bbae 100644 --- a/indra/llcommon/llerror.cpp +++ b/indra/llcommon/llerror.cpp @@ -53,46 +53,6 @@  #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 - -namespace LLError -{ - -	class SettingsConfig; -	typedef LLPointer<SettingsConfig> SettingsConfigPtr; - -	class Settings : public LLSingleton<Settings> -	{ -		LLSINGLETON(Settings); -	public: -		SettingsConfigPtr getSettingsConfig(); -		~Settings(); - -		void reset(); -		SettingsStoragePtr saveAndReset(); -		void restore(SettingsStoragePtr pSettingsStorage); - -		int getDupStderr() const; - -	private: -		SettingsConfigPtr mSettingsConfig; -		int mDupStderr; -	}; - -} // namespace LLError -  namespace {  #if LL_WINDOWS  	void debugger_print(const std::string& s) @@ -160,8 +120,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)  			{ @@ -169,19 +128,25 @@ namespace {  			}  			else  			{ +#if LL_DARWIN || LL_LINUX  				// 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)); +				// But first, save the original stream in case we want it later. +				mSavedStderr = ::dup(STDERR_FILENO); +				::dup2(::fileno(mFile), STDERR_FILENO); +#endif  			}  		}  		~RecordToFile()  		{ +#if LL_DARWIN || LL_LINUX  			// restore stderr to its original fileno so any subsequent output  			// to stderr goes to original stream -			fhdup2(mSavedStderr, fhfileno(stderr)); +			::dup2(mSavedStderr, STDERR_FILENO); +#endif  			mFile.close();  		} @@ -201,8 +166,7 @@ namespace {  		virtual void recordMessage(LLError::ELevel level,  									const std::string& message) override  		{ -			::fwrite(message.c_str(), sizeof(char), message.length(), mFile); -			::fputc('\n', mFile); +			fwrite(message.c_str(), sizeof(char), message.length(), mFile);  			if (LLError::getAlwaysFlush())  			{  				::fflush(mFile); @@ -212,26 +176,22 @@ namespace {  	private:  		const std::string mName;  		LLUniqueFile mFile; -		int mSavedStderr; +		int mSavedStderr{0};  	};  	class RecordToStderr : public LLError::Recorder  	{  	public: -		RecordToStderr(bool timestamp) : -			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")) -		{ -			this->showMultiline(true); -		} - -		virtual bool enabled() override +		RecordToStderr(bool timestamp) : mUseANSI(checkANSI())   		{ -			return LLError::getEnabledLogTypesMask() & 0x04; +            this->showMultiline(true);  		} +		 +        virtual bool enabled() override +        { +            return LLError::getEnabledLogTypesMask() & 0x04; +        }  		virtual void recordMessage(LLError::ELevel level,  					   const std::string& message) override @@ -254,18 +214,17 @@ namespace {  					break;  				}  			} -			fprintf(mStderr, "%s\n", message.c_str()); +			fprintf(stderr, "%s\n", message.c_str());  			if (mUseANSI) colorANSI("0"); // reset  		} - +	  	private:  		bool mUseANSI; -		LLFILE* mStderr;  		void colorANSI(const std::string color)  		{  			// ANSI color code escape sequence -			fprintf(mStderr, "\033[%sm", color.c_str() ); +			fprintf(stderr, "\033[%sm", color.c_str() );  		};  		static bool checkANSI(void) @@ -274,7 +233,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(2)) &&  				(NULL == getenv("LL_NO_ANSI_COLOR"));  #endif // LL_LINUX  			return false; @@ -545,6 +504,22 @@ namespace LLError  		SettingsConfig();  	}; +	typedef LLPointer<SettingsConfig> SettingsConfigPtr; + +	class Settings : public LLSingleton<Settings> +	{ +		LLSINGLETON(Settings); +	public: +		SettingsConfigPtr getSettingsConfig(); + +		void reset(); +		SettingsStoragePtr saveAndReset(); +		void restore(SettingsStoragePtr pSettingsStorage); +		 +	private: +		SettingsConfigPtr mSettingsConfig; +	}; +  	SettingsConfig::SettingsConfig()  		: LLRefCount(),  		mDefaultLevel(LLError::LEVEL_DEBUG), @@ -568,18 +543,8 @@ namespace LLError  	}  	Settings::Settings(): -		mSettingsConfig(new SettingsConfig()), -		// duplicate stderr file handle right away -		mDupStderr(fhdup(fhfileno(stderr))) -	{ -	} - -	Settings::~Settings() +		mSettingsConfig(new SettingsConfig())  	{ -		// restore original stderr -		fhdup2(mDupStderr, fhfileno(stderr)); -		// and close the duplicate -		fhclose(mDupStderr);  	}  	SettingsConfigPtr Settings::getSettingsConfig() @@ -607,11 +572,6 @@ namespace LLError  		mSettingsConfig = newSettingsConfig;  	} -	int Settings::getDupStderr() const -	{ -		return mDupStderr; -	} -  	bool is_available()  	{  		return Settings::instanceExists() && Globals::instanceExists(); | 
