Skip to content

Commit

Permalink
Streamline argument passing in printer function
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Jan 18, 2025
1 parent a32fbcd commit c231972
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 50 deletions.
3 changes: 2 additions & 1 deletion src/nfdump/nfdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ static stat_record_t process_data(void *engine, int processMode, char *wfile, Re
record_header_t *record_ptr = GetCursor(dataBlock);

uint64_t recordCounter = dataHandle->recordCnt;
outputParams->ident = dataHandle->ident;

// successfully read block
total_bytes += dataBlock->size;
Expand Down Expand Up @@ -600,7 +601,7 @@ static stat_record_t process_data(void *engine, int processMode, char *wfile, Re
dataBlock_w = AppendToBuffer(nffile_w, dataBlock_w, (void *)record_ptr, record_ptr->size);
break;
case PRINTRECORD:
print_record(stdout, recordHandle, outputParams->doTag);
print_record(stdout, recordHandle, outputParams);
break;
}

Expand Down
6 changes: 3 additions & 3 deletions src/nfdump/nflowcache.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2024, Peter Haag
* Copyright (c) 2009-2025, Peter Haag
* Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
* All rights reserved.
*
Expand Down Expand Up @@ -1631,9 +1631,9 @@ static inline void PrintSortList(SortElement_t *SortList, uint64_t maxindex, out
}

if (outputParams->postFilter) {
if (FilterRecord(outputParams->postFilter, &recordHandle)) print_record(stdout, &recordHandle, outputParams->doTag);
if (FilterRecord(outputParams->postFilter, &recordHandle)) print_record(stdout, &recordHandle, outputParams);
} else {
print_record(stdout, &recordHandle, outputParams->doTag);
print_record(stdout, &recordHandle, outputParams);
}
}

Expand Down
14 changes: 5 additions & 9 deletions src/output/output.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Haag
* Copyright (c) 2024-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -80,7 +80,7 @@

static void AddFormat(char *format, char *name, char *fmtString);

static void null_record(FILE *stream, recordHandle_t *record, int tag);
static void null_record(FILE *stream, recordHandle_t *record, outputParams_t *outputParam);

static void null_prolog(outputParams_t *outputParam);

Expand Down Expand Up @@ -130,7 +130,7 @@ static PrologPrinter_t print_epilog; // prints the output epilog

static void UpdateFormatList(void);

static void null_record(FILE *stream, recordHandle_t *record, int tag) {
static void null_record(FILE *stream, recordHandle_t *record, outputParams_t *outputParam) {
// empty - do not list any flows
} // End of null_record

Expand Down Expand Up @@ -308,13 +308,9 @@ RecordPrinter_t SetupOutputMode(char *print_format, outputParams_t *outputParams

} // End of SetupOutputMode

void PrintProlog(outputParams_t *outputParams) {
print_prolog(outputParams);
} // End of PrintProlog
void PrintProlog(outputParams_t *outputParams) { print_prolog(outputParams); } // End of PrintProlog

void PrintEpilog(outputParams_t *outputParams) {
print_epilog(outputParams);
} // End of PrintEpilog
void PrintEpilog(outputParams_t *outputParams) { print_epilog(outputParams); } // End of PrintEpilog

void PrintOutputHelp(void) {
printf("Available output formats:\n");
Expand Down
13 changes: 4 additions & 9 deletions src/output/output.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Haag
* Copyright (c) 2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -36,13 +36,7 @@

#include "nfdump.h"

typedef enum { MODE_NULL = 0,
MODE_RAW,
MODE_FMT,
MODE_CSV,
MODE_CSV_FAST,
MODE_JSON,
MODE_NDJSON } outputMode_t;
typedef enum { MODE_NULL = 0, MODE_RAW, MODE_FMT, MODE_CSV, MODE_CSV_FAST, MODE_JSON, MODE_NDJSON } outputMode_t;

typedef struct outputParams_s {
bool printPlain;
Expand All @@ -52,10 +46,11 @@ typedef struct outputParams_s {
bool hasTorDB;
outputMode_t mode;
int topN;
char *ident;
void *postFilter;
} outputParams_t;

typedef void (*RecordPrinter_t)(FILE *, recordHandle_t *, int);
typedef void (*RecordPrinter_t)(FILE *, recordHandle_t *, outputParams_t *);
typedef void (*PrologPrinter_t)(outputParams_t *);
typedef void (*EpilogPrinter_t)(outputParams_t *);

Expand Down
6 changes: 3 additions & 3 deletions src/output/output_csv.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Haag
* Copyright (c) 2024-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -585,7 +585,7 @@ static void ListOutputFormats(void) {

} // End of ListOutputFormats

void csv_record(FILE *stream, recordHandle_t *recordHandle, int tag) {
void csv_record(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
EXtunIPv4_t *tunIPv4 = (EXtunIPv4_t *)recordHandle->extensionList[EXtunIPv4ID];
EXtunIPv6_t *tunIPv6 = (EXtunIPv6_t *)recordHandle->extensionList[EXtunIPv6ID];
Expand Down Expand Up @@ -618,7 +618,7 @@ void csv_record(FILE *stream, recordHandle_t *recordHandle, int tag) {
tunIPv6Flow->dstAddr[1] = tunIPv6->tunDstAddr[1];
tunRecordHandle.extensionList[EXipv6FlowID] = tunIPv6Flow;
}
csv_record(stream, &tunRecordHandle, tag);
csv_record(stream, &tunRecordHandle, outputParam);
free(p);
}

Expand Down
6 changes: 3 additions & 3 deletions src/output/output_csv.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Peter Haag
* Copyright (c) 2022-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -42,12 +42,12 @@ void csv_prolog(outputParams_t *outputParam);

void csv_epilog(outputParams_t *outputParam);

void csv_record(FILE *stream, recordHandle_t *recordHandle, int tag);
void csv_record(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam);

void csv_prolog_fast(outputParams_t *outputParam);

void csv_epilog_fast(outputParams_t *outputParam);

void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag);
void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam);

#endif // _OUTPUT_CSV_H
4 changes: 2 additions & 2 deletions src/output/output_csv_fast.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Haag
* Copyright (c) 2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -94,7 +94,7 @@ void csv_epilog_fast(outputParams_t *outputParam) {
streamBuff = NULL;
} // End of csv_epilog_fast

void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag) {
void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
EXipv4Flow_t *ipv4Flow = (EXipv4Flow_t *)recordHandle->extensionList[EXipv4FlowID];
EXipv6Flow_t *ipv6Flow = (EXipv6Flow_t *)recordHandle->extensionList[EXipv6FlowID];
Expand Down
10 changes: 4 additions & 6 deletions src/output/output_fmt.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2024, Peter Haag
* Copyright (c) 2009-2025, Peter Haag
* Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
* All rights reserved.
*
Expand Down Expand Up @@ -73,7 +73,6 @@ static int token_index = 0;

static int max_format_index = 0;

static int do_tag = 0;
static int long_v6 = 0;
static int printPlain = 0;
static double duration = 0;
Expand Down Expand Up @@ -615,7 +614,7 @@ static void ListOutputFormats(void) {

} // End of ListOutputFormats

void fmt_record(FILE *stream, recordHandle_t *recordHandle, int tag) {
void fmt_record(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam) {
EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID];
EXtunIPv4_t *tunIPv4 = (EXtunIPv4_t *)recordHandle->extensionList[EXtunIPv4ID];
EXtunIPv6_t *tunIPv6 = (EXtunIPv6_t *)recordHandle->extensionList[EXtunIPv6ID];
Expand Down Expand Up @@ -648,12 +647,11 @@ void fmt_record(FILE *stream, recordHandle_t *recordHandle, int tag) {
tunIPv6Flow->dstAddr[1] = tunIPv6->tunDstAddr[1];
tunRecordHandle.extensionList[EXipv6FlowID] = tunIPv6Flow;
}
fmt_record(stream, &tunRecordHandle, tag);
fmt_record(stream, &tunRecordHandle, outputParam);
free(p);
}

do_tag = tag;
tag_string[0] = do_tag ? TAG_CHAR : '\0';
tag_string[0] = outputParam->doTag ? TAG_CHAR : '\0';
tag_string[1] = '\0';

duration = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/output/output_fmt.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2024, Peter Haag
* Copyright (c) 2009-2025, Peter Haag
* Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
* All rights reserved.
*
Expand Down Expand Up @@ -55,7 +55,7 @@ void CondenseV6(char *s);

int ParseFMTOutputFormat(char *format, int printPlain);

void fmt_record(FILE *stream, recordHandle_t *recordHandle, int tag);
void fmt_record(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam);

#define TAG_CHAR ''

Expand Down
4 changes: 2 additions & 2 deletions src/output/output_json.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, Peter Haag
* Copyright (c) 2019-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -805,7 +805,7 @@ void json_epilog(outputParams_t *outputParam) {
streamBuff = NULL;
} // End of json_epilog

void flow_record_to_json(FILE *stream, recordHandle_t *recordHandle, int tag) {
void flow_record_to_json(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam) {
// ws is whitespace after object opening and before object closing {WS WS}
// ' ' is printed before each record for clarity if needed
// fs is Field Separator
Expand Down
4 changes: 2 additions & 2 deletions src/output/output_json.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Peter Haag
* Copyright (c) 2022-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -38,6 +38,6 @@ void json_prolog(outputParams_t *outputParam);

void json_epilog(outputParams_t *outputParam);

void flow_record_to_json(FILE *stream, recordHandle_t *recordHandle, int tag);
void flow_record_to_json(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam);

#endif // _OUTPUT_JSON_H
4 changes: 2 additions & 2 deletions src/output/output_ndjson.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, Peter Haag
* Copyright (c) 2019-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -791,7 +791,7 @@ void ndjson_epilog(outputParams_t *outputParam) {

enum { FORMAT_NDJSON = 0, FORMAT_JSON };

void flow_record_to_ndjson(FILE *stream, recordHandle_t *recordHandle, int tag) {
void flow_record_to_ndjson(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam) {
// ws is whitespace after object opening and before object closing {WS WS}
// indent is printed before each record for clarity if needed
// fs is Field Separator
Expand Down
4 changes: 2 additions & 2 deletions src/output/output_ndjson.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Peter Haag
* Copyright (c) 2022-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -38,6 +38,6 @@ void ndjson_prolog(outputParams_t *outputParam);

void ndjson_epilog(outputParams_t *outputParam);

void flow_record_to_ndjson(FILE *stream, recordHandle_t *recordHandle, int tag);
void flow_record_to_ndjson(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam);

#endif // _OUTPUT_NDJSON_H
4 changes: 2 additions & 2 deletions src/output/output_raw.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, Peter Haag
* Copyright (c) 2019-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -851,7 +851,7 @@ void raw_epilog(outputParams_t *outputParam) {
// empty epilog
} // End of raw_epilog

void raw_record(FILE *stream, recordHandle_t *recordHandle, int tag) {
void raw_record(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam) {
recordHeaderV3_t *recordHeaderV3 = recordHandle->recordHeaderV3;

char elementString[MAXEXTENSIONS * 5];
Expand Down
4 changes: 2 additions & 2 deletions src/output/output_raw.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Peter Haag
* Copyright (c) 2024-2025, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -40,6 +40,6 @@ void raw_prolog(outputParams_t *outputParam);

void raw_epilog(outputParams_t *outputParam);

void raw_record(FILE *stream, recordHandle_t *recordHandle, int tag);
void raw_record(FILE *stream, recordHandle_t *recordHandle, outputParams_t *outputParam);

#endif // _OUTPUT_RAW_H

0 comments on commit c231972

Please sign in to comment.