Permalink
Browse files

Handle the --log option correctly

  • Loading branch information...
1 parent 1af2f7e commit 127a2e9464994bac57423958a04bffcec5a8dcf0 @rosslagerwall committed Aug 20, 2012
Showing with 104 additions and 2 deletions.
  1. +1 −1 Makefile.am
  2. +51 −0 src/log.c
  3. +27 −0 src/log.h
  4. +25 −1 src/main.c
View
@@ -2,7 +2,7 @@ bin_PROGRAMS=yummycacher
yummycacher_SOURCES=src/main.c src/pserv.c src/pserv.h src/dns.c src/dns.h \
src/protoshttp.c src/protoshttp.h src/pclient.c src/pclient.h \
src/protochttp.c src/protochttp.h src/cxmap.h src/cxmap.c \
- src/options.c src/options.h
+ src/options.c src/options.h src/log.c src/log.h
AM_CPPFLAGS = $(DEPS_CFLAGS) $(DEPS2_CFLAGS)
yummycacher_LDADD = -levent_core -levent_extra $(DEPS2_LIBS)
View
@@ -0,0 +1,51 @@
+/*
+ Copyright (C) 2012 The yummycacher Authors
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "log.h"
+#include "options.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <glib.h>
+
+FILE *log_file;
+
+void
+log_handler(const char *log_domain, GLogLevelFlags log_level,
+ const char *message, gpointer user_data)
+{
+ fprintf(log_file, "%s\n", message);
+}
+
+/* Set up logging */
+void
+log_init(void)
+{
+ if (strcmp(y_options.log, "-")) {
+ log_file = fopen(y_options.log, "a");
+ setlinebuf(log_file);
+ g_log_set_handler(NULL, G_LOG_LEVEL_MASK, log_handler, NULL);
+ }
+}
+
+void
+log_close(void)
+{
+ if (log_file != NULL)
+ fclose(log_file);
+}
View
@@ -0,0 +1,27 @@
+/*
+ Copyright (C) 2012 The yummycacher Authors
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef LOG_H
+#define LOG_H
+
+void
+log_init(void);
+
+void
+log_close(void);
+
+#endif
View
@@ -19,6 +19,7 @@
#include "pserv.h"
#include "dns.h"
#include "options.h"
+#include "log.h"
#include <glib.h>
#include <event2/event.h>
@@ -82,11 +83,34 @@ start_loop(void)
return 0;
}
+void
+handler(int signum)
+{
+ log_close();
+ exit(0);
+}
+
+void
+setup_signals(void)
+{
+ struct sigaction sa;
+ sa.sa_flags = SA_SIGINFO;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_handler = handler;
+ if (sigaction(SIGINT, &sa, NULL) == -1)
+ g_error("Error setting up signals");
+}
+
int main(int argc, char **argv) {
+ int res;
if (!parse_options(argc, argv) || y_options.help) {
print_usage(argv[0]);
return 1;
}
+ setup_signals();
+ log_init();
cxmap_init();
- return start_loop();
+ res = start_loop();
+ log_close();
+ return res;
}

0 comments on commit 127a2e9

Please sign in to comment.