Skip to content

Commit 2a944ca

Browse files
authored
Merge pull request #73 from CESNET/hutak-fdsdump-unix-timestamp
Hutak fdsdump unix timestamp
2 parents c18bbd7 + f9a9676 commit 2a944ca

File tree

4 files changed

+77
-22
lines changed

4 files changed

+77
-22
lines changed

src/tools/fdsdump/src/common/common.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@
66
namespace fdsdump {
77

88
std::vector<std::string>
9-
string_split(const std::string &str, const std::string &delimiter)
9+
string_split(
10+
const std::string &str,
11+
const std::string &delimiter,
12+
unsigned int max_pieces)
1013
{
1114
std::vector<std::string> pieces;
1215
std::size_t pos = 0;
1316

1417
for (;;) {
18+
if (max_pieces > 0 && pieces.size() == max_pieces - 1) {
19+
pieces.emplace_back(str.begin() + pos, str.end());
20+
break;
21+
}
22+
1523
std::size_t next_pos = str.find(delimiter, pos);
1624
if (next_pos == std::string::npos) {
1725
pieces.emplace_back(str.begin() + pos, str.end());
1826
break;
1927
}
2028
pieces.emplace_back(str.begin() + pos, str.begin() + next_pos);
21-
pos = next_pos + 1;
29+
pos = next_pos + delimiter.size();
2230
}
2331
return pieces;
2432
}

src/tools/fdsdump/src/common/common.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ using shared_tsnapshot = std::shared_ptr<fds_tsnapshot_t>;
2727
* If no delimeter is found, the result contains only the original string.
2828
* @param str String to be splitted
2929
* @param delimiter String delimeter
30+
* @param max_pieces The maximum number of pieces (0 = no limit)
3031
* @return Vector of piecies.
3132
*/
3233
std::vector<std::string>
33-
string_split(const std::string &str, const std::string &delimiter);
34+
string_split(
35+
const std::string &str,
36+
const std::string &delimiter,
37+
unsigned int max_pieces = 0);
3438

3539
/**
3640
* @brief Split string by a delimiter from right.

src/tools/fdsdump/src/lister/jsonPrinter.cpp

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,40 @@ JsonPrinter::parse_fields(const std::string &str, const shared_iemgr &iemgr)
5252
void
5353
JsonPrinter::parse_opts(const std::string &str)
5454
{
55-
std::vector<std::string> opt_names;
55+
std::vector<std::string> opts;
5656

5757
if (str.empty()) {
5858
return;
5959
}
6060

61-
opt_names = string_split(str, ",");
61+
opts = string_split(str, ",");
6262

63-
for (const std::string &opt_raw : opt_names) {
63+
for (const std::string &opt_raw : opts) {
6464
std::string opt = string_trim_copy(opt_raw);
65+
std::vector<std::string> opt_parts = string_split(opt, "=", 2);
66+
const std::string &opt_name = (opt_parts.size() >= 1) ? opt_parts[0] : "";
67+
const std::string &opt_value = (opt_parts.size() >= 2) ? opt_parts[1] : "";
6568

6669
if (strcasecmp(opt.c_str(), "no-biflow-split") == 0) {
6770
m_biflow_split = false;
71+
72+
} else if (strcasecmp(opt_name.c_str(), "timestamp") == 0) {
73+
if (opt_value.empty()) {
74+
throw std::invalid_argument(
75+
"JSON output: timestamp option is missing value"
76+
", use 'timestamp=unix' or 'timestamp=formatted'");
77+
}
78+
79+
if (opt_value == "unix") {
80+
m_format_timestamp = false;
81+
} else if (opt_value == "formatted") {
82+
m_format_timestamp = true;
83+
} else {
84+
throw std::invalid_argument(
85+
"JSON output: invalid timestamp option '" + opt_value + "'"
86+
", use 'timestamp=unix' or 'timestamp=formatted'");
87+
}
88+
6889
} else {
6990
throw std::invalid_argument("JSON output: unknown option '" + opt + "'");
7091
}
@@ -306,24 +327,45 @@ JsonPrinter::append_boolean(const fds_drec_field &field)
306327
void
307328
JsonPrinter::append_timestamp(const fds_drec_field &field)
308329
{
309-
char buffer[FDS_CONVERT_STRLEN_DATE];
310-
int ret;
330+
if (m_format_timestamp) {
331+
// Convert to formatted string
332+
char buffer[FDS_CONVERT_STRLEN_DATE];
333+
int ret;
334+
335+
ret = fds_datetime2str_be(
336+
field.data,
337+
field.size,
338+
field.info->def->data_type,
339+
buffer,
340+
sizeof(buffer),
341+
FDS_CONVERT_TF_MSEC_UTC);
342+
if (ret < 0) {
343+
append_invalid();
344+
return;
345+
}
311346

312-
ret = fds_datetime2str_be(
313-
field.data,
314-
field.size,
315-
field.info->def->data_type,
316-
buffer,
317-
sizeof(buffer),
318-
FDS_CONVERT_TF_MSEC_UTC);
319-
if (ret < 0) {
320-
append_invalid();
321-
return;
322-
}
347+
m_buffer.push_back('"');
348+
m_buffer.append(buffer);
349+
m_buffer.push_back('"');
323350

324-
m_buffer.push_back('"');
325-
m_buffer.append(buffer);
326-
m_buffer.push_back('"');
351+
352+
} else {
353+
// Convert to UNIX timestamp (in milliseconds)
354+
uint64_t time;
355+
if (fds_get_datetime_lp_be(field.data, field.size, field.info->def->data_type, &time) != FDS_OK) {
356+
append_invalid();
357+
return;
358+
}
359+
360+
time = htobe64(time); // Convert to network byte order and use fast libfds converter
361+
char buffer[32];
362+
if (fds_uint2str_be(&time, sizeof(time), buffer, sizeof(buffer)) < 0) {
363+
append_invalid();
364+
return;
365+
}
366+
367+
m_buffer.append(buffer);
368+
}
327369
}
328370

329371
void

src/tools/fdsdump/src/lister/jsonPrinter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class JsonPrinter : public Printer {
5353
std::string m_buffer;
5454
unsigned int m_rec_printed = 0;
5555
bool m_biflow_split = true;
56+
bool m_format_timestamp = true;
5657
};
5758

5859
} // lister

0 commit comments

Comments
 (0)