1 //===-- MICmnStreamStderr.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // File: MICmnStreamStderr.cpp
13 // Overview: CMICmnStreamStderr implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmnStreamStderr.h"
25 #include "MICmnResources.h"
28 //++ ------------------------------------------------------------------------------------
29 // Details: CMICmnStreamStderr constructor.
35 CMICmnStreamStderr::CMICmnStreamStderr( void )
39 //++ ------------------------------------------------------------------------------------
40 // Details: CMICmnStreamStderr destructor.
46 CMICmnStreamStderr::~CMICmnStreamStderr( void )
51 //++ ------------------------------------------------------------------------------------
52 // Details: Initialize resources for *this stderr stream.
55 // Return: MIstatus::success - Functional succeeded.
56 // MIstatus::failure - Functional failed.
59 bool CMICmnStreamStderr::Initialize( void )
61 m_clientUsageRefCnt++;
64 return MIstatus::success;
66 bool bOk = MIstatus::success;
69 // Debugging / I/O issues with client.
70 // This is only required on Windows if you do not use ::flush(stderr). MI uses
72 // It trys to ensure the process attached to the stderr steam gets ALL the data.
73 //::setbuf( stderr, NULL );
78 return MIstatus::success;
81 //++ ------------------------------------------------------------------------------------
82 // Details: Release resources for *this stderr stream.
85 // Return: MIstatus::success - Functional succeeded.
86 // MIstatus::failure - Functional failed.
89 bool CMICmnStreamStderr::Shutdown( void )
91 if( --m_clientUsageRefCnt > 0 )
92 return MIstatus::success;
95 return MIstatus::success;
97 ClrErrorDescription();
99 m_bInitialized = false;
101 return MIstatus::success;
104 //++ ------------------------------------------------------------------------------------
105 // Details: Write text data to stderr. Prefix the message with "MI:". The text data does
106 // not need to include a carrage line return as this is added to the text. The
107 // function also then passes the text data into the CMICmnLog logger.
109 // Args: vText - (R) Text data.
110 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
111 // Return: MIstatus::success - Functional succeeded.
112 // MIstatus::failure - Functional failed.
115 bool CMICmnStreamStderr::Write( const CMIUtilString & vText, const bool vbSendToLog /* = true */ )
117 if( vText.length() == 0 )
118 return MIstatus::failure;
120 const CMIUtilString strPrefixed( CMIUtilString::Format( "%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(), vText.c_str() ) );
122 return WritePriv( strPrefixed, vText, vbSendToLog );
125 //++ ------------------------------------------------------------------------------------
126 // Details: Write an LLDB text message to stderr.
127 // The text data does not need to include a carrage line return as this is added
128 // to the text. The function also then passes the text data into the CMICmnLog
131 // Args: vText - (R) Text data.
132 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
133 // Return: MIstatus::success - Functional succeeded.
134 // MIstatus::failure - Functional failed.
137 bool CMICmnStreamStderr::WriteLLDBMsg( const CMIUtilString & vText, const bool vbSendToLog /* = true */ )
139 if( vText.length() == 0 )
140 return MIstatus::failure;
142 const CMIUtilString strPrefixed( CMIUtilString::Format( "LLDB: %s", vText.c_str() ) );
144 return WritePriv( vText, strPrefixed, vbSendToLog );
147 //++ ------------------------------------------------------------------------------------
148 // Details: Write text data to stderr. The text data does not need to
149 // include a carrage line return as this is added to the text. The function also
150 // then passes the text data into the CMICmnLog logger.
152 // Args: vText - (R) Text data. May be prefixed with MI app's short name.
153 // vTxtForLogFile - (R) Text data.
154 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
155 // Return: MIstatus::success - Functional succeeded.
156 // MIstatus::failure - Functional failed.
159 bool CMICmnStreamStderr::WritePriv( const CMIUtilString & vText, const CMIUtilString & vTxtForLogFile, const bool vbSendToLog /* = true */ )
161 if( vText.length() == 0 )
162 return MIstatus::failure;
164 bool bOk = MIstatus::success;
166 // Grab the stderr thread lock while we print
167 CMIUtilThreadLock _lock( m_mutex );
169 // Send this text to stderr
170 const MIuint status = ::fputs( vText.c_str(), stderr );
173 const CMIUtilString errMsg( CMIUtilString::Format( MIRSRC( IDS_STDERR_ERR_NOT_ALL_DATA_WRITTEN ), vText.c_str() ) );
174 SetErrorDescription( errMsg );
175 bOk = MIstatus::failure;
179 ::fprintf( stderr, "\n" );
183 // Send this text to the log
184 if( bOk && vbSendToLog )
185 bOk &= m_pLog->WriteLog( vTxtForLogFile );
191 //++ ------------------------------------------------------------------------------------
192 // Details: Lock the availability of the stream stderr. Other users of *this stream will
193 // be stalled until it is available (Unlock()).
196 // Return: MIstatus::success - Functional succeeded.
197 // MIstatus::failure - Functional failed.
200 bool CMICmnStreamStderr::Lock( void )
203 return MIstatus::success;
206 //++ ------------------------------------------------------------------------------------
207 // Details: Release a previously locked stderr.
210 // Return: MIstatus::success - Functional succeeded.
211 // MIstatus::failure - Functional failed.
214 bool CMICmnStreamStderr::Unlock( void )
217 return MIstatus::success;
220 //++ ------------------------------------------------------------------------------------
221 // Details: Take MI Driver text message and send to the stderr stream. Also output to the
223 // Type: Static method.
224 // Args: vrTxt - (R) Text.
225 // Return: MIstatus::success - Functionality succeeded.
226 // MIstatus::failure - Functionality failed.
229 bool CMICmnStreamStderr::TextToStderr( const CMIUtilString & vrTxt )
231 const bool bLock = CMICmnStreamStderr::Instance().Lock();
232 const bool bOk = bLock && CMICmnStreamStderr::Instance().Write( vrTxt );
233 bLock && CMICmnStreamStderr::Instance().Unlock();
238 //++ ------------------------------------------------------------------------------------
239 // Details: Take an LLDB message and send to the stderr stream. The message is not always
240 // an error message. The user has typed a command in to the Eclipse console (by-
241 // passing Eclipse) and this is the result message from LLDB back to the user.
242 // Also output to the MI Log file.
243 // Type: Static method.
244 // Args: vrTxt - (R) Text.
245 // Return: MIstatus::success - Functionality succeeded.
246 // MIstatus::failure - Functionality failed.
249 bool CMICmnStreamStderr::LLDBMsgToConsole( const CMIUtilString & vrTxt )
251 const bool bLock = CMICmnStreamStderr::Instance().Lock();
252 const bool bOk = bLock && CMICmnStreamStderr::Instance().WriteLLDBMsg( vrTxt );
253 bLock && CMICmnStreamStderr::Instance().Unlock();