2016-09-07 04:57:50 +08:00
|
|
|
//===-- MICmnStreamStderr.cpp ------------------------------------*- C++
|
|
|
|
//-*-===//
|
2014-05-16 18:51:01 +08:00
|
|
|
//
|
|
|
|
// 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 "MICmnStreamStderr.h"
|
|
|
|
#include "MICmnLog.h"
|
|
|
|
#include "MICmnResources.h"
|
|
|
|
#include "MIDriver.h"
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMICmnStreamStderr constructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
CMICmnStreamStderr::CMICmnStreamStderr() {}
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMICmnStreamStderr destructor.
|
|
|
|
// Type: Overridable.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
CMICmnStreamStderr::~CMICmnStreamStderr() { Shutdown(); }
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Initialize resources for *this stderr stream.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::Initialize() {
|
|
|
|
m_clientUsageRefCnt++;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (m_bInitialized)
|
|
|
|
return MIstatus::success;
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool bOk = MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2014-11-18 02:06:21 +08:00
|
|
|
// Debugging / I/O issues with client.
|
|
|
|
// This is only required on Windows if you do not use ::flush(stderr). MI uses
|
|
|
|
// ::flush(stderr)
|
|
|
|
// It trys to ensure the process attached to the stderr steam gets ALL the data.
|
|
|
|
//::setbuf( stderr, NULL );
|
2014-05-16 18:51:01 +08:00
|
|
|
#endif // _MSC_VER
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
m_bInitialized = bOk;
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Release resources for *this stderr stream.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::Shutdown() {
|
|
|
|
if (--m_clientUsageRefCnt > 0)
|
|
|
|
return MIstatus::success;
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (!m_bInitialized)
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
ClrErrorDescription();
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
m_bInitialized = false;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return MIstatus::success;
|
2014-11-18 02:06:21 +08:00
|
|
|
}
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Details: Write text data to stderr. Prefix the message with "MI:". The text
|
|
|
|
// data does
|
|
|
|
// not need to include a carriage line return as this is added to the
|
|
|
|
// text. The
|
2014-11-18 02:06:21 +08:00
|
|
|
// function also then passes the text data into the CMICmnLog logger.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vText - (R) Text data.
|
2016-09-07 04:57:50 +08:00
|
|
|
// vbSendToLog - (R) True = Yes send to the Log file too, false = do
|
|
|
|
// not. (Dflt = true)
|
2014-11-18 02:06:21 +08:00
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::Write(const CMIUtilString &vText,
|
|
|
|
const bool vbSendToLog /* = true */) {
|
|
|
|
if (vText.length() == 0)
|
|
|
|
return MIstatus::failure;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const CMIUtilString strPrefixed(CMIUtilString::Format(
|
|
|
|
"%s: %s", CMIDriver::Instance().GetAppNameShort().c_str(),
|
|
|
|
vText.c_str()));
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return WritePriv(strPrefixed, vText, vbSendToLog);
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Write an LLDB text message to stderr.
|
2016-09-07 04:57:50 +08:00
|
|
|
// 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
|
2014-11-18 02:06:21 +08:00
|
|
|
// logger.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vText - (R) Text data.
|
2016-09-07 04:57:50 +08:00
|
|
|
// vbSendToLog - (R) True = Yes send to the Log file too, false = do
|
|
|
|
// not. (Dflt = true)
|
2014-11-18 02:06:21 +08:00
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-08-09 00:47:42 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::WriteLLDBMsg(const CMIUtilString &vText,
|
|
|
|
const bool vbSendToLog /* = true */) {
|
|
|
|
if (vText.length() == 0)
|
|
|
|
return MIstatus::failure;
|
2014-08-09 00:47:42 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const CMIUtilString strPrefixed(
|
|
|
|
CMIUtilString::Format("LLDB: %s", vText.c_str()));
|
2014-08-09 00:47:42 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return WritePriv(vText, strPrefixed, vbSendToLog);
|
2014-08-09 00:47:42 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Write text data to stderr. The text data does not need to
|
2016-09-07 04:57:50 +08:00
|
|
|
// include a carriage line return as this is added to the text. The
|
|
|
|
// function also
|
2014-11-18 02:06:21 +08:00
|
|
|
// then passes the text data into the CMICmnLog logger.
|
|
|
|
// Type: Method.
|
2016-09-07 04:57:50 +08:00
|
|
|
// Args: vText - (R) Text data. May be prefixed with MI app's short
|
|
|
|
// name.
|
2014-11-18 02:06:21 +08:00
|
|
|
// vTxtForLogFile - (R) Text data.
|
2016-09-07 04:57:50 +08:00
|
|
|
// vbSendToLog - (R) True = Yes send to the Log file too, false =
|
|
|
|
// do not. (Dflt = true)
|
2014-11-18 02:06:21 +08:00
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::WritePriv(const CMIUtilString &vText,
|
|
|
|
const CMIUtilString &vTxtForLogFile,
|
|
|
|
const bool vbSendToLog /* = true */) {
|
|
|
|
if (vText.length() == 0)
|
|
|
|
return MIstatus::failure;
|
|
|
|
|
|
|
|
bool bOk = MIstatus::success;
|
|
|
|
{
|
|
|
|
// Grab the stderr thread lock while we print
|
|
|
|
CMIUtilThreadLock _lock(m_mutex);
|
|
|
|
|
|
|
|
// Send this text to stderr
|
|
|
|
const MIint status = ::fputs(vText.c_str(), stderr);
|
|
|
|
if (status == EOF) {
|
|
|
|
const CMIUtilString errMsg(CMIUtilString::Format(
|
|
|
|
MIRSRC(IDS_STDERR_ERR_NOT_ALL_DATA_WRITTEN), vText.c_str()));
|
|
|
|
SetErrorDescription(errMsg);
|
|
|
|
bOk = MIstatus::failure;
|
|
|
|
} else {
|
|
|
|
::fprintf(stderr, "\n");
|
|
|
|
::fflush(stderr);
|
2014-11-18 02:06:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// Send this text to the log
|
|
|
|
if (bOk && vbSendToLog)
|
|
|
|
bOk &= m_pLog->WriteLog(vTxtForLogFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bOk;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Details: Lock the availability of the stream stderr. Other users of *this
|
|
|
|
// stream will
|
2014-11-18 02:06:21 +08:00
|
|
|
// be stalled until it is available (Unlock()).
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::Lock() {
|
|
|
|
m_mutex.Lock();
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Release a previously locked stderr.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::Unlock() {
|
|
|
|
m_mutex.Unlock();
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Details: Take MI Driver text message and send to the stderr stream. Also
|
|
|
|
// output to the
|
2014-11-18 02:06:21 +08:00
|
|
|
// MI Log file.
|
|
|
|
// Type: Static method.
|
|
|
|
// Args: vrTxt - (R) Text.
|
|
|
|
// Return: MIstatus::success - Functionality succeeded.
|
|
|
|
// MIstatus::failure - Functionality failed.
|
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::TextToStderr(const CMIUtilString &vrTxt) {
|
|
|
|
const bool bLock = CMICmnStreamStderr::Instance().Lock();
|
|
|
|
const bool bOk = bLock && CMICmnStreamStderr::Instance().Write(vrTxt);
|
|
|
|
bLock &&CMICmnStreamStderr::Instance().Unlock();
|
|
|
|
|
|
|
|
return bOk;
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
2014-08-09 00:47:42 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Details: Take an LLDB message and send to the stderr stream. The message is
|
|
|
|
// not always
|
|
|
|
// an error message. The user has typed a command in to the Eclipse
|
|
|
|
// console (by-
|
|
|
|
// passing Eclipse) and this is the result message from LLDB back to
|
|
|
|
// the user.
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-08-09 00:47:42 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmnStreamStderr::LLDBMsgToConsole(const CMIUtilString &vrTxt) {
|
|
|
|
const bool bLock = CMICmnStreamStderr::Instance().Lock();
|
|
|
|
const bool bOk = bLock && CMICmnStreamStderr::Instance().WriteLLDBMsg(vrTxt);
|
|
|
|
bLock &&CMICmnStreamStderr::Instance().Unlock();
|
|
|
|
|
|
|
|
return bOk;
|
2014-11-18 02:06:21 +08:00
|
|
|
}
|