Consistently convert UTF-16 to UTF-8 for trace output.
* Previously, most places in the codebase used "%ls", which relies on the CRT to do a wide-to-mbcs conversion, and I think(?) that uses locale settings? * Do the conversion explicitly using narrowString, which is renamed to utf8FromWide and moved to a new file, src/shared/StringUtil.cc.
This commit is contained in:
parent
c2756d8a79
commit
7baffb85b8
@ -29,6 +29,7 @@
|
||||
#include "../shared/Buffer.h"
|
||||
#include "../shared/winpty_snprintf.h"
|
||||
#include "../shared/WinptyAssert.h"
|
||||
#include "../shared/StringUtil.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -200,7 +201,8 @@ NamedPipe *Agent::makeSocket(LPCWSTR pipeName)
|
||||
{
|
||||
NamedPipe *pipe = createNamedPipe();
|
||||
if (!pipe->connectToServer(pipeName)) {
|
||||
trace("error: could not connect to %ls", pipeName);
|
||||
trace("error: could not connect to %s",
|
||||
utf8FromWide(pipeName).c_str());
|
||||
::exit(1);
|
||||
}
|
||||
pipe->setReadBufferSize(64 * 1024);
|
||||
|
@ -31,9 +31,9 @@
|
||||
|
||||
#include "../shared/DebugClient.h"
|
||||
#include "../shared/OsModule.h"
|
||||
#include "../shared/StringUtil.h"
|
||||
#include "../shared/WinptyAssert.h"
|
||||
#include "../shared/winpty_snprintf.h"
|
||||
#include "../shared/winpty_wcsnlen.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@ -315,25 +315,6 @@ static void dumpFontTable(HANDLE conout, const char *prefix) {
|
||||
}
|
||||
}
|
||||
|
||||
static std::string narrowString(const std::wstring &input)
|
||||
{
|
||||
int mblen = WideCharToMultiByte(
|
||||
CP_UTF8, 0,
|
||||
input.data(), input.size(),
|
||||
NULL, 0, NULL, NULL);
|
||||
if (mblen <= 0) {
|
||||
return std::string();
|
||||
}
|
||||
std::vector<char> tmp(mblen);
|
||||
int mblen2 = WideCharToMultiByte(
|
||||
CP_UTF8, 0,
|
||||
input.data(), input.size(),
|
||||
tmp.data(), tmp.size(),
|
||||
NULL, NULL);
|
||||
ASSERT(mblen2 == mblen);
|
||||
return std::string(tmp.data(), tmp.size());
|
||||
}
|
||||
|
||||
static std::string stringToCodePoints(const std::wstring &str) {
|
||||
std::string ret = "(";
|
||||
for (size_t i = 0; i < str.size(); ++i) {
|
||||
@ -361,7 +342,7 @@ static void dumpFontInfoEx(
|
||||
prefix,
|
||||
static_cast<unsigned>(infoex.nFont),
|
||||
infoex.dwFontSize.X, infoex.dwFontSize.Y,
|
||||
infoex.FontFamily, infoex.FontWeight, narrowString(faceName).c_str(),
|
||||
infoex.FontFamily, infoex.FontWeight, utf8FromWide(faceName).c_str(),
|
||||
stringToCodePoints(faceName).c_str());
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include "EventLoop.h"
|
||||
#include "NamedPipe.h"
|
||||
#include "../shared/DebugClient.h"
|
||||
#include "../shared/StringUtil.h"
|
||||
#include "../shared/WinptyAssert.h"
|
||||
|
||||
NamedPipe::NamedPipe() :
|
||||
@ -196,7 +197,8 @@ bool NamedPipe::connectToServer(LPCWSTR pipeName)
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_OVERLAPPED,
|
||||
NULL);
|
||||
trace("connection to [%ls], handle == %p", pipeName, handle);
|
||||
trace("connection to [%s], handle == %p",
|
||||
utf8FromWide(pipeName).c_str(), handle);
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
return false;
|
||||
m_handle = handle;
|
||||
|
@ -38,9 +38,9 @@ AGENT_OBJECTS = \
|
||||
build/agent/agent/main.o \
|
||||
build/agent/shared/Buffer.o \
|
||||
build/agent/shared/DebugClient.o \
|
||||
build/agent/shared/StringUtil.o \
|
||||
build/agent/shared/WinptyAssert.o \
|
||||
build/agent/shared/WinptyVersion.o \
|
||||
build/agent/shared/winpty_wcsnlen.o
|
||||
build/agent/shared/WinptyVersion.o
|
||||
|
||||
build/winpty-agent.exe : $(AGENT_OBJECTS)
|
||||
$(info Linking $@)
|
||||
|
@ -26,6 +26,7 @@ LIBWINPTY_OBJECTS = \
|
||||
build/libwinpty/libwinpty/winpty.o \
|
||||
build/libwinpty/shared/Buffer.o \
|
||||
build/libwinpty/shared/DebugClient.o \
|
||||
build/libwinpty/shared/StringUtil.o \
|
||||
build/libwinpty/shared/WinptyAssert.o
|
||||
|
||||
build/winpty.dll : $(LIBWINPTY_OBJECTS)
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "../shared/AgentMsg.h"
|
||||
#include "../shared/Buffer.h"
|
||||
#include "../shared/StringBuilder.h"
|
||||
#include "../shared/StringUtil.h"
|
||||
|
||||
// TODO: Error handling, handle out-of-memory.
|
||||
|
||||
@ -279,13 +280,15 @@ static void startAgentProcess(const BackgroundDesktop &desktop,
|
||||
NULL, NULL,
|
||||
&sui, &pi);
|
||||
if (success) {
|
||||
trace("Created agent successfully, pid=%ld, cmdline=%ls",
|
||||
static_cast<long>(pi.dwProcessId), cmdline.c_str());
|
||||
trace("Created agent successfully, pid=%u, cmdline=%s",
|
||||
static_cast<unsigned int>(pi.dwProcessId),
|
||||
utf8FromWide(cmdline).c_str());
|
||||
} else {
|
||||
unsigned int err = GetLastError();
|
||||
trace("Error creating agent, err=%#x, cmdline=%ls",
|
||||
err, cmdline.c_str());
|
||||
fprintf(stderr, "Error %#x starting %ls\n", err, cmdline.c_str());
|
||||
trace("Error creating agent, err=%#x, cmdline=%s",
|
||||
err, utf8FromWide(cmdline).c_str());
|
||||
fprintf(stderr, "Error %#x starting %s\n", err,
|
||||
utf8FromWide(cmdline).c_str());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,11 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
|
||||
#include "winpty_wcsnlen.h"
|
||||
#include "StringUtil.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "WinptyAssert.h"
|
||||
|
||||
@ -33,3 +37,21 @@ size_t winpty_wcsnlen(const wchar_t *s, size_t maxlen) {
|
||||
}
|
||||
return maxlen;
|
||||
}
|
||||
|
||||
std::string utf8FromWide(const std::wstring &input) {
|
||||
int mblen = WideCharToMultiByte(
|
||||
CP_UTF8, 0,
|
||||
input.data(), input.size(),
|
||||
NULL, 0, NULL, NULL);
|
||||
if (mblen <= 0) {
|
||||
return std::string();
|
||||
}
|
||||
std::vector<char> tmp(mblen);
|
||||
int mblen2 = WideCharToMultiByte(
|
||||
CP_UTF8, 0,
|
||||
input.data(), input.size(),
|
||||
tmp.data(), tmp.size(),
|
||||
NULL, NULL);
|
||||
ASSERT(mblen2 == mblen);
|
||||
return std::string(tmp.data(), tmp.size());
|
||||
}
|
@ -18,12 +18,14 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
|
||||
#ifndef WINPTY_WCSNLEN_H
|
||||
#define WINPTY_WCSNLEN_H
|
||||
#ifndef STRING_UTIL_H
|
||||
#define STRING_UTIL_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
size_t winpty_wcsnlen(const wchar_t *s, size_t maxlen);
|
||||
std::string utf8FromWide(const std::wstring &input);
|
||||
|
||||
#endif // WINPTY_WCSNLEN_H
|
||||
#endif // STRING_UTIL_H
|
@ -78,6 +78,8 @@
|
||||
'shared/DebugClient.cc',
|
||||
'shared/OsModule.h',
|
||||
'shared/StringBuilder.h',
|
||||
'shared/StringUtil.cc',
|
||||
'shared/StringUtil.h',
|
||||
'shared/UnixCtrlChars.h',
|
||||
'shared/WinptyAssert.h',
|
||||
'shared/WinptyAssert.cc',
|
||||
@ -85,8 +87,6 @@
|
||||
'shared/WinptyVersion.h',
|
||||
'shared/WinptyVersion.cc',
|
||||
'shared/winpty_snprintf.h',
|
||||
'shared/winpty_wcsnlen.cc',
|
||||
'shared/winpty_wcsnlen.h',
|
||||
],
|
||||
},
|
||||
{
|
||||
@ -110,6 +110,8 @@
|
||||
'shared/DebugClient.h',
|
||||
'shared/DebugClient.cc',
|
||||
'shared/StringBuilder.h',
|
||||
'shared/StringUtil.cc',
|
||||
'shared/StringUtil.h',
|
||||
'shared/WinptyAssert.h',
|
||||
'shared/WinptyAssert.cc',
|
||||
'shared/WinptyException.h',
|
||||
|
Loading…
Reference in New Issue
Block a user