commit 84c72daa5d030e02315fe8f9be18bcdf88fc719f
parent af60b7592172ef0d8c272ab671aeb41a9316d7ac
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date: Wed, 24 Jun 2015 22:14:05 +0200
Moving the JSON output code to its own function
Diffstat:
M | src/logswan.c | | | 86 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
1 file changed, 45 insertions(+), 41 deletions(-)
diff --git a/src/logswan.c b/src/logswan.c
@@ -69,6 +69,50 @@ FILE *logFile;
char *endptr;
int getoptFlag;
+void output(Results results) {
+ json_t *jsonObject = json_object();
+ json_t *hitsObject = json_object();
+ json_t *countriesObject = json_object();
+ json_t *hoursObject = json_object();
+
+ for (int loop=0; loop<255; loop++) {
+ if (results.countries[loop] != 0) {
+ json_object_set_new(countriesObject, GeoIP_code_by_id(loop), json_integer(results.countries[loop]));
+ }
+ }
+
+ char *hoursString[] = {
+ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
+ };
+
+ for (int loop=0; loop<24; loop++) {
+ if (results.hours[loop] != 0) {
+ json_object_set_new(hoursObject, hoursString[loop], json_integer(results.hours[loop]));
+ }
+ }
+
+ json_object_set_new(hitsObject, "ipv4", json_integer(results.hitsIPv4));
+ json_object_set_new(hitsObject, "ipv6", json_integer(results.hitsIPv6));
+ json_object_set_new(hitsObject, "total", json_integer(results.hits));
+ json_object_set_new(hitsObject, "countries", countriesObject);
+ json_object_set_new(hitsObject, "hours", hoursObject);
+
+ json_object_set_new(jsonObject, "date", json_string(results.timeStamp));
+ json_object_set_new(jsonObject, "file_size", json_integer(results.fileSize));
+ json_object_set_new(jsonObject, "processed_lines", json_integer(results.processedLines));
+ json_object_set_new(jsonObject, "invalid_lines", json_integer(results.invalidLines));
+ json_object_set_new(jsonObject, "bandwidth", json_integer(results.bandwidth));
+ json_object_set_new(jsonObject, "runtime", json_real(results.runtime));
+ json_object_set_new(jsonObject, "hits", hitsObject);
+
+ printf("%s", json_dumps(jsonObject, JSON_INDENT(3) | JSON_PRESERVE_ORDER));
+
+ json_decref(jsonObject);
+ json_decref(hitsObject);
+ json_decref(countriesObject);
+ json_decref(hoursObject);
+}
+
int main (int argc, char *argv[]) {
printf("-------------------------------------------------------------------------------\n" \
" Logswan (c) by Frederic Cambus 2015 \n" \
@@ -172,47 +216,7 @@ int main (int argc, char *argv[]) {
printf("Processed %" PRIu64 " lines in %f seconds\n", results.processedLines, results.runtime);
fclose(logFile);
- json_t *jsonObject = json_object();
- json_t *hitsObject = json_object();
- json_t *countriesObject = json_object();
- json_t *hoursObject = json_object();
-
- for (int loop=0; loop<255; loop++) {
- if (results.countries[loop] != 0) {
- json_object_set_new(countriesObject, GeoIP_code_by_id(loop), json_integer(results.countries[loop]));
- }
- }
-
- char *hoursString[] = {
- "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
- };
-
- for (int loop=0; loop<24; loop++) {
- if (results.hours[loop] != 0) {
- json_object_set_new(hoursObject, hoursString[loop], json_integer(results.hours[loop]));
- }
- }
-
- json_object_set_new(hitsObject, "ipv4", json_integer(results.hitsIPv4));
- json_object_set_new(hitsObject, "ipv6", json_integer(results.hitsIPv6));
- json_object_set_new(hitsObject, "total", json_integer(results.hits));
- json_object_set_new(hitsObject, "countries", countriesObject);
- json_object_set_new(hitsObject, "hours", hoursObject);
-
- json_object_set_new(jsonObject, "date", json_string(results.timeStamp));
- json_object_set_new(jsonObject, "file_size", json_integer(results.fileSize));
- json_object_set_new(jsonObject, "processed_lines", json_integer(results.processedLines));
- json_object_set_new(jsonObject, "invalid_lines", json_integer(results.invalidLines));
- json_object_set_new(jsonObject, "bandwidth", json_integer(results.bandwidth));
- json_object_set_new(jsonObject, "runtime", json_real(results.runtime));
- json_object_set_new(jsonObject, "hits", hitsObject);
-
- printf("%s", json_dumps(jsonObject, JSON_INDENT(3) | JSON_PRESERVE_ORDER));
-
- json_decref(jsonObject);
- json_decref(hitsObject);
- json_decref(countriesObject);
- json_decref(hoursObject);
+ output(results);
return EXIT_SUCCESS;
}