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.
59 bool CMICmnStreamStdout::Initialize( void )
61 m_clientUsageRefCnt++;
64 return MIstatus::success;
66 bool bOk = MIstatus::success;
69 // Debugging / I/O issues with client.
70 // This is only required on Windows if you do not use ::flush(stdout). MI uses
72 // It trys to ensure the process attached to the stdout steam gets ALL the data.
73 //::setbuf( stdout, NULL );
78 return MIstatus::success;
81 //++ ------------------------------------------------------------------------------------
82 // Details: Release resources for *this Stdout stream.
85 // Return: MIstatus::success - Functional succeeded.
86 // MIstatus::failure - Functional failed.
89 bool CMICmnStreamStdout::Shutdown( void )
91 if( --m_clientUsageRefCnt > 0 )
92 return MIstatus::success;
95 return MIstatus::success;
97 ClrErrorDescription();
99 m_bInitialized = false;
101 return MIstatus::success;
104 //++ ------------------------------------------------------------------------------------
105 // Details: Write an MI format type response to stdout. The text data does not need to
106 // include a carrage line return as this is added to the text. The function also
107 // then passes the text data into the CMICmnLog logger.
109 // Args: vText - (R) MI formatted text.
110 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
111 // Return: MIstatus::success - Functional succeeded.
112 // MIstatus::failure - Functional failed.
115 bool CMICmnStreamStdout::WriteMIResponse( const CMIUtilString & vText, const bool vbSendToLog /* = true */ )
117 return WritePriv( vText, vText, vbSendToLog );
120 //++ ------------------------------------------------------------------------------------
121 // Details: Write text data to stdout. The text data does not need to
122 // include a carrage line return as this is added to the text. The function also
123 // then passes the text data into the CMICmnLog logger.
125 // Args: vText - (R) Text data.
126 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
127 // Return: MIstatus::success - Functional succeeded.
128 // MIstatus::failure - Functional failed.
131 bool CMICmnStreamStdout::Write( const CMIUtilString & vText, const bool vbSendToLog /* = true */ )
133 if( vText.length() == 0 )
134 return MIstatus::failure;
136 const CMIUtilString strPrefixed( CMIUtilString::Format( "%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(), vText.c_str() ) );
138 return WritePriv( strPrefixed, vText, vbSendToLog );
141 //++ ------------------------------------------------------------------------------------
142 // Details: Write text data to stdout. The text data does not need to
143 // include a carrage line return as this is added to the text. The function also
144 // then passes the text data into the CMICmnLog logger.
146 // Args: vText - (R) Text data prefixed with MI app's short name.
147 // vTxtForLogFile - (R) Text data.
148 // vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
149 // Return: MIstatus::success - Functional succeeded.
150 // MIstatus::failure - Functional failed.
153 bool CMICmnStreamStdout::WritePriv( const CMIUtilString & vText, const CMIUtilString & vTxtForLogFile, const bool vbSendToLog /* = true */ )
155 if( vText.length() == 0 )
156 return MIstatus::failure;
158 bool bOk = MIstatus::success;
160 // Grab the stdout thread lock while we print
161 CMIUtilThreadLock _lock( m_mutex );
163 // Send this text to stdout
164 const MIuint status = ::fputs( vText.c_str(), stdout );
167 const CMIUtilString errMsg( CMIUtilString::Format( MIRSRC( IDS_STDOUT_ERR_NOT_ALL_DATA_WRITTEN ), vText.c_str() ) );
168 SetErrorDescription( errMsg );
169 bOk = MIstatus::failure;
173 ::fprintf( stdout, "\n" );
177 // Send this text to the log
178 if( bOk && vbSendToLog )
179 bOk &= m_pLog->WriteLog( vTxtForLogFile );
185 //++ ------------------------------------------------------------------------------------
186 // Details: Lock the availability of the stream stdout. Other users of *this stream will
187 // be stalled until it is available (Unlock()).
190 // Return: MIstatus::success - Functional succeeded.
191 // MIstatus::failure - Functional failed.
194 bool CMICmnStreamStdout::Lock( void )
197 return MIstatus::success;
200 //++ ------------------------------------------------------------------------------------
201 // Details: Release a previously locked stdout.
204 // Return: MIstatus::success - Functional succeeded.
205 // MIstatus::failure - Functional failed.
208 bool CMICmnStreamStdout::Unlock( void )
211 return MIstatus::success;
214 //++ ------------------------------------------------------------------------------------
215 // Details: Take a text data and send to the stdout stream. Also output to the MI Log
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 )
225 const bool bLock = CMICmnStreamStdout::Instance().Lock();
226 const bool bOk = bLock && CMICmnStreamStdout::Instance().WriteMIResponse( vrTxt );
227 bLock && CMICmnStreamStdout::Instance().Unlock();