1 //===-- MIUtilStreamStdout.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: MIUtilStreamcStdout.cpp
13 // Overview: CMICmnStreamStdout implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmnStreamStdout.h"
25 #include "MICmnResources.h"
28 //++ ------------------------------------------------------------------------------------
29 // Details: CMICmnStreamStdout constructor.
35 CMICmnStreamStdout::CMICmnStreamStdout(void)
39 //++ ------------------------------------------------------------------------------------
40 // Details: CMICmnStreamStdout destructor.
46 CMICmnStreamStdout::~CMICmnStreamStdout(void)
51 //++ ------------------------------------------------------------------------------------
52 // Details: Initialize resources for *this Stdout stream.
55 // Return: MIstatus::success - Functional succeeded.
56 // MIstatus::failure - Functional failed.
60 CMICmnStreamStdout::Initialize(void)
62 m_clientUsageRefCnt++;
65 return MIstatus::success;
67 bool bOk = MIstatus::success;
70 // Debugging / I/O issues with client.
71 // This is only required on Windows if you do not use ::flush(stdout). MI uses
73 // It trys to ensure the process attached to the stdout steam gets ALL the data.
74 //::setbuf( stdout, NULL );
79 return MIstatus::success;
82 //++ ------------------------------------------------------------------------------------
83 // Details: Release resources for *this Stdout stream.
86 // Return: MIstatus::success - Functional succeeded.
87 // MIstatus::failure - Functional failed.
91 CMICmnStreamStdout::Shutdown(void)
93 if (--m_clientUsageRefCnt > 0)
94 return MIstatus::success;
97 return MIstatus::success;
99 ClrErrorDescription();
101 m_bInitialized = false;
103 return MIstatus::success;
106 //++ ------------------------------------------------------------------------------------
107 // Details: Write an MI format type response to stdout. The text data does not need to
108 // include a carrage line return as this is added to the text. The function also
109 // then passes the text data into the CMICmnLog logger.
111 // Args: vText - (R) MI formatted text.
112 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
113 // Return: MIstatus::success - Functional succeeded.
114 // MIstatus::failure - Functional failed.
118 CMICmnStreamStdout::WriteMIResponse(const CMIUtilString &vText, const bool vbSendToLog /* = true */)
120 return WritePriv(vText, vText, vbSendToLog);
123 //++ ------------------------------------------------------------------------------------
124 // Details: Write text data to stdout. The text data does not need to
125 // include a carrage line return as this is added to the text. The function also
126 // then passes the text data into the CMICmnLog logger.
128 // Args: vText - (R) Text data.
129 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
130 // Return: MIstatus::success - Functional succeeded.
131 // MIstatus::failure - Functional failed.
135 CMICmnStreamStdout::Write(const CMIUtilString &vText, const bool vbSendToLog /* = true */)
137 if (vText.length() == 0)
138 return MIstatus::failure;
140 const CMIUtilString strPrefixed(CMIUtilString::Format("%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(), vText.c_str()));
142 return WritePriv(strPrefixed, vText, vbSendToLog);
145 //++ ------------------------------------------------------------------------------------
146 // Details: Write text data to stdout. The text data does not need to
147 // include a carrage line return as this is added to the text. The function also
148 // then passes the text data into the CMICmnLog logger.
150 // Args: vText - (R) Text data prefixed with MI app's short name.
151 // vTxtForLogFile - (R) Text data.
152 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
153 // Return: MIstatus::success - Functional succeeded.
154 // MIstatus::failure - Functional failed.
158 CMICmnStreamStdout::WritePriv(const CMIUtilString &vText, const CMIUtilString &vTxtForLogFile, const bool vbSendToLog /* = true */)
160 if (vText.length() == 0)
161 return MIstatus::failure;
163 bool bOk = MIstatus::success;
165 // Grab the stdout thread lock while we print
166 CMIUtilThreadLock _lock(m_mutex);
168 // Send this text to stdout
169 const MIint status = ::fputs(vText.c_str(), stdout);
172 const CMIUtilString errMsg(CMIUtilString::Format(MIRSRC(IDS_STDOUT_ERR_NOT_ALL_DATA_WRITTEN), vText.c_str()));
173 SetErrorDescription(errMsg);
174 bOk = MIstatus::failure;
178 ::fprintf(stdout, "\n");
182 // Send this text to the log
183 if (bOk && vbSendToLog)
184 bOk &= m_pLog->WriteLog(vTxtForLogFile);
190 //++ ------------------------------------------------------------------------------------
191 // Details: Lock the availability of the stream stdout. Other users of *this stream will
192 // be stalled until it is available (Unlock()).
195 // Return: MIstatus::success - Functional succeeded.
196 // MIstatus::failure - Functional failed.
200 CMICmnStreamStdout::Lock(void)
203 return MIstatus::success;
206 //++ ------------------------------------------------------------------------------------
207 // Details: Release a previously locked stdout.
210 // Return: MIstatus::success - Functional succeeded.
211 // MIstatus::failure - Functional failed.
215 CMICmnStreamStdout::Unlock(void)
218 return MIstatus::success;
221 //++ ------------------------------------------------------------------------------------
222 // Details: Take a text data and send to the stdout stream. Also output to the MI Log
224 // Type: Static method.
225 // Args: vrTxt - (R) Text.
226 // Return: MIstatus::success - Functionality succeeded.
227 // MIstatus::failure - Functionality failed.
231 CMICmnStreamStdout::TextToStdout(const CMIUtilString &vrTxt)
233 const bool bLock = CMICmnStreamStdout::Instance().Lock();
234 const bool bOk = bLock && CMICmnStreamStdout::Instance().WriteMIResponse(vrTxt);
235 bLock &&CMICmnStreamStdout::Instance().Unlock();