mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 09:38:19 +00:00
Merge remote branch 'origin/topic/gilbert/ascii-header'
* origin/topic/gilbert/ascii-header: Updated tests; removed net type from type conversion code. Updated header format (see #558) Header modification to LogWriterAscii to make it easier for scripts to understand bro log files. Notes: - I've refactored the code a bit, also adapting the style a bit. Also edited the header format slightly. - I'm skipping the testing/btest/profiles directory, which seems unrelated. - I'm also skipping the baseline updates as they weren't up-to-date anymore. Will update them in a subsequent commit.
This commit is contained in:
commit
630c256a72
3 changed files with 53 additions and 10 deletions
|
@ -6,6 +6,27 @@
|
|||
#include "LogWriterAscii.h"
|
||||
#include "NetVar.h"
|
||||
|
||||
/**
|
||||
* Takes a string, escapes each character into its equivalent hex code (\x##), and
|
||||
* returns a string containing all escaped values.
|
||||
*
|
||||
* @param str string to escape
|
||||
* @return A std::string containing a list of escaped hex values of the form \x##
|
||||
*/
|
||||
static string get_escaped_string(const std::string& str)
|
||||
{
|
||||
char tbuf[16];
|
||||
string esc = "";
|
||||
|
||||
for ( size_t i = 0; i < str.length(); ++i )
|
||||
{
|
||||
snprintf(tbuf, sizeof(tbuf), "\\x%02x", str[i]);
|
||||
esc += tbuf;
|
||||
}
|
||||
|
||||
return esc;
|
||||
}
|
||||
|
||||
LogWriterAscii::LogWriterAscii()
|
||||
{
|
||||
file = 0;
|
||||
|
@ -52,6 +73,14 @@ LogWriterAscii::~LogWriterAscii()
|
|||
delete [] header_prefix;
|
||||
}
|
||||
|
||||
bool LogWriterAscii::WriteHeaderField(const string& key, const string& val)
|
||||
{
|
||||
string str = string(header_prefix, header_prefix_len) +
|
||||
key + string(separator, separator_len) + val + "\n";
|
||||
|
||||
return (fwrite(str.c_str(), str.length(), 1, file) == 1);
|
||||
}
|
||||
|
||||
bool LogWriterAscii::DoInit(string path, int num_fields,
|
||||
const LogField* const * fields)
|
||||
{
|
||||
|
@ -70,22 +99,35 @@ bool LogWriterAscii::DoInit(string path, int num_fields,
|
|||
|
||||
if ( include_header )
|
||||
{
|
||||
if ( fwrite(header_prefix, header_prefix_len, 1, file) != 1 )
|
||||
string str = string(header_prefix, header_prefix_len)
|
||||
+ "separator " // Always use space as separator here.
|
||||
+ get_escaped_string(string(separator, separator_len))
|
||||
+ "\n";
|
||||
|
||||
if( fwrite(str.c_str(), str.length(), 1, file) != 1 )
|
||||
goto write_error;
|
||||
|
||||
for ( int i = 0; i < num_fields; i++ )
|
||||
if ( ! WriteHeaderField("path", path) )
|
||||
goto write_error;
|
||||
|
||||
string names;
|
||||
string types;
|
||||
|
||||
for ( int i = 0; i < num_fields; ++i )
|
||||
{
|
||||
if ( i > 0 &&
|
||||
fwrite(separator, separator_len, 1, file) != 1 )
|
||||
goto write_error;
|
||||
|
||||
const LogField* field = fields[i];
|
||||
|
||||
if ( fputs(field->name.c_str(), file) == EOF )
|
||||
goto write_error;
|
||||
if ( i > 0 )
|
||||
{
|
||||
names += string(separator, separator_len);
|
||||
types += string(separator, separator_len);
|
||||
}
|
||||
|
||||
if ( fputc('\n', file) == EOF )
|
||||
const LogField* field = fields[i];
|
||||
names += field->name;
|
||||
types += type_name(field->type);
|
||||
}
|
||||
|
||||
if ( ! (WriteHeaderField("fields", names)
|
||||
&& WriteHeaderField("types", types)) )
|
||||
goto write_error;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ protected:
|
|||
private:
|
||||
bool IsSpecial(string path) { return path.find("/dev/") == 0; }
|
||||
bool DoWriteOne(ODesc* desc, LogVal* val, const LogField* field);
|
||||
bool WriteHeaderField(const string& key, const string& value);
|
||||
|
||||
FILE* file;
|
||||
string fname;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue