1 //===-- MICmnStreamStderr.cpp ------------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
12 #include "MICmnStreamStderr.h"
14 #include "MICmnResources.h"
18 //------------------------------------------------------------------------------------
19 // Details: CMICmnStreamStderr constructor.
25 CMICmnStreamStderr::CMICmnStreamStderr() {}
28 //------------------------------------------------------------------------------------
29 // Details: CMICmnStreamStderr destructor.
35 CMICmnStreamStderr::~CMICmnStreamStderr() { Shutdown(); }
38 //------------------------------------------------------------------------------------
39 // Details: Initialize resources for *this stderr stream.
42 // Return: MIstatus::success - Functional succeeded.
43 // MIstatus::failure - Functional failed.
46 bool CMICmnStreamStderr::Initialize() {
47 m_clientUsageRefCnt++;
50 return MIstatus::success;
52 bool bOk = MIstatus::success;
55 // Debugging / I/O issues with client.
56 // This is only required on Windows if you do not use ::flush(stderr). MI uses
58 // It trys to ensure the process attached to the stderr steam gets ALL the data.
59 //::setbuf( stderr, NULL );
64 return MIstatus::success;
68 //------------------------------------------------------------------------------------
69 // Details: Release resources for *this stderr stream.
72 // Return: MIstatus::success - Functional succeeded.
73 // MIstatus::failure - Functional failed.
76 bool CMICmnStreamStderr::Shutdown() {
77 if (--m_clientUsageRefCnt > 0)
78 return MIstatus::success;
81 return MIstatus::success;
83 ClrErrorDescription();
85 m_bInitialized = false;
87 return MIstatus::success;
91 //------------------------------------------------------------------------------------
92 // Details: Write text data to stderr. Prefix the message with "MI:". The text
94 // not need to include a carriage line return as this is added to the
96 // function also then passes the text data into the CMICmnLog logger.
98 // Args: vText - (R) Text data.
99 // vbSendToLog - (R) True = Yes send to the Log file too, false = do
100 // not. (Dflt = true)
101 // Return: MIstatus::success - Functional succeeded.
102 // MIstatus::failure - Functional failed.
105 bool CMICmnStreamStderr::Write(const CMIUtilString &vText,
106 const bool vbSendToLog /* = true */) {
107 if (vText.length() == 0)
108 return MIstatus::failure;
110 const CMIUtilString strPrefixed(CMIUtilString::Format(
111 "%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(),
114 return WritePriv(strPrefixed, vText, vbSendToLog);
118 //------------------------------------------------------------------------------------
119 // Details: Write an LLDB text message to stderr.
120 // The text data does not need to include a carriage line return as
122 // to the text. The function also then passes the text data into the
126 // Args: vText - (R) Text data.
127 // vbSendToLog - (R) True = Yes send to the Log file too, false = do
128 // not. (Dflt = true)
129 // Return: MIstatus::success - Functional succeeded.
130 // MIstatus::failure - Functional failed.
133 bool CMICmnStreamStderr::WriteLLDBMsg(const CMIUtilString &vText,
134 const bool vbSendToLog /* = true */) {
135 if (vText.length() == 0)
136 return MIstatus::failure;
138 const CMIUtilString strPrefixed(
139 CMIUtilString::Format("LLDB: %s", vText.c_str()));
141 return WritePriv(vText, strPrefixed, vbSendToLog);
145 //------------------------------------------------------------------------------------
146 // Details: Write text data to stderr. The text data does not need to
147 // include a carriage line return as this is added to the text. The
149 // then passes the text data into the CMICmnLog logger.
151 // Args: vText - (R) Text data. May be prefixed with MI app's short
153 // vTxtForLogFile - (R) Text data.
154 // vbSendToLog - (R) True = Yes send to the Log file too, false =
155 // do not. (Dflt = true)
156 // Return: MIstatus::success - Functional succeeded.
157 // MIstatus::failure - Functional failed.
160 bool CMICmnStreamStderr::WritePriv(const CMIUtilString &vText,
161 const CMIUtilString &vTxtForLogFile,
162 const bool vbSendToLog /* = true */) {
163 if (vText.length() == 0)
164 return MIstatus::failure;
166 bool bOk = MIstatus::success;
168 // Grab the stderr thread lock while we print
169 CMIUtilThreadLock _lock(m_mutex);
171 // Send this text to stderr
172 const MIint status = ::fputs(vText.c_str(), stderr);
174 const CMIUtilString errMsg(CMIUtilString::Format(
175 MIRSRC(IDS_STDERR_ERR_NOT_ALL_DATA_WRITTEN), vText.c_str()));
176 SetErrorDescription(errMsg);
177 bOk = MIstatus::failure;
179 ::fprintf(stderr, "\n");
183 // Send this text to the log
184 if (bOk && vbSendToLog)
185 bOk &= m_pLog->WriteLog(vTxtForLogFile);
192 //------------------------------------------------------------------------------------
193 // Details: Lock the availability of the stream stderr. Other users of *this
195 // be stalled until it is available (Unlock()).
198 // Return: MIstatus::success - Functional succeeded.
199 // MIstatus::failure - Functional failed.
202 bool CMICmnStreamStderr::Lock() {
204 return MIstatus::success;
208 //------------------------------------------------------------------------------------
209 // Details: Release a previously locked stderr.
212 // Return: MIstatus::success - Functional succeeded.
213 // MIstatus::failure - Functional failed.
216 bool CMICmnStreamStderr::Unlock() {
218 return MIstatus::success;
222 //------------------------------------------------------------------------------------
223 // Details: Take MI Driver text message and send to the stderr stream. Also
226 // Type: Static method.
227 // Args: vrTxt - (R) Text.
228 // Return: MIstatus::success - Functionality succeeded.
229 // MIstatus::failure - Functionality failed.
232 bool CMICmnStreamStderr::TextToStderr(const CMIUtilString &vrTxt) {
233 const bool bLock = CMICmnStreamStderr::Instance().Lock();
234 const bool bOk = bLock && CMICmnStreamStderr::Instance().Write(vrTxt);
235 bLock &&CMICmnStreamStderr::Instance().Unlock();
241 //------------------------------------------------------------------------------------
242 // Details: Take an LLDB message and send to the stderr stream. The message is
244 // an error message. The user has typed a command in to the Eclipse
246 // passing Eclipse) and this is the result message from LLDB back to
248 // Also output to the MI Log file.
249 // Type: Static method.
250 // Args: vrTxt - (R) Text.
251 // Return: MIstatus::success - Functionality succeeded.
252 // MIstatus::failure - Functionality failed.
255 bool CMICmnStreamStderr::LLDBMsgToConsole(const CMIUtilString &vrTxt) {
256 const bool bLock = CMICmnStreamStderr::Instance().Lock();
257 const bool bOk = bLock && CMICmnStreamStderr::Instance().WriteLLDBMsg(vrTxt);
258 bLock &&CMICmnStreamStderr::Instance().Unlock();