fixed --wdir option.
[psensor.git] / src / server / server.c
index 44438e2..32b3cfd 100644 (file)
@@ -1,21 +1,21 @@
 /*
   Copyright (C) 2010-2011 jeanfi@gmail.com
-
-    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 2 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, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
-*/
* Copyright (C) 2010-2012 jeanfi@gmail.com
+ *
+ * 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
+ */
 #include <locale.h>
 #include <libintl.h>
 #define _(str) gettext(str)
 #include "cpu.h"
 #endif
 
+#include "log.h"
 #include "psensor_json.h"
 #include "url.h"
-#include "p_io.h"
 #include "server.h"
 
+static const char *DEFAULT_LOG_FILE = "/var/log/psensor-server.log";
+
 static const char *program_name;
 
 #define DEFAULT_PORT 3131
 
-#define PAGE_NOT_FOUND (_("<html><body><p>\
-Page not found - Go to <a href='/'>Main page</a>\
-</p></body>"))
+#define PAGE_NOT_FOUND (_("<html><body><p>"\
+"Page not found - Go to <a href='/'>Main page</a></p></body>"))
 
 static struct option long_options[] = {
        {"version", no_argument, 0, 'v'},
        {"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'},
        {0, 0, 0, 0}
 };
 
@@ -67,19 +69,17 @@ 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()
 {
        printf("psensor-server %s\n", VERSION);
-       printf(_("Copyright (C) %s jeanfi@gmail.com\n\
-License GPLv2: GNU GPL version 2 or later \
-<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
-This is free software: you are free to change and redistribute it.\n\
-There is NO WARRANTY, to the extent permitted by law.\n"),
-              "2010-2011");
+       printf(_("Copyright (C) %s jeanfi@gmail.com\n"
+                "License GPLv2: GNU GPL version 2 or later "
+                "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
+                "This is free software: you are free to change and redistribute it.\n"
+                "There is NO WARRANTY, to the extent permitted by law.\n"),
+              "2010-2012");
 }
 
 void print_help()
@@ -91,19 +91,20 @@ void print_help()
 
        puts("");
        puts("Options:");
-       puts(_("\
-  -h, --help           display this help and exit\n\
-  -v, --version                display version information and exit"));
+       puts(_("  -h, --help            display this help and exit\n"
+              "  -v, --version         display version information and exit"));
 
        puts("");
-
-       puts(_("\
-  -d,--debug           run in debug mode\n\
-  -p,--port=PORT       webserver port\n\
-  -w,--wdir=DIR                directory containing webserver pages"));
+       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("");
        printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
        puts("");
        printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
@@ -131,7 +132,7 @@ char *get_path(const char *url, const char *www_dir)
 }
 
 #if MHD_VERSION >= 0x00090200
-static int
+static ssize_t
 file_reader(void *cls, uint64_t pos, char *buf, size_t max)
 #else
 static int
@@ -154,12 +155,9 @@ create_response_api(const char *nurl,
        char *page = NULL;
 
        if (!strcmp(nurl, URL_BASE_API_1_0_SENSORS))  {
-
                page = sensors_to_json_string(server_data.sensors);
-
 #ifdef HAVE_GTOP
        } else if (!strcmp(nurl, URL_API_1_0_SYSINFO)) {
-
                page = sysinfo_to_json_string(&server_data.psysinfo);
        } else if (!strcmp(nurl, URL_API_1_0_CPU_USAGE)) {
                page = sensor_to_json_string(server_data.cpu_usage);
@@ -175,7 +173,7 @@ 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>"
@@ -203,28 +201,33 @@ create_response_file(const char *nurl,
                     unsigned int *rp_code,
                     const char *fpath)
 {
-       if (is_file(fpath)) {
-               FILE *file = fopen(fpath, "rb");
+       struct stat st;
+       int ret;
+       FILE *file;
 
-               if (file) {
-                       struct stat buf;
+       ret = stat(fpath, &st);
+
+       if (!ret && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
+               file = fopen(fpath, "rb");
 
-                       stat(fpath, &buf);
+               if (file) {
                        *rp_code = MHD_HTTP_OK;
 
-                       if (!buf.st_size) {
+                       if (!st.st_size) {
                                fclose(file);
                                return MHD_create_response_from_data
                                        (0, NULL, MHD_NO, MHD_NO);
                        }
 
                        return MHD_create_response_from_callback
-                               (buf.st_size,
+                               (st.st_size,
                                 32 * 1024,
                                 &file_reader,
                                 file,
                                 (MHD_ContentReaderFreeCallback)&fclose);
 
+               } else {
+                       log_printf(LOG_ERR, "Failed to open: %s\n", fpath);
                }
        }
 
@@ -287,8 +290,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);
 
@@ -310,6 +312,7 @@ int main(int argc, char *argv[])
        int port = DEFAULT_PORT;
        int optc;
        int cmdok = 1;
+       char *log_file;
 
        program_name = argv[0];
 
@@ -320,11 +323,12 @@ int main(int argc, char *argv[])
        textdomain(PACKAGE);
 #endif
 
-       server_data.www_dir = DEFAULT_WWW_DIR;
+       server_data.www_dir = NULL;
        server_data.psysinfo.interfaces = NULL;
+       log_file = NULL;
 
        while ((optc = getopt_long(argc, argv,
-                                  "vhp:w:d", long_options, NULL)) != -1) {
+                                  "vhp:w:d:", long_options, NULL)) != -1) {
                switch (optc) {
                case 'w':
                        if (optarg)
@@ -341,7 +345,14 @@ int main(int argc, char *argv[])
                        print_version();
                        exit(EXIT_SUCCESS);
                case 'd':
-                       debug = 1;
+                       log_level = atoi(optarg);
+                       log_printf(LOG_INFO,
+                                  _("Enables debug mode: %d"),
+                                  log_level);
+                       break;
+               case 'l':
+                       if (optarg)
+                               log_file = strdup(optarg);
                        break;
                default:
                        cmdok = 0;
@@ -349,15 +360,23 @@ int main(int argc, char *argv[])
                }
        }
 
+       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"),
                        program_name);
                exit(EXIT_FAILURE);
        }
 
+       log_open(log_file);
+
        psensor_init();
 
-       server_data.sensors = get_all_sensors(600);
+       server_data.sensors = get_all_sensors(0, 600);
 
 #ifdef HAVE_GTOP
        server_data.cpu_usage = create_cpu_usage_sensor(600);
@@ -388,6 +407,8 @@ int main(int argc, char *argv[])
 #endif
                psensor_list_update_measures(server_data.sensors);
 
+               psensor_log_measures(server_data.sensors);
+
                pthread_mutex_unlock(&mutex);
                sleep(5);
        }
@@ -407,5 +428,8 @@ int main(int argc, char *argv[])
        cpu_cleanup();
 #endif
 
+       if (log_file != DEFAULT_LOG_FILE)
+               free(log_file);
+
        return EXIT_SUCCESS;
 }