1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
//===-- MIUtilStreamStdout.cpp ----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// In-house headers:
#include "MICmnStreamStdout.h"
#include "MICmnLog.h"
#include "MICmnResources.h"
#include "MIDriver.h"
//++ ------------------------------------------------------------------------------------
// Details: CMICmnStreamStdout constructor.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
CMICmnStreamStdout::CMICmnStreamStdout(void)
{
}
//++ ------------------------------------------------------------------------------------
// Details: CMICmnStreamStdout destructor.
// Type: Overridable.
// Args: None.
// Return: None.
// Throws: None.
//--
CMICmnStreamStdout::~CMICmnStreamStdout(void)
{
Shutdown();
}
//++ ------------------------------------------------------------------------------------
// Details: Initialize resources for *this Stdout stream.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::Initialize(void)
{
m_clientUsageRefCnt++;
if (m_bInitialized)
return MIstatus::success;
bool bOk = MIstatus::success;
#ifdef _MSC_VER
// Debugging / I/O issues with client.
// This is only required on Windows if you do not use ::flush(stdout). MI uses
// ::flush(stdout)
// It trys to ensure the process attached to the stdout steam gets ALL the data.
//::setbuf( stdout, NULL );
#endif // _MSC_VER
m_bInitialized = bOk;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Release resources for *this Stdout stream.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::Shutdown(void)
{
if (--m_clientUsageRefCnt > 0)
return MIstatus::success;
if (!m_bInitialized)
return MIstatus::success;
ClrErrorDescription();
m_bInitialized = false;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Write an MI format type response to stdout. The text data does not need to
// include a carriage line return as this is added to the text. The function also
// then passes the text data into the CMICmnLog logger.
// Type: Method.
// Args: vText - (R) MI formatted text.
// vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::WriteMIResponse(const CMIUtilString &vText, const bool vbSendToLog /* = true */)
{
return WritePriv(vText, vText, vbSendToLog);
}
//++ ------------------------------------------------------------------------------------
// Details: Write text data to stdout. The text data does not need to
// include a carriage line return as this is added to the text. The function also
// then passes the text data into the CMICmnLog logger.
// Type: Method.
// Args: vText - (R) Text data.
// vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::Write(const CMIUtilString &vText, const bool vbSendToLog /* = true */)
{
if (vText.length() == 0)
return MIstatus::failure;
const CMIUtilString strPrefixed(CMIUtilString::Format("%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(), vText.c_str()));
return WritePriv(strPrefixed, vText, vbSendToLog);
}
//++ ------------------------------------------------------------------------------------
// Details: Write text data to stdout. The text data does not need to
// include a carriage line return as this is added to the text. The function also
// then passes the text data into the CMICmnLog logger.
// Type: Method.
// Args: vText - (R) Text data prefixed with MI app's short name.
// vTxtForLogFile - (R) Text data.
// vbSendToLog - (R) True = Yes send to the Log file too, false = do not. (Dflt = true)
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::WritePriv(const CMIUtilString &vText, const CMIUtilString &vTxtForLogFile, const bool vbSendToLog /* = true */)
{
if (vText.length() == 0)
return MIstatus::failure;
bool bOk = MIstatus::success;
{
// Grab the stdout thread lock while we print
CMIUtilThreadLock _lock(m_mutex);
// Send this text to stdout
const MIint status = ::fputs(vText.c_str(), stdout);
if (status == EOF)
// Don't call the CMICmnBase::SetErrorDescription() because it will cause a stack overflow:
// CMICmnBase::SetErrorDescription -> CMICmnStreamStdout::Write -> CMICmnStreamStdout::WritePriv -> CMICmnBase::SetErrorDescription
bOk = MIstatus::failure;
else
{
::fprintf(stdout, "\n");
::fflush(stdout);
}
// Send this text to the log
if (bOk && vbSendToLog)
bOk &= m_pLog->WriteLog(vTxtForLogFile);
}
return bOk;
}
//++ ------------------------------------------------------------------------------------
// Details: Lock the availability of the stream stdout. Other users of *this stream will
// be stalled until it is available (Unlock()).
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::Lock(void)
{
m_mutex.Lock();
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Release a previously locked stdout.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::Unlock(void)
{
m_mutex.Unlock();
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Take a text data and send to the stdout stream. Also output to the MI Log
// file.
// Type: Static method.
// Args: vrTxt - (R) Text.
// Return: MIstatus::success - Functionality succeeded.
// MIstatus::failure - Functionality failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::TextToStdout(const CMIUtilString &vrTxt)
{
const bool bSendToLog = true;
return CMICmnStreamStdout::Instance().WriteMIResponse(vrTxt, bSendToLog);
}
//++ ------------------------------------------------------------------------------------
// Details: Write prompt to stdout if it's enabled.
// Type: Static method.
// Args: None.
// Return: MIstatus::success - Function succeeded.
// MIstatus::failure - Function failed.
// Throws: None.
//--
bool
CMICmnStreamStdout::WritePrompt(void)
{
const CMICmnStreamStdin &rStdinMan = CMICmnStreamStdin::Instance();
if (rStdinMan.GetEnablePrompt())
return TextToStdout(rStdinMan.GetPrompt());
return MIstatus::success;
}
|