commit aad349c8cbdbd4e185dba4e42166822a43e84f32
parent 84c72daa5d030e02315fe8f9be18bcdf88fc719f
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date: Wed, 24 Jun 2015 22:29:02 +0200
Moving the 'output' function to its own file
Diffstat:
5 files changed, 120 insertions(+), 64 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -17,6 +17,6 @@ include_directories(/usr/local/include)
add_definitions(-Wall -Wextra -Werror -std=c99 -pedantic)
-add_executable(logswan src/logswan.c src/parse.c)
+add_executable(logswan src/logswan.c src/output.c src/parse.c)
target_link_libraries(logswan ${LIB_GEOIP} ${LIB_JANSSON})
diff --git a/src/logswan.c b/src/logswan.c
@@ -4,7 +4,7 @@
/* https://github.com/fcambus/logswan */
/* */
/* Created: 2015/05/31 */
-/* Last Updated: 2015/06/22 */
+/* Last Updated: 2015/06/24 */
/* */
/* Logswan is released under the BSD 3-Clause license. */
/* See LICENSE file for details. */
@@ -22,9 +22,10 @@
#include <time.h>
#include <GeoIP.h>
-#include <jansson.h>
+#include "output.h"
#include "parse.h"
+#include "results.h"
#define VERSION "Logswan"
#define LINE_MAX_LENGTH 4096
@@ -35,23 +36,6 @@ clock_t begin, end;
char lineBuffer[LINE_MAX_LENGTH];
-struct results {
- uint64_t fileSize;
- uint64_t invalidLines;
- uint64_t processedLines;
- uint64_t bandwidth;
- uint64_t hits;
- uint64_t hitsIPv4;
- uint64_t hitsIPv6;
- uint64_t countries[255];
- int hours[24];
- int httpStatus[512];
- double runtime;
- char timeStamp[20];
-};
-
-typedef struct results Results;
-
Results results;
struct date parsedDate;
struct logLine parsedLine;
@@ -69,50 +53,6 @@ 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" \
diff --git a/src/output.c b/src/output.c
@@ -0,0 +1,61 @@
+/*****************************************************************************/
+/* */
+/* Logswan (c) by Frederic Cambus 2015 */
+/* https://github.com/fcambus/logswan */
+/* */
+/* Created: 2015/05/31 */
+/* Last Updated: 2015/06/24 */
+/* */
+/* Logswan is released under the BSD 3-Clause license. */
+/* See LICENSE file for details. */
+/* */
+/*****************************************************************************/
+
+#include <GeoIP.h>
+#include <jansson.h>
+
+#include "results.h"
+
+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);
+}
diff --git a/src/output.h b/src/output.h
@@ -0,0 +1,21 @@
+/*****************************************************************************/
+/* */
+/* Logswan (c) by Frederic Cambus 2015 */
+/* https://github.com/fcambus/logswan */
+/* */
+/* Created: 2015/05/31 */
+/* Last Updated: 2015/06/24 */
+/* */
+/* Logswan is released under the BSD 3-Clause license. */
+/* See LICENSE file for details. */
+/* */
+/*****************************************************************************/
+
+#ifndef OUTPUT_H
+#define OUTPUT_H
+
+#include "results.h"
+
+void output(Results results);
+
+#endif
diff --git a/src/results.h b/src/results.h
@@ -0,0 +1,34 @@
+/*****************************************************************************/
+/* */
+/* Logswan (c) by Frederic Cambus 2015 */
+/* https://github.com/fcambus/logswan */
+/* */
+/* Created: 2015/05/31 */
+/* Last Updated: 2015/06/24 */
+/* */
+/* Logswan is released under the BSD 3-Clause license. */
+/* See LICENSE file for details. */
+/* */
+/*****************************************************************************/
+
+#ifndef RESULTS_H
+#define RESULTS_H
+
+struct results {
+ uint64_t fileSize;
+ uint64_t invalidLines;
+ uint64_t processedLines;
+ uint64_t bandwidth;
+ uint64_t hits;
+ uint64_t hitsIPv4;
+ uint64_t hitsIPv6;
+ uint64_t countries[255];
+ int hours[24];
+ int httpStatus[512];
+ double runtime;
+ char timeStamp[20];
+};
+
+typedef struct results Results;
+
+#endif