adding logging of sensors
[psensor.git] / src / server / server.c
index 0576d3c..510082e 100644 (file)
 #include "cpu.h"
 #endif
 
+#include "log.h"
 #include "psensor_json.h"
 #include "url.h"
 #include "server.h"
+#include "slog.h"
+
+static const char *DEFAULT_LOG_FILE = "/var/log/psensor-server.log";
+
+#define HTML_STOP_REQUESTED \
+(_("<html><body><p>Server stop requested</p></body></html>"))
 
 static const char *program_name;
 
@@ -57,7 +64,9 @@ static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"port", required_argument, 0, 'p'},
        {"wdir", required_argument, 0, 'w'},
-       {"debug", no_argument, 0, 'd'},
+       {"debug", required_argument, 0, 'd'},
+       {"log-file", required_argument, 0, 'l'},
+       {"sensor-log-file", required_argument, 0, 's'},
        {0, 0, 0, 0}
 };
 
@@ -65,8 +74,6 @@ static struct server_data server_data;
 
 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
-static int debug;
-
 static int server_stop_requested;
 
 void print_version()
@@ -84,8 +91,8 @@ void print_help()
 {
        printf(_("Usage: %s [OPTION]...\n"), program_name);
 
-       puts(_("psensor-server is an HTTP server "
-              "for monitoring hardware sensors remotely."));
+       puts(_("psensor-server is an HTTP server for monitoring hardware "
+              "sensors remotely."));
 
        puts("");
        puts("Options:");
@@ -93,13 +100,16 @@ void print_help()
               "  -v, --version         display version information and exit"));
 
        puts("");
-
-       puts(_("  -d,--debug            run in debug mode\n"
-              "  -p,--port=PORT        webserver port\n"
+       puts(_("  -p,--port=PORT        webserver port\n"
               "  -w,--wdir=DIR         directory containing webserver pages"));
 
        puts("");
+       puts(_("  -d, --debug=LEVEL     "
+              "set the debug level, integer between 0 and 3"));
+       puts(_("  -l, --log-file=PATH   set the log file to PATH"));
+       puts(_("  -s, --sensor-log-file=PATH set the sensor log file to PATH"));
 
+       puts("");
        printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
        puts("");
        printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
@@ -168,11 +178,10 @@ create_response_api(const char *nurl,
                if (s)
                        page = sensor_to_json_string(s);
 
-       } else if (debug && !strcmp(nurl, URL_API_1_0_SERVER_STOP)) {
+       } else if (!strcmp(nurl, URL_API_1_0_SERVER_STOP)) {
 
                server_stop_requested = 1;
-               page = strdup(_("<html><body><p>"
-                               "Server stop requested</p></body></html>"));
+               page = strdup(HTML_STOP_REQUESTED);
        }
 
        if (page) {
@@ -222,7 +231,7 @@ create_response_file(const char *nurl,
                                 (MHD_ContentReaderFreeCallback)&fclose);
 
                } else {
-                       log_printf(LOG_ERR, "Failed to open: %s\n", fpath);
+                       log_err("Failed to open: %s.", fpath);
                }
        }
 
@@ -285,8 +294,7 @@ cbk_http_request(void *cls,
 
        *ptr = NULL;            /* clear context pointer */
 
-       if (debug)
-               printf(_("HTTP Request: %s\n"), url);
+       log_debug(_("HTTP Request: %s"), url);
 
        nurl = url_normalize(url);
 
@@ -308,6 +316,7 @@ int main(int argc, char *argv[])
        int port = DEFAULT_PORT;
        int optc;
        int cmdok = 1;
+       char *log_file, *slog_file;
 
        program_name = argv[0];
 
@@ -320,9 +329,11 @@ int main(int argc, char *argv[])
 
        server_data.www_dir = NULL;
        server_data.psysinfo.interfaces = NULL;
+       log_file = NULL;
+       slog_file = NULL;
 
        while ((optc = getopt_long(argc, argv,
-                                  "vhp:w:d", long_options, NULL)) != -1) {
+                                  "vhp:w:d:l:s:", long_options, NULL)) != -1) {
                switch (optc) {
                case 'w':
                        if (optarg)
@@ -339,7 +350,16 @@ int main(int argc, char *argv[])
                        print_version();
                        exit(EXIT_SUCCESS);
                case 'd':
-                       debug = 1;
+                       log_level = atoi(optarg);
+                       log_info(_("Enables debug mode: %d"), log_level);
+                       break;
+               case 'l':
+                       if (optarg)
+                               log_file = strdup(optarg);
+                       break;
+               case 's':
+                       if (optarg)
+                               slog_file = strdup(optarg);
                        break;
                default:
                        cmdok = 0;
@@ -347,7 +367,11 @@ int main(int argc, char *argv[])
                }
        }
 
-       server_data.www_dir = strdup(DEFAULT_WWW_DIR);
+       if (!server_data.www_dir)
+               server_data.www_dir = strdup(DEFAULT_WWW_DIR);
+
+       if (!log_file)
+               log_file = strdup(DEFAULT_LOG_FILE);
 
        if (!cmdok || optind != argc) {
                fprintf(stderr, _("Try `%s --help' for more information.\n"),
@@ -355,6 +379,8 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
 
+       log_open(log_file);
+
        psensor_init();
 
        server_data.sensors = get_all_sensors(0, 600);
@@ -364,20 +390,23 @@ int main(int argc, char *argv[])
 #endif
 
        if (!*server_data.sensors)
-               fprintf(stderr, _("ERROR: no sensors detected\n"));
+               log_err(_("No sensors detected."));
 
        d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
                             port,
                             NULL, NULL, &cbk_http_request, server_data.sensors,
                             MHD_OPTION_END);
        if (!d) {
-               fprintf(stderr, _("ERROR: Fail to create web server\n"));
+               log_err(_("Failed to create Web server."));
                exit(EXIT_FAILURE);
        }
 
-       log_printf(LOG_INFO, _("Web server started on port: %d"), port);
-       log_printf(LOG_INFO, _("WWW directory: %s"), server_data.www_dir);
-       log_printf(LOG_INFO, _("URL: http://localhost:%d"), port);
+       log_info(_("Web server started on port: %d"), port);
+       log_info(_("WWW directory: %s"), server_data.www_dir);
+       log_info(_("URL: http://localhost:%d"), port);
+
+       if (slog_file)
+               slog_init(slog_file, server_data.sensors);
 
        while (!server_stop_requested) {
                pthread_mutex_lock(&mutex);
@@ -388,10 +417,16 @@ int main(int argc, char *argv[])
 #endif
                psensor_list_update_measures(server_data.sensors);
 
+               psensor_log_measures(server_data.sensors);
+
+               slog_write_sensors(server_data.sensors);
+
                pthread_mutex_unlock(&mutex);
                sleep(5);
        }
 
+       slog_close();
+
        MHD_stop_daemon(d);
 
        /* sanity cleanup for valgrind */
@@ -407,5 +442,8 @@ int main(int argc, char *argv[])
        cpu_cleanup();
 #endif
 
+       if (log_file != DEFAULT_LOG_FILE)
+               free(log_file);
+
        return EXIT_SUCCESS;
 }