Allow specifying character set for wxLogStderr and wxLogStream.

Until now, a mixture of non-UTF-8 and UTF-8 could be written in some circumstances.
This commit is contained in:
Lauri Nurmi
2016-02-20 15:07:11 +02:00
parent 1417776d33
commit 6b73bd9136
5 changed files with 104 additions and 11 deletions

View File

@@ -839,7 +839,8 @@ void wxLogBuffer::DoLogTextAtLevel(wxLogLevel level, const wxString& msg)
// wxLogStderr class implementation
// ----------------------------------------------------------------------------
wxLogStderr::wxLogStderr(FILE *fp)
wxLogStderr::wxLogStderr(FILE *fp, const wxMBConv &conv):
m_conv(conv.Clone())
{
if ( fp == NULL )
m_fp = stderr;
@@ -847,12 +848,17 @@ wxLogStderr::wxLogStderr(FILE *fp)
m_fp = fp;
}
wxLogStderr::~wxLogStderr()
{
delete m_conv;
}
void wxLogStderr::DoLogText(const wxString& msg)
{
// First send it to stderr, even if we don't have it (e.g. in a Windows GUI
// application under) it's not a problem to try to use it and it's easier
// than determining whether we do have it or not.
wxMessageOutputStderr(m_fp).Output(msg);
wxMessageOutputStderr(m_fp, *m_conv).Output(msg);
// under GUI systems such as Windows or Mac, programs usually don't have
// stderr at all, so show the messages also somewhere else, typically in
@@ -874,7 +880,8 @@ void wxLogStderr::DoLogText(const wxString& msg)
#if wxUSE_STD_IOSTREAM
#include "wx/ioswrap.h"
wxLogStream::wxLogStream(wxSTD ostream *ostr)
wxLogStream::wxLogStream(wxSTD ostream *ostr, const wxMBConv &conv):
m_conv(conv.Clone())
{
if ( ostr == NULL )
m_ostr = &wxSTD cerr;
@@ -882,9 +889,30 @@ wxLogStream::wxLogStream(wxSTD ostream *ostr)
m_ostr = ostr;
}
wxLogStream::~wxLogStream()
{
delete m_conv;
}
void wxLogStream::DoLogText(const wxString& msg)
{
(*m_ostr) << msg << wxSTD endl;
wxString msgLF(msg);
if ( msgLF.empty() || *msgLF.rbegin() != '\n' )
msgLF += '\n';
#if defined(__WINDOWS__)
// Determine whether the encoding is UTF-16. In that case, the file
// should have been opened in "wb" mode, and EOL-style must be handled
// here.
if (m_conv->GetMBNulLen() == 2)
{
msgLF.Replace("\n", "\r\n");
}
#endif
const wxCharBuffer buf = m_conv->cWX2MB(msgLF.c_str());
m_ostr->write(buf, buf.length());
}
#endif // wxUSE_STD_IOSTREAM

View File

@@ -136,6 +136,17 @@ void wxMessageOutputBest::Output(const wxString& str)
// wxMessageOutputStderr
// ----------------------------------------------------------------------------
wxMessageOutputStderr::wxMessageOutputStderr(FILE *fp,
const wxMBConv &conv):
m_fp(fp), m_conv(conv.Clone())
{
}
wxMessageOutputStderr::~wxMessageOutputStderr()
{
delete m_conv;
}
wxString wxMessageOutputStderr::AppendLineFeedIfNeeded(const wxString& str)
{
wxString strLF(str);
@@ -147,9 +158,22 @@ wxString wxMessageOutputStderr::AppendLineFeedIfNeeded(const wxString& str)
void wxMessageOutputStderr::Output(const wxString& str)
{
const wxString strWithLF = AppendLineFeedIfNeeded(str);
wxString strWithLF = AppendLineFeedIfNeeded(str);
fprintf(m_fp, "%s", (const char*) strWithLF.mb_str(wxConvWhateverWorks));
#if defined(__WINDOWS__)
// Determine whether the encoding is UTF-16. In that case, the file
// should have been opened in "wb" mode, and EOL-style must be handled
// here.
if (m_conv->GetMBNulLen() == 2)
{
strWithLF.Replace("\n", "\r\n");
}
#endif
const wxCharBuffer buf = m_conv->cWX2MB(strWithLF.c_str());
fwrite(buf, buf.length(), 1, m_fp);
fflush(m_fp);
}