1 //===-- MICmnStreamStderr.cpp ------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
11 #include "MICmnStreamStderr.h"
13 #include "MICmnResources.h"
17 // Details: CMICmnStreamStderr constructor.
23 CMICmnStreamStderr::CMICmnStreamStderr() {}
26 // Details: CMICmnStreamStderr destructor.
32 CMICmnStreamStderr::~CMICmnStreamStderr() { Shutdown(); }
35 // Details: Initialize resources for *this stderr stream.
38 // Return: MIstatus::success - Functional succeeded.
39 // MIstatus::failure - Functional failed.
42 bool CMICmnStreamStderr::Initialize() {
43 m_clientUsageRefCnt++;
46 return MIstatus::success;
48 bool bOk = MIstatus::success;
51 // Debugging / I/O issues with client.
52 // This is only required on Windows if you do not use ::flush(stderr). MI uses
54 // It trys to ensure the process attached to the stderr steam gets ALL the data.
55 //::setbuf( stderr, NULL );
60 return MIstatus::success;
64 // Details: Release resources for *this stderr stream.
67 // Return: MIstatus::success - Functional succeeded.
68 // MIstatus::failure - Functional failed.
71 bool CMICmnStreamStderr::Shutdown() {
72 if (--m_clientUsageRefCnt > 0)
73 return MIstatus::success;
76 return MIstatus::success;
78 ClrErrorDescription();
80 m_bInitialized = false;
82 return MIstatus::success;
86 // Details: Write text data to stderr. Prefix the message with "MI:". The text
88 // not need to include a carriage line return as this is added to the
90 // function also then passes the text data into the CMICmnLog logger.
92 // Args: vText - (R) Text data.
93 // vbSendToLog - (R) True = Yes send to the Log file too, false = do
95 // Return: MIstatus::success - Functional succeeded.
96 // MIstatus::failure - Functional failed.
99 bool CMICmnStreamStderr::Write(const CMIUtilString &vText,
100 const bool vbSendToLog /* = true */) {
101 if (vText.length() == 0)
102 return MIstatus::failure;
104 const CMIUtilString strPrefixed(CMIUtilString::Format(
105 "%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(),
108 return WritePriv(strPrefixed, vText, vbSendToLog);
112 // Details: Write an LLDB text message to stderr.
113 // The text data does not need to include a carriage line return as
115 // to the text. The function also then passes the text data into the
119 // Args: vText - (R) Text data.
120 // vbSendToLog - (R) True = Yes send to the Log file too, false = do
121 // not. (Dflt = true)
122 // Return: MIstatus::success - Functional succeeded.
123 // MIstatus::failure - Functional failed.
126 bool CMICmnStreamStderr::WriteLLDBMsg(const CMIUtilString &vText,
127 const bool vbSendToLog /* = true */) {
128 if (vText.length() == 0)
129 return MIstatus::failure;
131 const CMIUtilString strPrefixed(
132 CMIUtilString::Format("LLDB: %s", vText.c_str()));
134 return WritePriv(vText, strPrefixed, vbSendToLog);
138 // Details: Write text data to stderr. The text data does not need to
139 // include a carriage line return as this is added to the text. The
141 // then passes the text data into the CMICmnLog logger.
143 // Args: vText - (R) Text data. May be prefixed with MI app's short
145 // vTxtForLogFile - (R) Text data.
146 // vbSendToLog - (R) True = Yes send to the Log file too, false =
147 // do not. (Dflt = true)
148 // Return: MIstatus::success - Functional succeeded.
149 // MIstatus::failure - Functional failed.
152 bool CMICmnStreamStderr::WritePriv(const CMIUtilString &vText,
153 const CMIUtilString &vTxtForLogFile,
154 const bool vbSendToLog /* = true */) {
155 if (vText.length() == 0)
156 return MIstatus::failure;
158 bool bOk = MIstatus::success;
160 // Grab the stderr thread lock while we print
161 CMIUtilThreadLock _lock(m_mutex);
163 // Send this text to stderr
164 const MIint status = ::fputs(vText.c_str(), stderr);
166 const CMIUtilString errMsg(CMIUtilString::Format(
167 MIRSRC(IDS_STDERR_ERR_NOT_ALL_DATA_WRITTEN), vText.c_str()));
168 SetErrorDescription(errMsg);
169 bOk = MIstatus::failure;
171 ::fprintf(stderr, "\n");
175 // Send this text to the log
176 if (bOk && vbSendToLog)
177 bOk &= m_pLog->WriteLog(vTxtForLogFile);
184 // Details: Lock the availability of the stream stderr. Other users of *this
186 // be stalled until it is available (Unlock()).
189 // Return: MIstatus::success - Functional succeeded.
190 // MIstatus::failure - Functional failed.
193 bool CMICmnStreamStderr::Lock() {
195 return MIstatus::success;
199 // Details: Release a previously locked stderr.
202 // Return: MIstatus::success - Functional succeeded.
203 // MIstatus::failure - Functional failed.
206 bool CMICmnStreamStderr::Unlock() {
208 return MIstatus::success;
212 // Details: Take MI Driver text message and send to the stderr stream. Also
215 // Type: Static method.
216 // Args: vrTxt - (R) Text.
217 // Return: MIstatus::success - Functionality succeeded.
218 // MIstatus::failure - Functionality failed.
221 bool CMICmnStreamStderr::TextToStderr(const CMIUtilString &vrTxt) {
222 const bool bLock = CMICmnStreamStderr::Instance().Lock();
223 const bool bOk = bLock && CMICmnStreamStderr::Instance().Write(vrTxt);
224 bLock &&CMICmnStreamStderr::Instance().Unlock();
230 // Details: Take an LLDB message and send to the stderr stream. The message is
232 // an error message. The user has typed a command in to the Eclipse
234 // passing Eclipse) and this is the result message from LLDB back to
236 // Also output to the MI Log file.
237 // Type: Static method.
238 // Args: vrTxt - (R) Text.
239 // Return: MIstatus::success - Functionality succeeded.
240 // MIstatus::failure - Functionality failed.
243 bool CMICmnStreamStderr::LLDBMsgToConsole(const CMIUtilString &vrTxt) {
244 const bool bLock = CMICmnStreamStderr::Instance().Lock();
245 const bool bOk = bLock && CMICmnStreamStderr::Instance().WriteLLDBMsg(vrTxt);
246 bLock &&CMICmnStreamStderr::Instance().Unlock();