mirror of
https://github.com/zeek/zeek.git
synced 2025-10-03 07:08:19 +00:00
Reformat the world
This commit is contained in:
parent
194cb24547
commit
b2f171ec69
714 changed files with 35149 additions and 35203 deletions
|
@ -1,23 +1,24 @@
|
|||
// See the file "COPYING" in the main distribution directory for copyright.
|
||||
|
||||
#include "zeek/zeek-config.h"
|
||||
#include "zeek/threading/formatters/JSON.h"
|
||||
|
||||
#include "zeek/zeek-config.h"
|
||||
|
||||
#ifndef __STDC_LIMIT_MACROS
|
||||
#define __STDC_LIMIT_MACROS
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include <rapidjson/internal/ieee754.h>
|
||||
#include <stdint.h>
|
||||
#include <sstream>
|
||||
|
||||
#include <rapidjson/internal/ieee754.h>
|
||||
|
||||
#include "zeek/Desc.h"
|
||||
#include "zeek/threading/MsgThread.h"
|
||||
|
||||
namespace zeek::threading::formatter {
|
||||
namespace zeek::threading::formatter
|
||||
{
|
||||
|
||||
bool JSON::NullDoubleWriter::Double(double d)
|
||||
{
|
||||
|
@ -32,12 +33,9 @@ JSON::JSON(MsgThread* t, TimeFormat tf) : Formatter(t), surrounding_braces(true)
|
|||
timestamps = tf;
|
||||
}
|
||||
|
||||
JSON::~JSON()
|
||||
{
|
||||
}
|
||||
JSON::~JSON() { }
|
||||
|
||||
bool JSON::Describe(ODesc* desc, int num_fields, const Field* const * fields,
|
||||
Value** vals) const
|
||||
bool JSON::Describe(ODesc* desc, int num_fields, const Field* const* fields, Value** vals) const
|
||||
{
|
||||
rapidjson::StringBuffer buffer;
|
||||
NullDoubleWriter writer(buffer);
|
||||
|
@ -79,8 +77,8 @@ bool JSON::Describe(ODesc* desc, Value* val, const std::string& name) const
|
|||
return true;
|
||||
}
|
||||
|
||||
Value* JSON::ParseValue(const std::string& s, const std::string& name,
|
||||
TypeTag type, TypeTag subtype) const
|
||||
Value* JSON::ParseValue(const std::string& s, const std::string& name, TypeTag type,
|
||||
TypeTag subtype) const
|
||||
{
|
||||
GetThread()->Error("JSON formatter does not support parsing yet.");
|
||||
return nullptr;
|
||||
|
@ -129,78 +127,80 @@ void JSON::BuildJSON(NullDoubleWriter& writer, Value* val, const std::string& na
|
|||
break;
|
||||
|
||||
case TYPE_TIME:
|
||||
{
|
||||
if ( timestamps == TS_ISO8601 )
|
||||
{
|
||||
char buffer[40];
|
||||
char buffer2[48];
|
||||
time_t the_time = time_t(floor(val->val.double_val));
|
||||
struct tm t;
|
||||
|
||||
if ( ! gmtime_r(&the_time, &t) ||
|
||||
! strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", &t) )
|
||||
if ( timestamps == TS_ISO8601 )
|
||||
{
|
||||
GetThread()->Error(GetThread()->Fmt("json formatter: failure getting time: (%lf)", val->val.double_val));
|
||||
// This was a failure, doesn't really matter what gets put here
|
||||
// but it should probably stand out...
|
||||
writer.String("2000-01-01T00:00:00.000000");
|
||||
char buffer[40];
|
||||
char buffer2[48];
|
||||
time_t the_time = time_t(floor(val->val.double_val));
|
||||
struct tm t;
|
||||
|
||||
if ( ! gmtime_r(&the_time, &t) ||
|
||||
! strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", &t) )
|
||||
{
|
||||
GetThread()->Error(GetThread()->Fmt(
|
||||
"json formatter: failure getting time: (%lf)", val->val.double_val));
|
||||
// This was a failure, doesn't really matter what gets put here
|
||||
// but it should probably stand out...
|
||||
writer.String("2000-01-01T00:00:00.000000");
|
||||
}
|
||||
else
|
||||
{
|
||||
double integ;
|
||||
double frac = modf(val->val.double_val, &integ);
|
||||
|
||||
if ( frac < 0 )
|
||||
frac += 1;
|
||||
|
||||
snprintf(buffer2, sizeof(buffer2), "%s.%06.0fZ", buffer,
|
||||
fabs(frac) * 1000000);
|
||||
writer.String(buffer2, strlen(buffer2));
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
else if ( timestamps == TS_EPOCH )
|
||||
writer.Double(val->val.double_val);
|
||||
|
||||
else if ( timestamps == TS_MILLIS )
|
||||
{
|
||||
double integ;
|
||||
double frac = modf(val->val.double_val, &integ);
|
||||
|
||||
if ( frac < 0 )
|
||||
frac += 1;
|
||||
|
||||
snprintf(buffer2, sizeof(buffer2), "%s.%06.0fZ", buffer, fabs(frac) * 1000000);
|
||||
writer.String(buffer2, strlen(buffer2));
|
||||
// ElasticSearch uses milliseconds for timestamps
|
||||
writer.Uint64((uint64_t)(val->val.double_val * 1000));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
else if ( timestamps == TS_EPOCH )
|
||||
writer.Double(val->val.double_val);
|
||||
|
||||
else if ( timestamps == TS_MILLIS )
|
||||
{
|
||||
// ElasticSearch uses milliseconds for timestamps
|
||||
writer.Uint64((uint64_t) (val->val.double_val * 1000));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TYPE_ENUM:
|
||||
case TYPE_STRING:
|
||||
case TYPE_FILE:
|
||||
case TYPE_FUNC:
|
||||
{
|
||||
writer.String(util::json_escape_utf8(
|
||||
std::string(val->val.string_val.data, val->val.string_val.length)));
|
||||
break;
|
||||
}
|
||||
{
|
||||
writer.String(util::json_escape_utf8(
|
||||
std::string(val->val.string_val.data, val->val.string_val.length)));
|
||||
break;
|
||||
}
|
||||
|
||||
case TYPE_TABLE:
|
||||
{
|
||||
writer.StartArray();
|
||||
{
|
||||
writer.StartArray();
|
||||
|
||||
for ( bro_int_t idx = 0; idx < val->val.set_val.size; idx++ )
|
||||
BuildJSON(writer, val->val.set_val.vals[idx]);
|
||||
for ( bro_int_t idx = 0; idx < val->val.set_val.size; idx++ )
|
||||
BuildJSON(writer, val->val.set_val.vals[idx]);
|
||||
|
||||
writer.EndArray();
|
||||
break;
|
||||
}
|
||||
writer.EndArray();
|
||||
break;
|
||||
}
|
||||
|
||||
case TYPE_VECTOR:
|
||||
{
|
||||
writer.StartArray();
|
||||
{
|
||||
writer.StartArray();
|
||||
|
||||
for ( bro_int_t idx = 0; idx < val->val.vector_val.size; idx++ )
|
||||
BuildJSON(writer, val->val.vector_val.vals[idx]);
|
||||
for ( bro_int_t idx = 0; idx < val->val.vector_val.size; idx++ )
|
||||
BuildJSON(writer, val->val.vector_val.vals[idx]);
|
||||
|
||||
writer.EndArray();
|
||||
break;
|
||||
}
|
||||
writer.EndArray();
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
reporter->Warning("Unhandled type in JSON::BuildJSON");
|
||||
|
@ -208,4 +208,4 @@ void JSON::BuildJSON(NullDoubleWriter& writer, Value* val, const std::string& na
|
|||
}
|
||||
}
|
||||
|
||||
} // namespace zeek::threading::formatter
|
||||
} // namespace zeek::threading::formatter
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue