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 #include "MICmnStreamStderr.h"
13 #include "MICmnResources.h"
16 //++ ------------------------------------------------------------------------------------
17 // Details: CMICmnStreamStderr constructor.
23 CMICmnStreamStderr::CMICmnStreamStderr(void)
27 //++ ------------------------------------------------------------------------------------
28 // Details: CMICmnStreamStderr destructor.
34 CMICmnStreamStderr::~CMICmnStreamStderr(void)
39 //++ ------------------------------------------------------------------------------------
40 // Details: Initialize resources for *this stderr stream.
43 // Return: MIstatus::success - Functional succeeded.
44 // MIstatus::failure - Functional failed.
48 CMICmnStreamStderr::Initialize(void)
50 m_clientUsageRefCnt++;
53 return MIstatus::success;
55 bool bOk = MIstatus::success;
58 // Debugging / I/O issues with client.
59 // This is only required on Windows if you do not use ::flush(stderr). MI uses
61 // It trys to ensure the process attached to the stderr steam gets ALL the data.
62 //::setbuf( stderr, NULL );
67 return MIstatus::success;
70 //++ ------------------------------------------------------------------------------------
71 // Details: Release resources for *this stderr stream.
74 // Return: MIstatus::success - Functional succeeded.
75 // MIstatus::failure - Functional failed.
79 CMICmnStreamStderr::Shutdown(void)
81 if (--m_clientUsageRefCnt > 0)
82 return MIstatus::success;
85 return MIstatus::success;
87 ClrErrorDescription();
89 m_bInitialized = false;
91 return MIstatus::success;
94 //++ ------------------------------------------------------------------------------------
95 // Details: Write text data to stderr. Prefix the message with "MI:". The text data does
96 // not need to include a carriage line return as this is added to the text. The
97 // function also then passes the text data into the CMICmnLog logger.
99 // Args: vText - (R) Text data.
100 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
101 // Return: MIstatus::success - Functional succeeded.
102 // MIstatus::failure - Functional failed.
106 CMICmnStreamStderr::Write(const CMIUtilString &vText, const bool vbSendToLog /* = true */)
108 if (vText.length() == 0)
109 return MIstatus::failure;
111 const CMIUtilString strPrefixed(CMIUtilString::Format("%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(), vText.c_str()));
113 return WritePriv(strPrefixed, vText, vbSendToLog);
116 //++ ------------------------------------------------------------------------------------
117 // Details: Write an LLDB text message to stderr.
118 // The text data does not need to include a carriage line return as this is added
119 // to the text. The function also then passes the text data into the CMICmnLog
122 // Args: vText - (R) Text data.
123 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
124 // Return: MIstatus::success - Functional succeeded.
125 // MIstatus::failure - Functional failed.
129 CMICmnStreamStderr::WriteLLDBMsg(const CMIUtilString &vText, const bool vbSendToLog /* = true */)
131 if (vText.length() == 0)
132 return MIstatus::failure;
134 const CMIUtilString strPrefixed(CMIUtilString::Format("LLDB: %s", vText.c_str()));
136 return WritePriv(vText, strPrefixed, vbSendToLog);
139 //++ ------------------------------------------------------------------------------------
140 // Details: Write text data to stderr. The text data does not need to
141 // include a carriage line return as this is added to the text. The function also
142 // then passes the text data into the CMICmnLog logger.
144 // Args: vText - (R) Text data. May be prefixed with MI app's short name.
145 // vTxtForLogFile - (R) Text data.
146 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
147 // Return: MIstatus::success - Functional succeeded.
148 // MIstatus::failure - Functional failed.
152 CMICmnStreamStderr::WritePriv(const CMIUtilString &vText, const CMIUtilString &vTxtForLogFile, const bool vbSendToLog /* = true */)
154 if (vText.length() == 0)
155 return MIstatus::failure;
157 bool bOk = MIstatus::success;
159 // Grab the stderr thread lock while we print
160 CMIUtilThreadLock _lock(m_mutex);
162 // Send this text to stderr
163 const MIint status = ::fputs(vText.c_str(), stderr);
166 const CMIUtilString errMsg(CMIUtilString::Format(MIRSRC(IDS_STDERR_ERR_NOT_ALL_DATA_WRITTEN), vText.c_str()));
167 SetErrorDescription(errMsg);
168 bOk = MIstatus::failure;
172 ::fprintf(stderr, "\n");
176 // Send this text to the log
177 if (bOk && vbSendToLog)
178 bOk &= m_pLog->WriteLog(vTxtForLogFile);
184 //++ ------------------------------------------------------------------------------------
185 // Details: Lock the availability of the stream stderr. Other users of *this stream will
186 // be stalled until it is available (Unlock()).
189 // Return: MIstatus::success - Functional succeeded.
190 // MIstatus::failure - Functional failed.
194 CMICmnStreamStderr::Lock(void)
197 return MIstatus::success;
200 //++ ------------------------------------------------------------------------------------
201 // Details: Release a previously locked stderr.
204 // Return: MIstatus::success - Functional succeeded.
205 // MIstatus::failure - Functional failed.
209 CMICmnStreamStderr::Unlock(void)
212 return MIstatus::success;
215 //++ ------------------------------------------------------------------------------------
216 // Details: Take MI Driver text message and send to the stderr stream. Also output to the
218 // Type: Static method.
219 // Args: vrTxt - (R) Text.
220 // Return: MIstatus::success - Functionality succeeded.
221 // MIstatus::failure - Functionality failed.
225 CMICmnStreamStderr::TextToStderr(const CMIUtilString &vrTxt)
227 const bool bLock = CMICmnStreamStderr::Instance().Lock();
228 const bool bOk = bLock && CMICmnStreamStderr::Instance().Write(vrTxt);
229 bLock &&CMICmnStreamStderr::Instance().Unlock();
234 //++ ------------------------------------------------------------------------------------
235 // Details: Take an LLDB message and send to the stderr stream. The message is not always
236 // an error message. The user has typed a command in to the Eclipse console (by-
237 // passing Eclipse) and this is the result message from LLDB back to the user.
238 // Also output to the MI Log file.
239 // Type: Static method.
240 // Args: vrTxt - (R) Text.
241 // Return: MIstatus::success - Functionality succeeded.
242 // MIstatus::failure - Functionality failed.
246 CMICmnStreamStderr::LLDBMsgToConsole(const CMIUtilString &vrTxt)
248 const bool bLock = CMICmnStreamStderr::Instance().Lock();
249 const bool bOk = bLock && CMICmnStreamStderr::Instance().WriteLLDBMsg(vrTxt);
250 bLock &&CMICmnStreamStderr::Instance().Unlock();