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
|
@ -11,7 +11,7 @@ export {
|
||||||
const include_header = T &redef;
|
const include_header = T &redef;
|
||||||
|
|
||||||
## Prefix for the header line if included.
|
## Prefix for the header line if included.
|
||||||
const header_prefix = "# " &redef;
|
const header_prefix = "#" &redef;
|
||||||
|
|
||||||
## Separator between fields.
|
## Separator between fields.
|
||||||
const separator = "\t" &redef;
|
const separator = "\t" &redef;
|
||||||
|
|
|
@ -6,6 +6,27 @@
|
||||||
#include "LogWriterAscii.h"
|
#include "LogWriterAscii.h"
|
||||||
#include "NetVar.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()
|
LogWriterAscii::LogWriterAscii()
|
||||||
{
|
{
|
||||||
file = 0;
|
file = 0;
|
||||||
|
@ -52,6 +73,14 @@ LogWriterAscii::~LogWriterAscii()
|
||||||
delete [] header_prefix;
|
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,
|
bool LogWriterAscii::DoInit(string path, int num_fields,
|
||||||
const LogField* const * fields)
|
const LogField* const * fields)
|
||||||
{
|
{
|
||||||
|
@ -70,22 +99,35 @@ bool LogWriterAscii::DoInit(string path, int num_fields,
|
||||||
|
|
||||||
if ( include_header )
|
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;
|
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 &&
|
if ( i > 0 )
|
||||||
fwrite(separator, separator_len, 1, file) != 1 )
|
{
|
||||||
goto write_error;
|
names += string(separator, separator_len);
|
||||||
|
types += string(separator, separator_len);
|
||||||
|
}
|
||||||
|
|
||||||
const LogField* field = fields[i];
|
const LogField* field = fields[i];
|
||||||
|
names += field->name;
|
||||||
if ( fputs(field->name.c_str(), file) == EOF )
|
types += type_name(field->type);
|
||||||
goto write_error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( fputc('\n', file) == EOF )
|
if ( ! (WriteHeaderField("fields", names)
|
||||||
|
&& WriteHeaderField("types", types)) )
|
||||||
goto write_error;
|
goto write_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ protected:
|
||||||
private:
|
private:
|
||||||
bool IsSpecial(string path) { return path.find("/dev/") == 0; }
|
bool IsSpecial(string path) { return path.find("/dev/") == 0; }
|
||||||
bool DoWriteOne(ODesc* desc, LogVal* val, const LogField* field);
|
bool DoWriteOne(ODesc* desc, LogVal* val, const LogField* field);
|
||||||
|
bool WriteHeaderField(const string& key, const string& value);
|
||||||
|
|
||||||
FILE* file;
|
FILE* file;
|
||||||
string fname;
|
string fname;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue