From 40f68c37206c95bb29c310b6f4337d45b0ea8ea0 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Sun, 15 Apr 2012 11:14:21 +0000 Subject: [PATCH] fixed --wdir option. added log level to -d option. added --log-file option. logged measures when log level is 3. --- NEWS | 6 +++++- po/Makefile.in | 2 +- src/lib/psensor.c | 12 ++++++++++++ src/lib/psensor.h | 2 ++ src/main.c | 14 +------------- src/server/description.txt | 2 +- src/server/server.c | 48 ++++++++++++++++++++++++++++++++++------------ 7 files changed, 58 insertions(+), 28 deletions(-) diff --git a/NEWS b/NEWS index e18028a..0792beb 100644 --- a/NEWS +++ b/NEWS @@ -16,9 +16,13 @@ ** psensor: fixed wrong background color, get the background color of the window and not the canvas widget. (Closes LP: #973122) ** psensor: used the foreground color defined by the theme for the - font of the graph. + font of the graph legend. ** psensor: used tab layout for psensor preferences . ** psensor: temperature unit choice (celcius/fahrenheit). +** psensor-server: fixed --wdir option. +** psensor-server: added log level to -d option. +** psensor-server: added --log-file option. +** psensor-server: logged measures when log level is 3. * v0.6.2.17 diff --git a/po/Makefile.in b/po/Makefile.in index 35752cc..136065a 100644 --- a/po/Makefile.in +++ b/po/Makefile.in @@ -21,7 +21,7 @@ srcdir = . top_srcdir = .. -prefix = /usr +prefix = /tmp/p exec_prefix = ${prefix} datarootdir = ${prefix}/share datadir = ${datarootdir} diff --git a/src/lib/psensor.c b/src/lib/psensor.c index 79095b9..ac6a5a5 100644 --- a/src/lib/psensor.c +++ b/src/lib/psensor.c @@ -485,6 +485,18 @@ void psensor_list_update_measures(struct psensor **sensors) #endif } +void psensor_log_measures(struct psensor **sensors) +{ + if (log_level == LOG_DEBUG) + while (*sensors) { + log_debug("Measure: %s %.2f", + (*sensors)->name, + psensor_get_current_value(*sensors)); + + sensors++; + } +} + void psensor_init() { lmsensor_init(); diff --git a/src/lib/psensor.h b/src/lib/psensor.h index cd44106..19d3a10 100644 --- a/src/lib/psensor.h +++ b/src/lib/psensor.h @@ -190,4 +190,6 @@ double get_max_value(struct psensor **sensors, int type); double celcius_to_fahrenheit(double c); +void psensor_log_measures(struct psensor **sensors); + #endif diff --git a/src/main.c b/src/main.c index 45e8140..95163e0 100644 --- a/src/main.c +++ b/src/main.c @@ -135,18 +135,6 @@ update_psensor_values_size(struct psensor **sensors, struct config *cfg) } } -static void log_measures(struct psensor **sensors) -{ - if (log_level == LOG_DEBUG) - while (*sensors) { - log_debug("Measure: %s %.2f", - (*sensors)->name, - psensor_get_current_value(*sensors)); - - sensors++; - } -} - void update_psensor_measures(struct ui_psensor *ui) { struct psensor **sensors = ui->sensors; @@ -171,7 +159,7 @@ void update_psensor_measures(struct ui_psensor *ui) amd_psensor_list_update(sensors); #endif - log_measures(sensors); + psensor_log_measures(sensors); g_mutex_unlock(ui->sensors_mutex); diff --git a/src/server/description.txt b/src/server/description.txt index 11e65fb..8c71a1c 100644 --- a/src/server/description.txt +++ b/src/server/description.txt @@ -47,7 +47,7 @@ Fields of the type 'sensor': The URL http://hostname:3131/api/1.0/sensors returns a JSON array containing all JSON objects of type 'sensor'. -If run in debug mode, psensor\-server can be stopped by sending an HTTP +psensor\-server can be stopped by sending an HTTP request with the URL 'http://hostname:port/api/1.0/server/stop'. [WARNING] diff --git a/src/server/server.c b/src/server/server.c index 0576d3c..32b3cfd 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -41,10 +41,13 @@ #include "cpu.h" #endif +#include "log.h" #include "psensor_json.h" #include "url.h" #include "server.h" +static const char *DEFAULT_LOG_FILE = "/var/log/psensor-server.log"; + static const char *program_name; #define DEFAULT_PORT 3131 @@ -57,7 +60,8 @@ 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'}, {0, 0, 0, 0} }; @@ -65,8 +69,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() @@ -93,13 +95,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(""); printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT); puts(""); printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL); @@ -168,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(_("

" @@ -285,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); @@ -308,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,9 +325,10 @@ int main(int argc, char *argv[]) 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) @@ -339,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; @@ -347,7 +360,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 +372,8 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + log_open(log_file); + psensor_init(); server_data.sensors = get_all_sensors(0, 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; } -- 2.7.4