Merge branch 'log-encoding'

See https://github.com/wxWidgets/wxWidgets/pull/552

Closes #17385.
This commit is contained in:
Vadim Zeitlin
2017-11-04 18:15:59 +01:00
6 changed files with 120 additions and 28 deletions

View File

@@ -839,12 +839,9 @@ void wxLogBuffer::DoLogTextAtLevel(wxLogLevel level, const wxString& msg)
// wxLogStderr class implementation
// ----------------------------------------------------------------------------
wxLogStderr::wxLogStderr(FILE *fp)
wxLogStderr::wxLogStderr(FILE *fp, const wxMBConv& conv)
: wxMessageOutputStderr(fp ? fp : stderr, conv)
{
if ( fp == NULL )
m_fp = stderr;
else
m_fp = fp;
}
void wxLogStderr::DoLogText(const wxString& msg)
@@ -852,7 +849,7 @@ 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::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 +871,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)
: wxMessageOutputWithConv(conv)
{
if ( ostr == NULL )
m_ostr = &wxSTD cerr;
@@ -884,7 +882,8 @@ wxLogStream::wxLogStream(wxSTD ostream *ostr)
void wxLogStream::DoLogText(const wxString& msg)
{
(*m_ostr) << msg << wxSTD endl;
const wxCharBuffer& buf = PrepareForOutput(msg);
m_ostr->write(buf, buf.length());
}
#endif // wxUSE_STD_IOSTREAM

View File

@@ -133,10 +133,10 @@ void wxMessageOutputBest::Output(const wxString& str)
}
// ----------------------------------------------------------------------------
// wxMessageOutputStderr
// wxMessageOutputWithConv
// ----------------------------------------------------------------------------
wxString wxMessageOutputStderr::AppendLineFeedIfNeeded(const wxString& str)
wxString wxMessageOutputWithConv::AppendLineFeedIfNeeded(const wxString& str)
{
wxString strLF(str);
if ( strLF.empty() || *strLF.rbegin() != '\n' )
@@ -145,11 +145,37 @@ wxString wxMessageOutputStderr::AppendLineFeedIfNeeded(const wxString& str)
return strLF;
}
wxCharBuffer wxMessageOutputWithConv::PrepareForOutput(const wxString& str)
{
wxString strWithLF = AppendLineFeedIfNeeded(str);
#if defined(__WINDOWS__)
// Determine whether the encoding is UTF-16. In that case, the file
// should have been opened in "wb" mode, and EOL conversion must be done
// here as it won't be done at stdio level.
if ( m_conv->GetMBNulLen() == 2 )
{
strWithLF.Replace("\n", "\r\n");
}
#endif // __WINDOWS__
return m_conv->cWX2MB(strWithLF.c_str());
}
// ----------------------------------------------------------------------------
// wxMessageOutputStderr
// ----------------------------------------------------------------------------
wxMessageOutputStderr::wxMessageOutputStderr(FILE *fp, const wxMBConv& conv)
: wxMessageOutputWithConv(conv),
m_fp(fp)
{
}
void wxMessageOutputStderr::Output(const wxString& str)
{
const wxString strWithLF = AppendLineFeedIfNeeded(str);
fprintf(m_fp, "%s", (const char*) strWithLF.mb_str(wxConvWhateverWorks));
const wxCharBuffer& buf = PrepareForOutput(str);
fwrite(buf, buf.length(), 1, m_fp);
fflush(m_fp);
}