In log headers, only escape information when necessary.

This commit is contained in:
Robin Sommer 2011-12-19 08:44:41 -08:00
parent a9f0b10e2e
commit c1e656d89e
4 changed files with 24 additions and 14 deletions

View file

@ -271,7 +271,7 @@ void ODesc::AddBytes(const void* bytes, unsigned int n)
} }
else else
{ {
string esc_str = get_escaped_string(string(p.first, p.second)); string esc_str = get_escaped_string(string(p.first, p.second), true);
AddBytesRaw(esc_str.c_str(), esc_str.size()); AddBytesRaw(esc_str.c_str(), esc_str.size());
} }
s = p.first + p.second; s = p.first + p.second;

View file

@ -82,19 +82,19 @@ bool LogWriterAscii::DoInit(string path, int num_fields,
{ {
string str = string(header_prefix, header_prefix_len) string str = string(header_prefix, header_prefix_len)
+ "separator " // Always use space as separator here. + "separator " // Always use space as separator here.
+ get_escaped_string(string(separator, separator_len)) + get_escaped_string(string(separator, separator_len), false)
+ "\n"; + "\n";
if( fwrite(str.c_str(), str.length(), 1, file) != 1 ) if( fwrite(str.c_str(), str.length(), 1, file) != 1 )
goto write_error; goto write_error;
if ( ! (WriteHeaderField("set_separator", get_escaped_string( if ( ! (WriteHeaderField("set_separator", get_escaped_string(
string(set_separator, set_separator_len))) && string(set_separator, set_separator_len), false)) &&
WriteHeaderField("empty_field", get_escaped_string( WriteHeaderField("empty_field", get_escaped_string(
string(empty_field, empty_field_len))) && string(empty_field, empty_field_len), false)) &&
WriteHeaderField("unset_field", get_escaped_string( WriteHeaderField("unset_field", get_escaped_string(
string(unset_field, unset_field_len))) && string(unset_field, unset_field_len), false)) &&
WriteHeaderField("path", path)) ) WriteHeaderField("path", get_escaped_string(path, false))) )
goto write_error; goto write_error;
string names; string names;

View file

@ -42,22 +42,32 @@
#include "Reporter.h" #include "Reporter.h"
/** /**
* Takes a string, escapes each character into its equivalent hex code (\x##), and * Takes a string, escapes characters into equivalent hex codes (\x##), and
* returns a string containing all escaped values. * returns a string containing all escaped values.
* *
* @param str string to escape * @param str string to escape
* @return A std::string containing a list of escaped hex values of the form \x## * @param escape_all If true, all characters are escaped. If false, only
*/ * characters are escaped that are either whitespace or not printable in
std::string get_escaped_string(const std::string& str) * ASCII.
* @return A std::string containing a list of escaped hex values of the form
* \x## */
std::string get_escaped_string(const std::string& str, bool escape_all)
{ {
char tbuf[16]; char tbuf[16];
string esc = ""; string esc = "";
for ( size_t i = 0; i < str.length(); ++i ) for ( size_t i = 0; i < str.length(); ++i )
{ {
snprintf(tbuf, sizeof(tbuf), "\\x%02x", str[i]); char c = str[i];
esc += tbuf;
} if ( escape_all || isspace(c) || ! isascii(c) || ! isprint(c) )
{
snprintf(tbuf, sizeof(tbuf), "\\x%02x", str[i]);
esc += tbuf;
}
else
esc += c;
}
return esc; return esc;
} }

View file

@ -89,7 +89,7 @@ void delete_each(T* t)
delete *it; delete *it;
} }
std::string get_escaped_string(const std::string& str); std::string get_escaped_string(const std::string& str, bool escape_all);
extern char* copy_string(const char* s); extern char* copy_string(const char* s);
extern int streq(const char* s1, const char* s2); extern int streq(const char* s1, const char* s2);