1 //===-- MICmnStreamStdout.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 "MICmnStreamStdout.h"
13 #include "MICmnResources.h"
17 //------------------------------------------------------------------------------------
18 // Details: CMICmnStreamStdout constructor.
24 CMICmnStreamStdout::CMICmnStreamStdout() {}
27 //------------------------------------------------------------------------------------
28 // Details: CMICmnStreamStdout destructor.
34 CMICmnStreamStdout::~CMICmnStreamStdout() { Shutdown(); }
37 //------------------------------------------------------------------------------------
38 // Details: Initialize resources for *this Stdout stream.
41 // Return: MIstatus::success - Functional succeeded.
42 // MIstatus::failure - Functional failed.
45 bool CMICmnStreamStdout::Initialize() {
46 m_clientUsageRefCnt++;
49 return MIstatus::success;
51 bool bOk = MIstatus::success;
54 // Debugging / I/O issues with client.
55 // This is only required on Windows if you do not use ::flush(stdout). MI uses
57 // It trys to ensure the process attached to the stdout steam gets ALL the data.
58 //::setbuf( stdout, NULL );
63 return MIstatus::success;
67 //------------------------------------------------------------------------------------
68 // Details: Release resources for *this Stdout stream.
71 // Return: MIstatus::success - Functional succeeded.
72 // MIstatus::failure - Functional failed.
75 bool CMICmnStreamStdout::Shutdown() {
76 if (--m_clientUsageRefCnt > 0)
77 return MIstatus::success;
80 return MIstatus::success;
82 ClrErrorDescription();
84 m_bInitialized = false;
86 return MIstatus::success;
90 //------------------------------------------------------------------------------------
91 // Details: Write an MI format type response to stdout. The text data does not
93 // include a carriage line return as this is added to the text. The
95 // then passes the text data into the CMICmnLog logger.
97 // Args: vText - (R) MI formatted text.
98 // vbSendToLog - (R) True = Yes send to the Log file too, false = do
100 // Return: MIstatus::success - Functional succeeded.
101 // MIstatus::failure - Functional failed.
104 bool CMICmnStreamStdout::WriteMIResponse(const CMIUtilString &vText,
105 const bool vbSendToLog /* = true */) {
106 return WritePriv(vText, vText, vbSendToLog);
110 //------------------------------------------------------------------------------------
111 // Details: Write text data to stdout. The text data does not need to
112 // include a carriage line return as this is added to the text. The
114 // then passes the text data into the CMICmnLog logger.
116 // Args: vText - (R) Text data.
117 // vbSendToLog - (R) True = Yes send to the Log file too, false = do
118 // not. (Dflt = true)
119 // Return: MIstatus::success - Functional succeeded.
120 // MIstatus::failure - Functional failed.
123 bool CMICmnStreamStdout::Write(const CMIUtilString &vText,
124 const bool vbSendToLog /* = true */) {
125 if (vText.length() == 0)
126 return MIstatus::failure;
128 const CMIUtilString strPrefixed(CMIUtilString::Format(
129 "%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(),
132 return WritePriv(strPrefixed, vText, vbSendToLog);
136 //------------------------------------------------------------------------------------
137 // Details: Write text data to stdout. The text data does not need to
138 // include a carriage line return as this is added to the text. The
140 // then passes the text data into the CMICmnLog logger.
142 // Args: vText - (R) Text data prefixed with MI app's short name.
143 // vTxtForLogFile - (R) Text data.
144 // vbSendToLog - (R) True = Yes send to the Log file too, false =
145 // do not. (Dflt = true)
146 // Return: MIstatus::success - Functional succeeded.
147 // MIstatus::failure - Functional failed.
150 bool CMICmnStreamStdout::WritePriv(const CMIUtilString &vText,
151 const CMIUtilString &vTxtForLogFile,
152 const bool vbSendToLog /* = true */) {
153 if (vText.length() == 0)
154 return MIstatus::failure;
156 bool bOk = MIstatus::success;
158 // Grab the stdout thread lock while we print
159 CMIUtilThreadLock _lock(m_mutex);
161 // Send this text to stdout
162 const MIint status = ::fputs(vText.c_str(), stdout);
164 // Don't call the CMICmnBase::SetErrorDescription() because it will cause
166 // CMICmnBase::SetErrorDescription -> CMICmnStreamStdout::Write ->
167 // CMICmnStreamStdout::WritePriv -> CMICmnBase::SetErrorDescription
168 bOk = MIstatus::failure;
170 ::fprintf(stdout, "\n");
174 // Send this text to the log
175 if (bOk && vbSendToLog)
176 bOk &= m_pLog->WriteLog(vTxtForLogFile);
183 //------------------------------------------------------------------------------------
184 // Details: Lock the availability of the stream stdout. 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 CMICmnStreamStdout::Lock() {
195 return MIstatus::success;
199 //------------------------------------------------------------------------------------
200 // Details: Release a previously locked stdout.
203 // Return: MIstatus::success - Functional succeeded.
204 // MIstatus::failure - Functional failed.
207 bool CMICmnStreamStdout::Unlock() {
209 return MIstatus::success;
213 //------------------------------------------------------------------------------------
214 // Details: Take a text data and send to the stdout stream. Also output to the
217 // Type: Static method.
218 // Args: vrTxt - (R) Text.
219 // Return: MIstatus::success - Functionality succeeded.
220 // MIstatus::failure - Functionality failed.
223 bool CMICmnStreamStdout::TextToStdout(const CMIUtilString &vrTxt) {
224 const bool bSendToLog = true;
225 return CMICmnStreamStdout::Instance().WriteMIResponse(vrTxt, bSendToLog);
229 //------------------------------------------------------------------------------------
230 // Details: Write prompt to stdout if it's enabled.
231 // Type: Static method.
233 // Return: MIstatus::success - Function succeeded.
234 // MIstatus::failure - Function failed.
237 bool CMICmnStreamStdout::WritePrompt() {
238 const CMICmnStreamStdin &rStdinMan = CMICmnStreamStdin::Instance();
239 if (rStdinMan.GetEnablePrompt())
240 return TextToStdout(rStdinMan.GetPrompt());
241 return MIstatus::success;