diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-27 21:09:07 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-27 21:09:07 +0000 |
commit | 3dd7a7a7806269c92861cbeb073dac8291d80e35 (patch) | |
tree | 2092e19648ce103bca50c9d1f9ef946ffdd48035 /ipc | |
parent | 8ede076f5956aa5caebd3c0320ecf9305fb0d5b6 (diff) | |
download | chromium_src-3dd7a7a7806269c92861cbeb073dac8291d80e35.zip chromium_src-3dd7a7a7806269c92861cbeb073dac8291d80e35.tar.gz chromium_src-3dd7a7a7806269c92861cbeb073dac8291d80e35.tar.bz2 |
posix: handle chars more delicately in ipc logging
Since we're displaying to stdout, we don't want to dump arbitrary bytes
to the IPC log. Make it display only ASCII and clip it to a reasonable
max length.
(PS: I tried to resolve the vector<char> / vector<unsigned char> schism
at first but it bleeds across out entire codebase...)
Review URL: http://codereview.chromium.org/159430
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21705 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ipc')
-rw-r--r-- | ipc/ipc_message_utils.h | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/ipc/ipc_message_utils.h b/ipc/ipc_message_utils.h index 3fd8123..523ce60 100644 --- a/ipc/ipc_message_utils.h +++ b/ipc/ipc_message_utils.h @@ -386,6 +386,29 @@ struct ParamTraits<std::string> { } }; +template<typename CharType> +static void LogBytes(const std::vector<CharType>& data, std::wstring* out) { +#if defined(OS_WIN) + // Windows has a GUI for logging, which can handle arbitrary binary data. + for (size_t i = 0; i < data.size(); ++i) + out->push_back(data[i]); +#else + // On POSIX, we log to stdout, which we assume can display ASCII. + static const size_t kMaxBytesToLog = 100; + for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) { + if (isprint(data[i])) + out->push_back(data[i]); + else + out->append(StringPrintf(L"[%02X]", static_cast<unsigned char>(data[i]))); + } + if (data.size() > kMaxBytesToLog) { + out->append( + StringPrintf(L" and %u more bytes", + static_cast<unsigned>(data.size() - kMaxBytesToLog))); + } +#endif +} + template <> struct ParamTraits<std::vector<unsigned char> > { typedef std::vector<unsigned char> param_type; @@ -408,8 +431,7 @@ struct ParamTraits<std::vector<unsigned char> > { return true; } static void Log(const param_type& p, std::wstring* l) { - for (size_t i = 0; i < p.size(); ++i) - l->push_back(p[i]); + LogBytes(p, l); } }; @@ -434,8 +456,7 @@ struct ParamTraits<std::vector<char> > { return true; } static void Log(const param_type& p, std::wstring* l) { - for (size_t i = 0; i < p.size(); ++i) - l->push_back(p[i]); + LogBytes(p, l); } }; |