summaryrefslogtreecommitdiffstats
path: root/dbus
diff options
context:
space:
mode:
authorsatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-07 16:41:26 +0000
committersatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-07 16:41:26 +0000
commit6df1b9592e5d011d789620fd8a34478c30f7219d (patch)
treebc410504adc9e407983db7da9d6e3811b9ac2df0 /dbus
parentac99a1906b6ac31b1f17b98f4658c3dc886970cd (diff)
downloadchromium_src-6df1b9592e5d011d789620fd8a34478c30f7219d.zip
chromium_src-6df1b9592e5d011d789620fd8a34478c30f7219d.tar.gz
chromium_src-6df1b9592e5d011d789620fd8a34478c30f7219d.tar.bz2
dbus: Truncate strings in dbus::Message::ToString() if too long
To prevent long strings from polluting logs. BUG=131261 TEST=added a unit test Review URL: https://chromiumcodereview.appspot.com/10537033 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141021 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'dbus')
-rw-r--r--dbus/message.cc12
-rw-r--r--dbus/message.h3
-rw-r--r--dbus/message_unittest.cc15
3 files changed, 28 insertions, 2 deletions
diff --git a/dbus/message.cc b/dbus/message.cc
index 2caf543..fd7c077 100644
--- a/dbus/message.cc
+++ b/dbus/message.cc
@@ -155,7 +155,17 @@ std::string Message::ToStringInternal(const std::string& indent,
std::string value;
if (!reader->PopString(&value))
return kBrokenMessage;
- output += indent + "string \"" + value + "\"\n";
+ // Truncate if the string is longer than the limit.
+ const size_t kTruncateLength = 100;
+ if (value.size() < kTruncateLength) {
+ output += indent + "string \"" + value + "\"\n";
+ } else {
+ std::string truncated;
+ TruncateUTF8ToByteSize(value, kTruncateLength, &truncated);
+ base::StringAppendF(&truncated, "... (%"PRIuS" bytes in total)",
+ value.size());
+ output += indent + "string \"" + truncated + "\"\n";
+ }
break;
}
case OBJECT_PATH: {
diff --git a/dbus/message.h b/dbus/message.h
index 0b9834d..38887f8 100644
--- a/dbus/message.h
+++ b/dbus/message.h
@@ -114,7 +114,8 @@ class Message {
uint32 GetReplySerial();
// Returns the string representation of this message. Useful for
- // debugging.
+ // debugging. The output is truncated as needed (ex. strings are truncated
+ // if longer than a certain limit defined in the .cc file).
std::string ToString();
protected:
diff --git a/dbus/message_unittest.cc b/dbus/message_unittest.cc
index d78d90f2..10835a29 100644
--- a/dbus/message_unittest.cc
+++ b/dbus/message_unittest.cc
@@ -613,3 +613,18 @@ TEST(MessageTest, SetInvalidHeaders) {
EXPECT_EQ("", message->GetErrorName());
EXPECT_EQ("", message->GetSender());
}
+
+TEST(MessageTest, ToString_LongString) {
+ const std::string kLongString(1000, 'o');
+
+ scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
+ dbus::MessageWriter writer(message.get());
+ writer.AppendString(kLongString);
+
+ ASSERT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
+ "signature: s\n\n"
+ "string \"oooooooooooooooooooooooooooooooooooooooooooooooo"
+ "oooooooooooooooooooooooooooooooooooooooooooooooooooo... "
+ "(1000 bytes in total)\"\n",
+ message->ToString());
+}