[tuxdroid-svn] r296 - in daemon/trunk: . libs
Status: Beta
Brought to you by:
ks156
From: neimad <c2m...@c2...> - 2007-05-02 15:09:26
|
Author: neimad Date: 2007-05-02 17:08:18 +0200 (Wed, 02 May 2007) New Revision: 296 Added: daemon/trunk/libs/USBDaemon_log.c daemon/trunk/libs/USBDaemon_log.h Modified: daemon/trunk/Makefile daemon/trunk/TODO daemon/trunk/libs/USBDaemon_command_tux.c daemon/trunk/libs/USBDaemon_globals.h daemon/trunk/libs/USBDaemon_pidfile.c daemon/trunk/libs/USBDaemon_pidfile.h daemon/trunk/libs/USBDaemon_status_table.c daemon/trunk/libs/USBDaemon_tcp_server.c daemon/trunk/libs/USBDaemon_usb_enum.c daemon/trunk/libs/USBDaemon_usb_readWrite.c daemon/trunk/main.c Log: Logging facility for Tux daemon. It has 3 logging modes: - syslog mode, which is the default when running as a daemon (option -d), unless option -v ("view in shell") is given. Messages are logged to /var/log/messages. - tux mode, where the daemon logs to its own separate logfile, /var/log/tuxdaemon.log. - shell mode, which is the default when *not* running as a daemon, where messages are printed to std{out,err}. There are 5 logging levels (from lowest to highest priority): debug, info, warning, error, none The logging level can be configured with log_set_level(). A message is logged at level L by calling log_L(), i.e. one of log_debug(), log_info(), log_warning() and log_error() Logging a message at level 'none' has no sense, so there is no corresponding function. All messages are logged prefixed with their logging level, except for 'info' messages. If a message is logged at level L such that L < C, where C is the current logging level, then that message is silently dropped. Most notably, if the logging level is set to 'none', then *no* message will be logged at all. The following changes were made: * Defined TUX_DAEMON_NAME, name of the daemon, for use everywhere in place of hardcoded strings. * Added module libs/USBDaemon_log.[ch], which implements the logging facility. * Moved function terminate() from libs/USBDaemon_pidfile.[ch] to main.c, which makes more sense. It is now registered with atexit() and is hence automatically executed at exit. * All calls to terminate() have been replaced with calls to exit() (see above). * Replaced printf() with log_info() and fprintf(stderr) with log_error(). Some of these calls should probably be replaced with log_warning() or log_debug(). * All status changes (wings motor, leds, etc) are logged at level DEBUG. * Added -l option to set the logging mode: -lsys Log to syslog -ltux Log to /var/log/tuxdaemon.log -lstd Print to std{out,err} Option -v is still there for now (as an alias for -lstd) but is marked deprecated. * Added -debug option to set the logging level to DEBUG at start (default is INFO). * Added ability to activate/deactivate the DEBUG level dynamically by sending USR2 to the daemon. * Removed logging from the TODO list. Modified: daemon/trunk/Makefile =================================================================== --- daemon/trunk/Makefile 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/Makefile 2007-05-02 15:08:18 UTC (rev 296) @@ -43,9 +43,9 @@ $(OBJ_DIR)/USBDaemon_status_table.o \ $(OBJ_DIR)/USBDaemon_pidfile.o \ $(OBJ_DIR)/USBDaemon_command_tux.o \ - $(OBJ_DIR)/USBDaemon_tcp_server.o + $(OBJ_DIR)/USBDaemon_tcp_server.o \ + $(OBJ_DIR)/USBDaemon_log.o - define build_target @echo Linking... @$(CC) -o "$(OUTPUT_DIR)/$(TARGET)" $(SRC_OBJS) $(LIB_DIRS) $(LIBS) $(LDFLAGS) @@ -93,6 +93,9 @@ version.h $(compile_source) +$(OBJ_DIR)/USBDaemon_log.o: libs/USBDaemon_log.c libs/USBDaemon_log.h + $(compile_source) + $(OBJ_DIR)/USBDaemon_usb_enum.o: libs/USBDaemon_usb_enum.c \ libs/USBDaemon_usb_enum.h \ libs/USBDaemon_globals.h \ Modified: daemon/trunk/TODO =================================================================== --- daemon/trunk/TODO 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/TODO 2007-05-02 15:08:18 UTC (rev 296) @@ -1,7 +1,5 @@ $Id$ -- Add a decent logging functionality to the daemon. - - Use the GNU Autotools framework to configure and compile the daemon. - Support multiple dongles. Modified: daemon/trunk/libs/USBDaemon_command_tux.c =================================================================== --- daemon/trunk/libs/USBDaemon_command_tux.c 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/libs/USBDaemon_command_tux.c 2007-05-02 15:08:18 UTC (rev 296) @@ -28,6 +28,7 @@ #include "USBDaemon_command_tux.h" #include "USBDaemon_usb_readWrite.h" #include "USBDaemon_globals.h" +#include "USBDaemon_log.h" #include "../tuxdefs/commands.h" #include "USBDaemon_tcp_server.h" #include "USBDaemon_pidfile.h" @@ -261,10 +262,10 @@ pthread_mutex_unlock(&tcp_mutex); break; case SUB_D_CMD_STRUC_KILL_DAEMON: - printf("WARNING : daemon killed by user\n"); + log_warning("daemon killed by user"); send_daemon_disconnected(); result[0] = ACK_CMD_OK; - terminate(0); + exit(0); break; case SUB_D_CMD_STRUC_DEFINE_CLIENT_NAME: { @@ -273,8 +274,8 @@ for (i = 0; i < 11; i++) tcp_clients_name[id_client][i] = data[i + 1]; result[0] = ACK_CMD_OK; - printf("Name of client %d is %s\n", id_client, - tcp_clients_name[id_client]); + log_debug("Name of client %d is %s", id_client, + tcp_clients_name[id_client]); } break; } Modified: daemon/trunk/libs/USBDaemon_globals.h =================================================================== --- daemon/trunk/libs/USBDaemon_globals.h 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/libs/USBDaemon_globals.h 2007-05-02 15:08:18 UTC (rev 296) @@ -29,6 +29,10 @@ #include <pthread.h> /*_____________________ D E F I N E S ______________________________________*/ + +/** Name of the daemon */ +#define TUX_DAEMON_NAME "tuxdaemon" + #define TUX_PID 0xFF07 #define TUX_VID 0x03EB #define TUX_INTERFACE 0x03 Added: daemon/trunk/libs/USBDaemon_log.c =================================================================== --- daemon/trunk/libs/USBDaemon_log.c (rev 0) +++ daemon/trunk/libs/USBDaemon_log.c 2007-05-02 15:08:18 UTC (rev 296) @@ -0,0 +1,210 @@ + +#include "USBDaemon_globals.h" +#include "USBDaemon_log.h" + +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <assert.h> +#include <syslog.h> + +/** Name of log file for target LOG_TARGET_TUX */ +#define LOG_FILE "/var/log/" TUX_DAEMON_NAME ".log" + +/** All logged messages are prefixed with this text */ +#define LOG_PREFIX TUX_DAEMON_NAME + +/** Current logging level */ +static log_level_t current_level = LOG_LEVEL_INFO; + +/** Logging level names */ +static const char *level_names[] = +{ + [LOG_LEVEL_DEBUG] = "debug", + [LOG_LEVEL_INFO] = "info", + [LOG_LEVEL_WARNING] = "warning", + [LOG_LEVEL_ERROR] = "error", + [LOG_LEVEL_NONE] = "none" +}; + +/** Logging level to syslog priority mapping */ +static int level_prio[] = +{ + [LOG_LEVEL_DEBUG] = LOG_DEBUG, + [LOG_LEVEL_INFO] = LOG_INFO, + [LOG_LEVEL_WARNING] = LOG_WARNING, + [LOG_LEVEL_ERROR] = LOG_ERR, + [LOG_LEVEL_NONE] = 0 +}; + +/** Current logging target */ +static log_target_t log_target = LOG_TARGET_SHELL; + +/** Log file for target LOG_TARGET_TUX */ +static FILE *log_file; + +/** Whether the log has been opened */ +static int log_opened; + +/** + * @brief Open the log. + * + * @param[in] target Logging target + * + * @return 1 if successfull, 0 otherwise + */ +int log_open(log_target_t target) +{ + if (log_opened) + return 1; + + switch (target) + { + case LOG_TARGET_SYSLOG: + openlog(LOG_PREFIX, 0, LOG_DAEMON); + break; + + case LOG_TARGET_TUX: + log_file = fopen(LOG_FILE, "at"); + if (log_file == NULL) + return 0; + break; + + case LOG_TARGET_SHELL: + break; + } + + log_target = target; + log_opened = 1; + + return 1; +} + +/** + * @brief Close the log. + */ +void log_close(void) +{ + if (!log_opened) + return; + + switch (log_target) + { + case LOG_TARGET_SYSLOG: + closelog(); + break; + + case LOG_TARGET_TUX: + fclose(log_file); + log_file = NULL; + break; + + case LOG_TARGET_SHELL: + break; + } + + log_opened = 0; +} + +/** + * @brief Set the logging level. + * + * @param[in] new_level New logging level + */ +void log_set_level(log_level_t new_level) +{ + assert(new_level >= LOG_LEVEL_DEBUG && new_level <= LOG_LEVEL_NONE); + current_level = new_level; +} + +/** + * @brief Get the logging level. + * + * @return current logging level + */ +log_level_t log_get_level(void) +{ + return current_level; +} + +/** + * @brief Log formatted message at the specified level. + * + * @param[in] at_level Level to log the message at + * @param[in] fmt Message format + * @param[in] ... Optional message data + * + * If the priority of the specifed level is lower than the priority + * of the current logging level, the message is silently dropped. + * + * @return 1 if successful, 0 otherwise + */ +int log_text(log_level_t at_level, const char *fmt, ...) +{ + char text[1024], *p = text; + size_t size = sizeof(text); + int prio; + time_t now; + va_list al; + int r; + + /* No need for the log to be 'opened' when logging to std{out,err} */ + if (log_target != LOG_TARGET_SHELL && !log_opened) + return 0; + + /* Logging at level NONE has no sense */ + assert(at_level >= LOG_LEVEL_DEBUG && at_level < LOG_LEVEL_NONE); + + if (at_level < current_level) + return 1; + + /* Add date & time when LOG_TARGET_TUX */ + if (log_target == LOG_TARGET_TUX) + { + now = time(NULL); + r = strftime(p, size, "%F %T ", localtime(&now)); + if (r == 0) + return 0; + + p += r; + size -= r; + } + + /* Only prefix non-INFO level messages */ + if (at_level != LOG_LEVEL_INFO) + { + r = snprintf(p, size, "%s: ", level_names[at_level]); + if (r < 0) + return 0; + + p += r; + size -= r; + } + + va_start(al, fmt); + r = vsnprintf(p, size, fmt, al); + va_end(al); + if (r < 0) + return 0; + + switch (log_target) + { + case LOG_TARGET_SYSLOG: + prio = level_prio[at_level]; + syslog(prio, "%s", text); + break; + + case LOG_TARGET_TUX: + fprintf(log_file, "%s\n", text); + break; + + case LOG_TARGET_SHELL: + if (at_level == LOG_LEVEL_WARNING || at_level == LOG_LEVEL_ERROR) + fprintf(stderr, "%s\n", text); + else + fprintf(stdout, "%s\n", text); + break; + } + + return 1; +} Added: daemon/trunk/libs/USBDaemon_log.h =================================================================== --- daemon/trunk/libs/USBDaemon_log.h (rev 0) +++ daemon/trunk/libs/USBDaemon_log.h 2007-05-02 15:08:18 UTC (rev 296) @@ -0,0 +1,36 @@ +#ifndef __USBDAEMON_LOG_H__ +#define __USBDAEMON_LOG_H__ + +/** Logging target */ +typedef enum log_target +{ + LOG_TARGET_SYSLOG, + LOG_TARGET_TUX, + LOG_TARGET_SHELL +} log_target_t; + +extern int log_open(log_target_t target); +extern void log_close(void); + +/** Logging levels, in increasing priorities */ +typedef enum log_level +{ + LOG_LEVEL_DEBUG, + LOG_LEVEL_INFO, + LOG_LEVEL_WARNING, + LOG_LEVEL_ERROR, + LOG_LEVEL_NONE +} log_level_t; + +extern void log_set_level(log_level_t new_level); +extern log_level_t log_get_level(void); + +extern int log_text(log_level_t at_level, const char *fmt, ...) + __attribute__((format(printf, 2, 3))); + +#define log_debug(fmt, ...) log_text(LOG_LEVEL_DEBUG, (fmt), ## __VA_ARGS__) +#define log_info(fmt, ...) log_text(LOG_LEVEL_INFO, (fmt), ## __VA_ARGS__) +#define log_warning(fmt, ...) log_text(LOG_LEVEL_WARNING, (fmt), ## __VA_ARGS__) +#define log_error(fmt, ...) log_text(LOG_LEVEL_WARNING, (fmt), ## __VA_ARGS__) + +#endif Modified: daemon/trunk/libs/USBDaemon_pidfile.c =================================================================== --- daemon/trunk/libs/USBDaemon_pidfile.c 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/libs/USBDaemon_pidfile.c 2007-05-02 15:08:18 UTC (rev 296) @@ -29,7 +29,9 @@ #include <string.h> #include <errno.h> #include <signal.h> + #include "USBDaemon_globals.h" +#include "USBDaemon_log.h" #define PIDFILE "/var/run/tuxdaemon.pid" @@ -94,7 +96,7 @@ if ((fd = open(PIDFILE, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1 || (f = fdopen(fd, "r+")) == NULL) { - fprintf(stderr, "Can't open or create %s: %m\n", PIDFILE); + log_error("Can't open or create %s: %m", PIDFILE); return 0; } @@ -102,14 +104,14 @@ { fscanf(f, "%d", &pid); fclose(f); - fprintf(stderr, "Can't lock, lock is held by pid %d.\n", pid); + log_error("Can't lock, lock is held by pid %d", pid); return 0; } pid = getpid(); if (!fprintf(f, "%d\n", pid)) { - fprintf(stderr, "Can't write pid: %m.\n"); + log_error("Can't write pid: %m"); close(fd); return 0; } @@ -117,7 +119,7 @@ if (flock(fd, LOCK_UN) == -1) { - fprintf(stderr, "Can't unlock pidfile %s: %m.\n", PIDFILE); + log_error("Can't unlock pidfile %s: %m", PIDFILE); close(fd); return 0; } @@ -137,16 +139,3 @@ { return unlink(PIDFILE); } - -/************************************************************************ */ - -/* Terminate */ - -/************************************************************************ */ -void terminate(int ret) -{ - if (remove_pid()) - fprintf(stderr, "Could not delete PID file\n"); - - exit(ret); -} Modified: daemon/trunk/libs/USBDaemon_pidfile.h =================================================================== --- daemon/trunk/libs/USBDaemon_pidfile.h 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/libs/USBDaemon_pidfile.h 2007-05-02 15:08:18 UTC (rev 296) @@ -62,10 +62,4 @@ */ extern int remove_pid(void); -/* terminate - * - * Remove the pid file and exit - */ -extern void terminate(int ret); - #endif Modified: daemon/trunk/libs/USBDaemon_status_table.c =================================================================== --- daemon/trunk/libs/USBDaemon_status_table.c 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/libs/USBDaemon_status_table.c 2007-05-02 15:08:18 UTC (rev 296) @@ -25,6 +25,7 @@ #include <stdio.h> #include "USBDaemon_status_table.h" #include "USBDaemon_globals.h" +#include "USBDaemon_log.h" #include "USBDaemon_tcp_server.h" #include "../tuxdefs/commands.h" @@ -120,8 +121,8 @@ default: if (show_invalid_raw) - printf("%.2x %.2x %.2x %.2x\n", new_status[0], new_status[1], - new_status[2], new_status[3]); + log_debug("%.2x %.2x %.2x %.2x", new_status[0], new_status[1], + new_status[2], new_status[3]); break; } } @@ -266,7 +267,7 @@ if (new_code) { - printf("Remote code : %.2x\n", code); + log_debug("Remote code : %.2x", code); for (i = 0; i < sizeof(tcp_frame); i++) tcp_frame[i] = '\0'; @@ -330,12 +331,12 @@ tcp_frame[4] = DATA_STATUS_WINGS_MOTOR_BACKWARD; if (portb.bits.PB0) { - printf("wings motor backward 0\n"); + log_debug("wings motor backward 0"); tcp_frame[5] = 0; } else { - printf("wings motor backward 1\n"); + log_debug("wings motor backward 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -347,12 +348,12 @@ tcp_frame[4] = DATA_STATUS_SPIN_MOTOR_BACKWARD; if (portb.bits.PB1) { - printf("spin motor backward 0\n"); + log_debug("spin motor backward 0"); tcp_frame[5] = 0; } else { - printf("spin motor backward 1\n"); + log_debug("spin motor backward 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -364,12 +365,12 @@ tcp_frame[4] = DATA_STATUS_SPIN_MOTOR_FORWARD; if (portb.bits.PB2) { - printf("spin motor forward 0\n"); + log_debug("spin motor forward 0"); tcp_frame[5] = 0; } else { - printf("spin motor forward 1\n"); + log_debug("spin motor forward 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -382,13 +383,13 @@ if (portb.bits.PB3) { /* Up */ - printf("mouth open position 0\n"); + log_debug("mouth open position 0"); tcp_frame[5] = 0; } else { /* Down */ - printf("mouth open position 1\n"); + log_debug("mouth open position 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -400,12 +401,12 @@ tcp_frame[4] = DATA_STATUS_MOUTH_CLOSED_POSITION; if (portb.bits.PB4) { - printf("mouth close position 0\n"); + log_debug("mouth close position 0"); tcp_frame[5] = 0; } else { - printf("mouth close position 1\n"); + log_debug("mouth close position 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -417,12 +418,12 @@ tcp_frame[4] = DATA_STATUS_CHARGER_INHIBIT_SIGNAL; if (portb.bits.PB6) { - printf("charger inhibit signal 0\n"); + log_debug("charger inhibit signal 0"); tcp_frame[5] = 0; } else { - printf("charger inhibit signal 1\n"); + log_debug("charger inhibit signal 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -455,12 +456,12 @@ tcp_frame[4] = DATA_STATUS_WINGS_POSITION_SWITCH; if (portc.bits.PB1) { - printf("wings position switch 0\n"); + log_debug("wings position switch 0"); tcp_frame[5] = 0; } else { - printf("wings position switch 1\n"); + log_debug("wings position switch 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -472,12 +473,12 @@ tcp_frame[4] = DATA_STATUS_RIGHT_BLUE_LED; if (portc.bits.PB2) { - printf("right blue led off\n"); + log_debug("right blue led off"); tcp_frame[5] = 0; } else { - printf("right blue led on\n"); + log_debug("right blue led on"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -489,12 +490,12 @@ tcp_frame[4] = DATA_STATUS_LEFT_BLUE_LED; if (portc.bits.PB3) { - printf("left blue led off\n"); + log_debug("left blue led off"); tcp_frame[5] = 0; } else { - printf("left blue led on\n"); + log_debug("left blue led on"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -527,12 +528,12 @@ tcp_frame[4] = DATA_STATUS_HEAD_MOTOR_FOR_EYES; if (portd.bits.PB0) { - printf("head motor for eyes 0\n"); + log_debug("head motor for eyes 0"); tcp_frame[5] = 0; } else { - printf("head motor for eyes 1\n"); + log_debug("head motor for eyes 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -544,12 +545,12 @@ tcp_frame[4] = DATA_STATUS_HEAD_MOTOR_FOR_MOUTH; if (portd.bits.PB1) { - printf("head motor for mouth 0\n"); + log_debug("head motor for mouth 0"); tcp_frame[5] = 0; } else { - printf("head motor for mouth 1\n"); + log_debug("head motor for mouth 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -561,12 +562,12 @@ tcp_frame[4] = DATA_STATUS_SPIN_POSITION_SWITCH; if (portd.bits.PB3) { - printf("spin position switch 0\n"); + log_debug("spin position switch 0"); tcp_frame[5] = 0; } else { - printf("spin position switch 1\n"); + log_debug("spin position switch 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -578,12 +579,12 @@ tcp_frame[4] = DATA_STATUS_WINGS_MOTOR_FORWARD; if (portd.bits.PB4) { - printf("wings motor forward 0\n"); + log_debug("wings motor forward 0"); tcp_frame[5] = 0; } else { - printf("wings motor forward 1\n"); + log_debug("wings motor forward 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -595,12 +596,12 @@ tcp_frame[4] = DATA_STATUS_IR_LED; if (portd.bits.PB5) { - printf("IR led 0\n"); + log_debug("IR led 0"); tcp_frame[5] = 0; } else { - printf("IR led 1\n"); + log_debug("IR led 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -612,12 +613,12 @@ tcp_frame[4] = DATA_STATUS_EYES_OPEN_POSITION_SWITCH; if (portd.bits.PB6) { - printf("eyes open position switch 0\n"); + log_debug("eyes open position switch 0"); tcp_frame[5] = 0; } else { - printf("eyes open position switch 1\n"); + log_debug("eyes open position switch 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -629,12 +630,12 @@ tcp_frame[4] = DATA_STATUS_EYES_CLOSED_POSITION_SWITCH; if (portd.bits.PB7) { - printf("eyes closed position switch 0\n"); + log_debug("eyes closed position switch 0"); tcp_frame[5] = 0; } else { - printf("eyes closed position switch 1\n"); + log_debug("eyes closed position switch 1"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -667,12 +668,12 @@ tcp_frame[4] = DATA_STATUS_LEFT_WING_PUSH; if (sensors1.bits.PB0) { - printf("Left wing button Up\n"); + log_debug("Left wing button Up"); tcp_frame[5] = 0; } else { - printf("Left wing button Down\n"); + log_debug("Left wing button Down"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -684,12 +685,12 @@ tcp_frame[4] = DATA_STATUS_RIGHT_WING_PUSH; if (sensors1.bits.PB1) { - printf("Right wing button Up\n"); + log_debug("Right wing button Up"); tcp_frame[5] = 0; } else { - printf("Right wing button Down\n"); + log_debug("Right wing button Down"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -701,12 +702,12 @@ tcp_frame[4] = DATA_STATUS_POWER_PLUG_SWITCH; if (sensors1.bits.PB2) { - printf("power plug insertion switch on\n"); + log_debug("power plug insertion switch on"); tcp_frame[5] = 0; } else { - printf("power plug insertion switch off\n"); + log_debug("power plug insertion switch off"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -718,12 +719,12 @@ tcp_frame[4] = DATA_STATUS_HEAD_PUSH_SWITCH; if (sensors1.bits.PB3) { - printf("Head button Up\n"); + log_debug("Head button Up"); tcp_frame[5] = 0; } else { - printf("Head button Down\n"); + log_debug("Head button Down"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); @@ -735,12 +736,12 @@ tcp_frame[4] = DATA_STATUS_CHARGER_LED_STATUS; if (sensors1.bits.PB4) { - printf("Charger led 1\n"); + log_debug("Charger led 1"); tcp_frame[5] = 1; } else { - printf("Charger led 0\n"); + log_debug("Charger led 0"); tcp_frame[5] = 0; } tcp_server_send_raw(tcp_frame); @@ -752,12 +753,12 @@ tcp_frame[4] = DATA_STATUS_MUTE_STATUS; if (sensors1.bits.PB7) { - printf("mute status off\n"); + log_debug("mute status off"); tcp_frame[5] = 0; } else { - printf("mute status on\n"); + log_debug("mute status on"); tcp_frame[5] = 1; } tcp_server_send_raw(tcp_frame); Modified: daemon/trunk/libs/USBDaemon_tcp_server.c =================================================================== --- daemon/trunk/libs/USBDaemon_tcp_server.c 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/libs/USBDaemon_tcp_server.c 2007-05-02 15:08:18 UTC (rev 296) @@ -34,6 +34,7 @@ #include "USBDaemon_status_table.h" #include "USBDaemon_tcp_server.h" #include "USBDaemon_pidfile.h" +#include "USBDaemon_log.h" /*_____________________ V A R I A B L E S __________________________________*/ @@ -63,7 +64,7 @@ if ((tcp_server_handle = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - printf("TCP socket : server error\n"); + log_error("TCP socket: socket creation failed: %m"); return NULL; } @@ -72,7 +73,7 @@ (tcp_server_handle, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) { /* Don't need to quit on this one. */ - printf("TCP socket : setsockopt error\n"); + log_error("TCP socket: setsockopt failed: %m"); } bzero(&tcp_server_sockaddr, sizeof(tcp_server_sockaddr)); @@ -83,15 +84,16 @@ (tcp_server_handle, (struct sockaddr *)&tcp_server_sockaddr, sizeof(tcp_server_sockaddr)) < 0) { - printf("TCP socket : binding error\n"); - terminate(2); + log_error("TCP socket: bind failed: %m"); + exit(2); } if (listen(tcp_server_handle, 5) < 0) { - printf("TCP socket : listening error\n"); + log_error("TCP socket: listen failed: %m"); return NULL; } - printf("TCP socket : opened\n"); + + log_info("TCP socket: opened"); tux_tcp_status = TUX_TCP_STATUS_UP; while (tcp_server_handle >= 0) @@ -130,7 +132,7 @@ accept(tcp_server_handle, (struct sockaddr *)NULL, NULL); if (tmp_client < 0) - fprintf(stderr, "accept failed: %m\n"); + log_error("accept failed: %m"); else { int client_added = 0; @@ -145,10 +147,10 @@ } if (client_added) - printf("client %d connected\n", i); + log_info("client %d connected", i); else { - fprintf(stderr, "client rejected: client table full\n"); + log_error("client rejected: client table full"); close(tmp_client); } } Modified: daemon/trunk/libs/USBDaemon_usb_enum.c =================================================================== --- daemon/trunk/libs/USBDaemon_usb_enum.c 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/libs/USBDaemon_usb_enum.c 2007-05-02 15:08:18 UTC (rev 296) @@ -26,6 +26,7 @@ #include <errno.h> #include "USBDaemon_usb_enum.h" #include "USBDaemon_globals.h" +#include "USBDaemon_log.h" #include "USBDaemon_status_table.h" #include "USBDaemon_usb_readWrite.h" #include "USBDaemon_command_tux.h" @@ -84,7 +85,7 @@ setuid(NEWUID); if (!tux_hdl) { - printf("Can't open device: %s (%d)\n", strerror(errno), errno); + log_error("Can't open device: %s (%d)", strerror(errno), errno); return NULL; } /* Claim device interface */ @@ -95,7 +96,7 @@ error = usb_claim_interface(tux_hdl, TUX_INTERFACE); if (error != 0) { - printf("Can't claim interface: %s (%d)\n", strerror(errno), errno); + log_error("Can't claim interface: %s (%d)", strerror(errno), errno); return NULL; } } @@ -116,7 +117,7 @@ tux_device = usb_find_TuxDroid(); if (tux_device == NULL) { - printf("Tux dongle not found\n"); + log_error("Tux dongle not found"); } sleep(1); } @@ -125,11 +126,11 @@ /* Old firmware should be discarded here */ if (tux_device->descriptor.bcdDevice < 0x100) { - printf("\nERROR: Your dongle firmware is too old.\n" + log_error("Your dongle firmware is too old.\n" "This version is not compatible with this daemon, please update\n" "the firmware to version 1.00 or better.\n" "Check http://www.tuxisalive.com/documentation/how-to/updating-the-firmware\n" - "for details.\n"); + "for details."); return (0); } @@ -137,7 +138,7 @@ tux_handle = usb_open_TuxDroid(tux_device); if (tux_handle == NULL) { - printf("You must load the daemon in root mode\n"); + log_error("You must load the daemon in root mode"); return (0); } return (1); @@ -167,9 +168,9 @@ if (dongle_is_present_counter == 0) if (usb_find_TuxDroid() == NULL) { - printf("Tux dongle is disconnected\n"); + log_error("Tux dongle is disconnected"); send_daemon_disconnected(); - terminate(1); + exit(1); } usb_get_status_TuxDroid(); usleep(50000); Modified: daemon/trunk/libs/USBDaemon_usb_readWrite.c =================================================================== --- daemon/trunk/libs/USBDaemon_usb_readWrite.c 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/libs/USBDaemon_usb_readWrite.c 2007-05-02 15:08:18 UTC (rev 296) @@ -25,6 +25,7 @@ #include <stdio.h> #include "USBDaemon_usb_enum.h" #include "USBDaemon_globals.h" +#include "USBDaemon_log.h" #include "USBDaemon_status_table.h" #include "USBDaemon_tcp_server.h" #include "USBDaemon_usb_readWrite.h" @@ -71,7 +72,7 @@ unsigned char cmd_send[5]; unsigned char cmd_dispatch[5]; int idx; - unsigned char i, j; + unsigned char i; for (i = 0; i < 64; i++) data[i] = 0; @@ -115,8 +116,8 @@ update_system_status_table(cmd_dispatch); if (show_frames) - printf("RF_st: %.2x CMD_st: %.2x NB_frames : %d idx : %d\n", RF_status, - CMD_status, NUMBER_frames, idx); + log_debug("RF_st: %.2x CMD_st: %.2x NB_frames : %d idx : %d", + RF_status, CMD_status, NUMBER_frames, idx); if (NUMBER_frames > 0 && NUMBER_frames < 16) { @@ -129,11 +130,9 @@ cmd_dispatch[4] = 0; if (show_raw_status) - { - for (j = 0; j < 5; j++) - printf("%.2x ", cmd_dispatch[j]); - printf("\n"); - } + log_debug("%.2x %.2x %.2x %.2x %.2x", cmd_dispatch[0], + cmd_dispatch[1], cmd_dispatch[2], cmd_dispatch[3], + cmd_dispatch[4]); update_raw_status_table(cmd_dispatch); } Modified: daemon/trunk/main.c =================================================================== --- daemon/trunk/main.c 2007-04-28 09:26:30 UTC (rev 295) +++ daemon/trunk/main.c 2007-05-02 15:08:18 UTC (rev 296) @@ -36,6 +36,7 @@ #include <signal.h> #include <sys/file.h> #include "libs/USBDaemon_globals.h" +#include "libs/USBDaemon_log.h" #include "libs/USBDaemon_pidfile.h" #include "libs/USBDaemon_usb_enum.h" #include "libs/USBDaemon_tcp_server.h" @@ -99,18 +100,6 @@ /************************************************************************ */ -/* On_close_daemon */ - -/************************************************************************ */ -static void on_close_daemon(int sig) -{ - send_daemon_disconnected(); - printf("Daemon closed\n"); - terminate(0); -} - -/************************************************************************ */ - /* Clean closing of STD IN-OUT FILENO*/ /************************************************************************ */ @@ -134,12 +123,57 @@ return fd2; } +/** + * \brief Called when exiting. Performs cleanup. + */ +static void terminate(void) +{ + send_daemon_disconnected(); + + if (remove_pid()) + log_error("Could not delete PID file"); + + log_info("terminated"); + log_close(); +} + +/** + * \brief Toggle debugging mode on/off. + * + * \param[in] sig Signal (unused) + */ +static void toggle_debugging(int sig) +{ + static int first_call = 1; + static log_level_t saved_level; + int debugging; + + if (first_call) + { + debugging = (log_get_level() == LOG_LEVEL_DEBUG); + first_call = 0; + } + + debugging = !debugging; + if (debugging) + { + saved_level = log_get_level(); + log_set_level(LOG_LEVEL_DEBUG); + log_debug("debugging mode activated"); + } + else + { + log_debug("debugging mode deactivated"); + log_set_level(saved_level); + } +} + /************************************************************************ */ /* Daemonizer */ /************************************************************************ */ -static void daemonize(unsigned char shell_view) +static void daemonize(log_target_t log_target, log_level_t log_level) { pid_t pid, sid; @@ -149,33 +183,44 @@ case 0: break; case -1: /*erreur */ - terminate(1); + exit(1); break; default: exit(0); } + atexit(terminate); + signal(SIGUSR2, toggle_debugging); + + if (!log_open(log_target)) + { + log_error("failed opening log file"); + exit(1); + } + + log_set_level(log_level); + umask(0); sid = setsid(); if (sid < 0) { - perror("sid"); - terminate(1); + log_error("setsid failed: %m"); + exit(1); } if (chdir("/") < 0) { - perror("chdir"); - terminate(1); + log_error("chdir failed: %m"); + exit(1); } daemon_pid = (unsigned short)getpid(); - printf("Tux USB Daemon started : pid = %d\n", (int)getpid()); + log_info("Tux USB Daemon started: pid = %d", (int)getpid()); if (!write_pid()) exit(1); /* No shell */ - if (!shell_view) + if (log_target != LOG_TARGET_SHELL) { fd_nullify(STDIN_FILENO); fd_nullify(STDOUT_FILENO); @@ -185,6 +230,16 @@ /************************************************************************ */ +/* On_close_daemon */ + +/************************************************************************ */ +static void on_close_daemon(int sig) +{ + exit(0); +} + +/************************************************************************ */ + /* Show usages of tux daemon */ /************************************************************************ */ @@ -192,12 +247,16 @@ { printf("usage : tuxdaemon [options]\n"); printf("options:\n"); - printf(" -d start in daemon mode\n"); - printf(" -v view in shell ( for daemon mode )\n"); - printf(" -st show the structure of the usb frames\n"); - printf(" -sr show all raw status\n"); - printf(" -si show only invalid raw status\n"); - printf(" -h show this usage list\n"); + printf(" -d start in daemon mode\n"); + printf(" -lsys log to syslog\n"); + printf(" -ltux log to /var/log/tuxdaemon\n"); + printf(" -lstd log to stdout & stderr\n"); + printf(" -v view in shell (deprecated, use -lstd instead)\n"); + printf(" -debug start in debug mode\n"); + printf(" -st show the structure of the usb frames\n"); + printf(" -sr show all raw status\n"); + printf(" -si show only invalid raw status\n"); + printf(" -h show this usage list\n"); } /************************************************************************ */ @@ -212,7 +271,8 @@ GThread *TcpThread; GError *error = NULL; unsigned char i; - unsigned char shell_view = 0; + log_target_t log_target = LOG_TARGET_SYSLOG; + log_level_t log_level = LOG_LEVEL_INFO; unsigned char daemonized = 0; /* On exiting */ @@ -227,10 +287,16 @@ /* Program arguments */ for (i = 1; i < argc; i++) { - if (!strcmp("-v", argv[i])) - shell_view = 1; - else if (!strcmp("-d", argv[i])) + if (!strcmp("-d", argv[i])) daemonized = 1; + else if (!strcmp("-lsys", argv[i])) + log_target = LOG_TARGET_SYSLOG; + else if (!strcmp("-ltux", argv[i])) + log_target = LOG_TARGET_TUX; + else if (!strcmp("-lstd", argv[i]) || !strcmp("-v", argv[i])) + log_target = LOG_TARGET_SHELL; + else if (!strcmp("-debug", argv[i])) + log_level = LOG_LEVEL_DEBUG; else if (!strcmp("-st", argv[i])) show_frames = 1; else if (!strcmp("-sr", argv[i])) @@ -250,9 +316,14 @@ } if (daemonized) - daemonize(shell_view); - else if (!write_pid()) - exit(1); + daemonize(log_target, log_level); + else + { + log_open(LOG_TARGET_SHELL); + log_set_level(log_level); + if (!write_pid()) + exit(1); + } /* Setup & print daemon information */ setup_daemon_info(); @@ -267,7 +338,7 @@ /* open USB handler and setuid nobody */ if (!usb_init_TuxDroid()) { - terminate(1); + exit(1); } /* Gestion Multithread */ if (!g_thread_supported()) @@ -276,16 +347,16 @@ } else { - printf("g_thread NOT supported\n"); - terminate(1); + log_error("g_thread NOT supported"); + exit(1); } /* Start USB task thread */ UsbThread = g_thread_create((GThreadFunc) usb_start_task, NULL, TRUE, &error); if (UsbThread == NULL) { - printf("Thread creation failed: %s \n", error->message); - terminate(2); + log_error("Thread creation failed: %s", error->message); + exit(2); } /* Start TCP task thread */ @@ -294,13 +365,12 @@ &error); if (TcpThread == NULL) { - printf("Thread creation failed: %s \n", error->message); - terminate(2); + log_error("Thread creation failed: %s", error->message); + exit(2); } loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(loop); - terminate(0); - return (0); + exit(0); } |