Author: Paul_R
Date: 2008-09-17 12:24:55 +0200 (Wed, 17 Sep 2008)
New Revision: 1866

Added:
   firmware/tuxup/trunk/log.c
   firmware/tuxup/trunk/log.h
Modified:
   firmware/tuxup/trunk/Makefile
   firmware/tuxup/trunk/bootloader.c
   firmware/tuxup/trunk/error.h
   firmware/tuxup/trunk/main.c
   firmware/tuxup/trunk/usb-connection.c
Log:
* Added the log module with the quiet and debug modes.
  (from prod version at rev 896)


Modified: firmware/tuxup/trunk/Makefile
===================================================================
--- firmware/tuxup/trunk/Makefile       2008-09-17 09:13:05 UTC (rev 1865)
+++ firmware/tuxup/trunk/Makefile       2008-09-17 10:24:55 UTC (rev 1866)
@@ -20,7 +20,7 @@
 ## Libraries
 
 ## Objects that must be built in order to link
-FILES = main.c usb-connection.c bootloader.c tux_hid_win32.c tux_misc.c
+FILES = main.c usb-connection.c bootloader.c tux_hid_win32.c tux_misc.c log.c
 
 ## Compile and link
 ifdef windir
@@ -70,9 +70,11 @@
  
 else
 all: $(TARGET)
-tuxup: main.c bootloader.c bootloader.h usb-connection.c usb-connection.h 
tux_hid_unix.c tux_hid_unix.h tux_misc.c tux_misc.h tux-api.h version.h 
common/commands.h
+tuxup: main.c bootloader.c bootloader.h usb-connection.c usb-connection.h \
+    tux_hid_unix.c tux_hid_unix.h tux_misc.c tux_misc.h tux-api.h version.h \
+    common/commands.h log.c log.h
        ${CC} ${LIBS} ${CFLAGS} ${C_INCLUDE_DIRS} ${DEFS} -o tuxup \
-           main.c bootloader.c usb-connection.c tux_hid_unix.c tux_misc.c
+           main.c bootloader.c usb-connection.c tux_hid_unix.c tux_misc.c log.c
 endif
 clean :
        -rm -f $(TARGET) *.o

Modified: firmware/tuxup/trunk/bootloader.c
===================================================================
--- firmware/tuxup/trunk/bootloader.c   2008-09-17 09:13:05 UTC (rev 1865)
+++ firmware/tuxup/trunk/bootloader.c   2008-09-17 10:24:55 UTC (rev 1866)
@@ -42,9 +42,8 @@
 #endif
 #include "tux-api.h"
 #include "error.h"
+#include "log.h"
 
-/* Whether to display verbose messages. */
-extern int verbose;
 
 bool HID;
 static float line_nb;
@@ -411,8 +410,7 @@
         }
         else
         {
-            fprintf(stderr,
-                    "Bootloading failed, program aborted at dongle reply.\n");
+            log_error("Bootloading failed, program aborted at dongle 
reply.\n");
             exit(E_TUXUP_BOOTLOADINGFAILED);
         }
     }
@@ -430,8 +428,7 @@
         }
         else
         {
-            fprintf(stderr,
-                    "\nBootloading failed, program aborted at dongle 
reply.\n");
+            log_error("\nBootloading failed, program aborted at dongle 
reply.\n");
             exit(E_TUXUP_BOOTLOADINGFAILED);
         }
     }
@@ -663,13 +660,13 @@
 
     if ((parser.segmentData = malloc(parser.segLen + 2)) == NULL)
     {
-        fprintf(stderr, "Unable to allocate segmentData space");
+        log_error("Unable to allocate segmentData space");
         goto cleanup;
     }
 
     if ((fs = fopen(fileName, "rt")) == NULL)
     {
-        fprintf(stderr, "Unable to open file '%s' for reading", fileName);
+        log_error("Unable to open file '%s' for reading", fileName);
         goto cleanup;
     }
 
@@ -764,7 +761,7 @@
     tux_hid_read(5, data_buffer);
     if (data_buffer[2] != BOOT_INIT_ACK)
     {
-        fprintf(stderr, "\nInitialisazation failed \n");
+        log_error("\nInitialisazation failed \n");
     }
     
 #else
@@ -772,7 +769,7 @@
     {
         if (!wait_status(BOOT_INIT_ACK, USB_TIMEOUT) || !ret)
         {
-            fprintf(stderr, "\nInitialization failed\n");
+            log_error("\nInitialization failed\n");
             return FALSE;
         }
     }
@@ -783,7 +780,7 @@
 #endif
         if ((ret != 64) && data_buffer[2] != BOOT_INIT_ACK)
         {
-            fprintf(stderr, "\nInitialization failed\n");
+            log_error("\nInitialization failed\n");
             return FALSE;
         }
     }
@@ -821,7 +818,7 @@
         if (!wait_status(BOOT_EXIT_ACK, USB_TIMEOUT))
 #endif
         {
-            fprintf(stderr, "\nBootloader exit failed \n");
+            log_error("\nBootloader exit failed \n");
             return FALSE;
         }
     }

Modified: firmware/tuxup/trunk/error.h
===================================================================
--- firmware/tuxup/trunk/error.h        2008-09-17 09:13:05 UTC (rev 1865)
+++ firmware/tuxup/trunk/error.h        2008-09-17 10:24:55 UTC (rev 1866)
@@ -23,16 +23,16 @@
 
 typedef enum
 {
-    E_TUXUP_NOERROR                 = 0,
-    E_TUXUP_BADPARAMETER            = 2,
-    E_TUXUP_DONGLENOTFOUND          = 3,
-    E_TUXUP_DONGLEMANUALBOOTLOAD    = 4,
-    E_TUXUP_BADPROGFILE             = 5,
-    E_TUXUP_BOOTLOADINGFAILED       = 6,
-    E_TUXUP_USBERROR                = 7,
-    E_TUXUP_DFUPROGNOTFOUND         = 8,
-    E_TUXUP_PROGRAMMINGFAILED       = 9,
-    E_FUXUSB_VER_ERROR              =10 
+    E_TUXUP_NOERROR = 0,
+    E_TUXUP_USAGE = 2,
+    E_TUXUP_DONGLENOTFOUND = 3,
+    E_TUXUP_DONGLEMANUALBOOTLOAD = 4,
+    E_TUXUP_BADPROGFILE = 5,
+    E_TUXUP_BOOTLOADINGFAILED = 6,
+    E_TUXUP_USBERROR = 7,
+    E_TUXUP_DFUPROGNOTFOUND = 8,
+    E_TUXUP_PROGRAMMINGFAILED = 9,
+    E_FUXUSB_VER_ERROR = 10 
 } tuxup_error_t;
 
 #endif /* _ERROR_ */

Added: firmware/tuxup/trunk/log.c
===================================================================
--- firmware/tuxup/trunk/log.c                          (rev 0)
+++ firmware/tuxup/trunk/log.c  2008-09-17 10:24:55 UTC (rev 1866)
@@ -0,0 +1,230 @@
+/*
+ * Tux Droid - USB Daemon
+ * Copyright (C) 2007 C2ME S.A. <[EMAIL PROTECTED]>
+ *
+ * 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, 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., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+/* $Id$ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <time.h>
+#include <assert.h>
+#include <syslog.h>
+
+#include "log.h"
+
+/** Name of log file for target LOG_TARGET_TUX */
+#define LOG_FILE  "/var/log/" LOG_FILE_NAME ".log"
+
+/** Current logging level */
+static log_level_t current_level = LOG_LEVEL_NOTICE;
+
+/** Logging level names */
+static const char *level_names[] =
+{
+    [LOG_LEVEL_DEBUG]   = "debug",
+    [LOG_LEVEL_INFO]    = "info",
+    [LOG_LEVEL_NOTICE]  = "notice",
+    [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_NOTICE]  = LOG_NOTICE,
+    [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 bool log_opened;
+
+/**
+ * Open the log.
+ *
+ * /param[in] target  Logging target
+ *
+ * /return true if successfull, false otherwise
+ */
+bool log_open(log_target_t target)
+{
+    if (log_opened)
+        return true;
+
+    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 false;
+        break;
+
+    case LOG_TARGET_SHELL:
+        break;
+    }
+
+    log_target = target;
+    log_opened = true;
+
+    return true;
+}
+
+/**
+ * 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 = false;
+}
+
+/**
+ * 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_NONE);
+    current_level = new_level;
+}
+
+/**
+ * Get the logging level.
+ *
+ * /return current logging level
+ */
+log_level_t log_get_level(void)
+{
+    return current_level;
+}
+
+/**
+ * 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 true if successful, false otherwise
+ */
+bool 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 false;
+
+    /* Logging at level NONE has no sense */
+    assert(at_level < LOG_LEVEL_NONE);
+
+    if (at_level < current_level)
+        return true;
+
+    /* 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 false;
+
+        p += r;
+        size -= r;
+    }
+
+    /* Only prefix non-NOTICE level messages */
+    if (at_level != LOG_LEVEL_NOTICE)
+    {
+        r = snprintf(p, size, "%s: ", level_names[at_level]);
+        if (r < 0)
+            return false;
+
+        p += r;
+        size -= r;
+    }
+
+    va_start(al, fmt);
+    r = vsnprintf(p, size, fmt, al);
+    va_end(al);
+    if (r < 0)
+        return false;
+
+    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 true;
+}


Property changes on: firmware/tuxup/trunk/log.c
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: firmware/tuxup/trunk/log.h
===================================================================
--- firmware/tuxup/trunk/log.h                          (rev 0)
+++ firmware/tuxup/trunk/log.h  2008-09-17 10:24:55 UTC (rev 1866)
@@ -0,0 +1,46 @@
+#ifndef __USBDAEMON_LOG_H__
+#define __USBDAEMON_LOG_H__
+
+#include <stdbool.h>
+
+/** All logged messages are prefixed with this text */
+#define LOG_PREFIX  "tuxup"
+
+/** Name of the log file, without extension */
+#define LOG_FILE_NAME  "tuxup_prod"
+
+/** Logging target */
+typedef enum log_target
+{
+    LOG_TARGET_SYSLOG,
+    LOG_TARGET_TUX,
+    LOG_TARGET_SHELL
+} log_target_t;
+
+extern bool log_open(log_target_t target);
+extern void log_close(void);
+
+/** Logging levels, in increasing priorities */
+typedef enum log_level
+{
+    LOG_LEVEL_DEBUG, /**> Show everything including debug messages */
+    LOG_LEVEL_INFO, /**> Show verbose information */
+    LOG_LEVEL_NOTICE, /**> Show normal user messages */
+    LOG_LEVEL_WARNING, /**> Show warnings and errors */
+    LOG_LEVEL_ERROR, /**> Show errors only */
+    LOG_LEVEL_NONE /**> Quiet mode, show nothing */
+} log_level_t;
+
+extern void log_set_level(log_level_t new_level);
+extern log_level_t log_get_level(void);
+
+extern bool 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_notice(fmt, ...)  log_text(LOG_LEVEL_NOTICE, (fmt), ## __VA_ARGS__)
+#define log_warning(fmt, ...)  log_text(LOG_LEVEL_WARNING, (fmt), ## 
__VA_ARGS__)
+#define log_error(fmt, ...)  log_text(LOG_LEVEL_ERROR, (fmt), ## __VA_ARGS__)
+
+#endif /* __USBDAEMON_LOG_H__ */


Property changes on: firmware/tuxup/trunk/log.h
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: firmware/tuxup/trunk/main.c
===================================================================
--- firmware/tuxup/trunk/main.c 2008-09-17 09:13:05 UTC (rev 1865)
+++ firmware/tuxup/trunk/main.c 2008-09-17 10:24:55 UTC (rev 1866)
@@ -33,6 +33,7 @@
 #include "common/defines.h"
 #include "tux_misc.h"
 #include "error.h"
+#include "log.h"
 #ifdef WIN32
 #   include "tux_hid_win32.h"
 #   include <windows.h>
@@ -109,10 +110,12 @@
             " -p --pretend  Don't do the programming, just simulate.\n"
             " -h --help     Display this usage information.\n"
             " -v --verbose  Print verbose messages.\n"
+            " -d --debug    Print debug messages. \n"
             " -V --version  Print the version number.\n" "\n"
             "Connection: connect the dongle alone, then press on tux's head\n"
             "  button while switching it on. Finally, connect the white 
cable\n"
-            "  between tux and the dongle.\n" "\n"
+            "  between tux and the dongle.\n" 
+            "\n"
             "Notes:\n"
             "  * Options '-a' and -'m' can't be used simultaneously.\n"
             "  * Inputfiles can be specified only if the -a and -m options 
are\n"
@@ -161,6 +164,7 @@
     return true;
 }
 
+
 static void fux_connect(void)
 {
 #ifndef WIN32
@@ -173,8 +177,7 @@
     if (!(tux_hid_capture(TUX_VID, TUX_PID))) 
     {
 #ifndef WIN32
-        /* Unable to capture the device :
-         * Try with the libusb */
+        /* Unable to capture the device, try with the libusb */
         for (;;)
         {
             device = usb_find_tux();
@@ -187,7 +190,7 @@
 
         if (device == NULL)
         {
-            fprintf(stderr, "The dongle was not found, now exiting.\n");
+            log_error("The dongle was not found, now exiting.\n");
             exit(E_TUXUP_DONGLENOTFOUND);
         }
         else
@@ -196,7 +199,7 @@
             HID = 0;
         }
 #else
-            fprintf(stderr, "The dongle was not found, now exiting.\n");
+            log_error("The dongle was not found, now exiting.\n");
             exit(E_TUXUP_DONGLENOTFOUND);
 #endif 
     }
@@ -205,11 +208,6 @@
         /* The dongle is a HID device */
         HID = 1;
 
-        if (! verify_version())
-        { 
-            fprintf(stderr, msg_old_fuxusb);
-            exit(E_FUXUSB_VER_ERROR);
-        }
  
     }
 
@@ -224,26 +222,19 @@
     {
         if (device->descriptor.bcdDevice < 0x030)
         {
-            fprintf(stderr, msg_old_firmware);
+            log_error(msg_old_firmware);
             exit(E_TUXUP_DONGLEMANUALBOOTLOAD);
         }
     
         /* open USB device */
         if ((dev_h = usb_open_tux(device)) == NULL)
         {
-            fprintf(stderr, "USB DEVICE INIT ERROR \n");
+            log_error("USB DEVICE INIT ERROR \n");
             exit(E_TUXUP_USBERROR);
         }
-        if (! verify_version())
-        {
-            fprintf(stderr, msg_old_fuxusb);
-            exit(E_FUXUSB_VER_ERROR);
-        }
-
     }
 #endif
-    if (verbose)
-        printf("Interface configured \n\n");
+        log_info("Interface configured \n");
     usb_connected = 1;
 }
 
@@ -252,8 +243,7 @@
 {
     if (!usb_connected)
         return;
-    if (verbose)
-        printf("Closing interface ...\n");
+    log_info("Closing interface ...\n");
     if (HID)
     {
         tux_hid_release();
@@ -265,8 +255,7 @@
         usb_close(dev_h);
     }
 #endif
-    if (verbose)
-        printf("     ... interface closed \n");
+    log_info("     ... interface closed \n");
     usb_connected = 0;
 }
 
@@ -293,7 +282,7 @@
     hex_nr[2] = (char)'\0';
     if ((fs = fopen(filename, "r")) == NULL)
     {
-        fprintf(stderr, "Unable to open file '%s' for reading\n", filename);
+        log_error("Unable to open file '%s' for reading\n", filename);
         exit(E_TUXUP_BADPROGFILE);
     }
 
@@ -328,40 +317,45 @@
     if (!pretend)
         fux_connect();
 
+    if (! verify_version())
+    {
+        log_error(msg_old_fuxusb);
+        exit(E_FUXUSB_VER_ERROR);
+    }
+
     if (check_hex_file(filename, &version))
     {
-        printf("[FAIL] Programming of '%s' failed, this file is not a correct"
+        log_error("Programming of '%s' failed, this file is not a " \
                " hex file for that CPU\n\n", filename);
         return E_TUXUP_BADPROGFILE;
     }
-    printf("Programming %s in the ", filename);
     if (version.cpu_nbr == TUXCORE_CPU_NUM)
     {
-        printf("core CPU\n");
+        log_notice("\nProgramming %s in the tuxcore CPU", filename);
         cpu_i2c_addr = TUXCORE_BL_ADDR;
     }
     else if (version.cpu_nbr == TUXAUDIO_CPU_NUM)
     {
-        printf("audio CPU\n");
+        log_notice("\nProgramming %s in the tuxaudio CPU", filename);
         cpu_i2c_addr = TUXAUDIO_BL_ADDR;
     }
     else if (version.cpu_nbr == TUXRF_CPU_NUM)
     {
-        printf("tux RF CPU\n");
+        log_notice("\nProgramming %s in the tuxrf CPU", filename);
         cpu_i2c_addr = TUXRF_BL_ADDR;
     }
     else if (version.cpu_nbr == FUXRF_CPU_NUM)
     {
-        printf("fux (dongle) RF CPU\n");
+        log_notice("\nProgramming %s in the fuxrf CPU", filename);
         cpu_i2c_addr = FUXRF_BL_ADDR;
     }
     else
     {
-        printf("Unrecognized CPU number, %s doesn't appear to be compiled"
+        log_error("Unrecognized CPU number, %s doesn't appear to be compiled"
                " for a CPU of tuxdroid.\n", filename);
         return E_TUXUP_BADPROGFILE;
     }
-    printf("Version %d.%d.%d\n", version.ver_major, version.ver_minor,
+    log_notice("Version %d.%d.%d\n", version.ver_major, version.ver_minor,
            version.ver_update);
 
     if (pretend)
@@ -382,9 +376,9 @@
     else
     {
 #ifdef WIN32
-        printf(" FAIL \n");
+        log_notice(" FAIL \n");
 #else
-        printf("\033[2C[\033[01;31mFAIL\033[00m]\n");
+        log_notice("\033[2C[\033[01;31mFAIL\033[00m]\n");
 #endif
     }
     return E_TUXUP_PROGRAMMINGFAILED;
@@ -398,20 +392,25 @@
     if (!pretend)
         fux_connect();
 
-    printf("Programming %s in ", filename);
+    if (! verify_version())
+    {
+        log_error(msg_old_fuxusb);
+        exit(E_FUXUSB_VER_ERROR);
+    }
+
     if (cpu_nbr == TUXCORE_CPU_NUM)
     {
-        printf("core CPU\n");
+        log_notice("Programming %s in tuxcore CPU\n", filename);
         cpu_i2c_addr = TUXCORE_BL_ADDR;
     }
     else if (cpu_nbr == TUXAUDIO_CPU_NUM)
     {
-        printf("audio CPU\n");
+        log_notice("Programming %s in tuxaudio CPU\n", filename);
         cpu_i2c_addr = TUXAUDIO_BL_ADDR;
     }
     else
     {
-        printf("Wrong CPU number specified for the eeprom.\n");
+        log_error("Wrong CPU number specified for the eeprom.\n");
         return E_TUXUP_BADPROGFILE;
     }
 
@@ -433,9 +432,9 @@
     else
     {
 #ifdef WIN32
-        printf("Fail \n");
+        log_notice("Fail \n");
 #else
-        printf("\033[2C[\033[01;31mFAIL\033[00m]\n");
+        log_notice("\033[2C[\033[01;31mFAIL\033[00m]\n");
 #endif
     }
         return E_TUXUP_PROGRAMMINGFAILED;
@@ -443,46 +442,39 @@
 
 static int prog_usb(char const *filename)
 {
+#define QUIET_CMD "1>/dev/null 2>&1"
     /* XXX include those as defines in commands.h */
     unsigned char send_data[5] = { 0x01, 0x01, 0x00, 0x00, 0xFF };
-    char tmp_string[PATH_MAX];
+    char command_str[PATH_MAX];
     int ret;
     version_bf_t version;
 
-    printf("Programming %s in the USB CPU\n", filename);
+    log_notice("Programming %s in the USB CPU\n", filename);
 
     /* Retrieving version number */
     if (check_hex_file(filename, &version))
     {
-        printf("[FAIL] Programming of '%s' failed, this file is not a correct"
+        log_error("Programming of '%s' failed, this file is not a correct"
                " hex file for that CPU\n\n", filename);
         return E_TUXUP_BADPROGFILE;
     }
 
     if (version.cpu_nbr != FUXUSB_CPU_NUM)
     {
-        printf("Unrecognized CPU number, %s doesn't appear to be compiled"
+        log_error("Unrecognized CPU number, %s doesn't appear to be compiled"
                " for a CPU of tuxdroid.\n", filename);
         return E_TUXUP_BADPROGFILE;
     }
-    printf("Version %d.%d.%d\n", version.ver_major, version.ver_minor,
+    log_notice("Version %d.%d.%d\n", version.ver_major, version.ver_minor,
            version.ver_update);
 
     if (pretend)
         return E_TUXUP_NOERROR;
 
     /* Check if the dongle is already in bootloader mode */
-    if (verbose)
-    {
-        fprintf(stdout,
-                "Testing if the dongle is already in bootloader mode.\n"
-                "If it is not, 'no device present' will be reported.\n");
-        ret = system("dfu-programmer at89c5130 get bootloader-version");
-    }
-    else
-        ret = system("dfu-programmer at89c5130 get bootloader-version"
-                     " 2> /dev/null");
-
+    log_info("Testing if the dongle is already in bootloader mode. "\
+             "If it is not, 'no device present' will be reported.");
+    ret = system("dfu-programmer at89c5130 get bootloader-version" QUIET_CMD);
     /* If dfu-programmer didn't detect the dongle, we try to set it in
        bootloader mode with the specific command */
     if (ret)
@@ -491,15 +483,12 @@
         /* XXX a better way to code that error, a define? */
         if (ret == 32512)
         {
-            fprintf(stdout, msg_dfu_programmer_not_installed);
+            log_error(msg_dfu_programmer_not_installed);
             exit(E_TUXUP_DFUPROGNOTFOUND);
         }
-        if (verbose)
-        {
-            fprintf(stdout,
-                    "The dongle was not detected in bootloader mode, "
-                    "now \ntrying to set it with a command.\n");
-        }
+           
+        log_info("The dongle was not detected in bootloader mode, "
+                 "now \ntrying to set it with a command.\n");
         fux_connect();
         /* Enter bootloader mode. */
         if (HID)
@@ -515,46 +504,70 @@
             ret = usb_send_commands(dev_h, send_data, 5);
             if (ret == 5)
             {
-                if (verbose)
-                    fprintf(stdout, "Switched to bootloader mode.\n");
                 sleep(1);
+                log_info("Switched to bootloader mode.\n");
+                sleep(2);
             }
 
             else
             {
-                fprintf(stderr, "Switching to bootloader mode failed.\n");
+                log_error("Switching to bootloader mode failed.\n");
                 return E_TUXUP_BOOTLOADINGFAILED;
             }
         }
 #endif
     }
-    ret = system("dfu-programmer at89c5130 erase");
+    else
+    {
+        log_info("Dongle in bootloader mode");
+    }
+
+    bool quiet = true;
+    if (log_get_level() <= LOG_LEVEL_INFO)
+        quiet = false;
+
+    sprintf(command_str, "dfu-programmer at89c5130 erase %s", quiet ?
+            QUIET_CMD : "");
+    log_info(command_str);
+    ret = system(command_str);
+
     if (ret)
     {
-        fprintf(stderr, "[FAIL] Erasing the USB CPU failed.\n");
+        log_error("Erasing the USB CPU failed.\n");
+        log_notice("\033[2C[\033[01;31mFAIL\033[00m]\n");
         return E_TUXUP_PROGRAMMINGFAILED;
     }
-    sprintf(tmp_string, "dfu-programmer at89c5130 flash %s", filename);
-    ret = system(tmp_string);
+    sprintf(command_str, "dfu-programmer at89c5130 flash %s %s", filename, 
quiet ?
+            QUIET_CMD : "");
+    log_info(command_str);
+    ret = system(command_str);
     if (ret)
     {
-        fprintf(stderr, "[FAIL] Flashing the USB CPU failed.\n");
+        log_error("Flashing the USB CPU failed.\n");
+        log_notice("\033[2C[\033[01;31mFAIL\033[00m]\n");
         return E_TUXUP_PROGRAMMINGFAILED;
     }
-    ret = system("dfu-programmer at89c5130 configure HSB 0x7b");
+    sprintf(command_str, "dfu-programmer at89c5130 configure HSB 0x7b %s", 
quiet ?
+                 QUIET_CMD : "");
+    log_info(command_str);
+    ret = system(command_str);
     if (ret)
     {
-        fprintf(stderr, "[FAIL] Configuring the USB CPU failed.\n");
+        log_error("Configuring the USB CPU failed.");
+        log_notice("\033[2C[\033[01;31mFAIL\033[00m]\n");
         return E_TUXUP_PROGRAMMINGFAILED;
     }
-    ret = system("dfu-programmer at89c5130 start");
 
-    fprintf(stdout, "[OK]\n");
+    sprintf(command_str, "dfu-programmer at89c5130 start %s", quiet ? QUIET_CMD
+            : "");
+    log_info(command_str);
+    ret = system(command_str);
+    log_notice("\033[2C[ \033[01;32mOK\033[00m ]\n");
 
     fux_disconnect();
     /* Windows needs more time than linux to enumerate the device */
-    sleep(5);
-    fux_connect();
+    /*sleep(5);*/
+    /*fux_connect();*/
     return E_TUXUP_NOERROR;
 }
 
@@ -580,8 +593,7 @@
         strcat(filenamepath, filename);
         filename = filenamepath;
     }
-    if (verbose)
-        printf("Processing: %s\n", filename);
+    log_info("Processing: %s\n", filename);
 
     extension = strrchr(filename, '.');
     /* check that an extension has been given */
@@ -604,7 +616,9 @@
                 }
             }
             if (!usb)           /* AVR hex file. */
+            {
                 ret = prog_flash(filename);
+            }
         }
         else if (!strcmp(extension, ".eep"))
         {
@@ -627,8 +641,7 @@
         }
     }
     if (ret == E_TUXUP_BADPROGFILE)
-        fprintf(stderr, "%s is not a valid programming file.\n", filename);
-    fprintf(stdout, "\n");
+        log_error("%s is not a valid programming file.\n", filename);
     return ret;
 }
 
@@ -645,26 +658,32 @@
     int next_option;
 
     /* A string listing valid short options letters.  */
-    char const *const short_options = "maphvV";
+    char const *const short_options = "maqphvdV";
 
     /* An array describing valid long options. */
     const struct option long_options[] = {
         {"main",    0, NULL, 'm'},
         {"all",     0, NULL, 'a'},
+        {"quiet",   0, NULL, 'q'},
         {"pretend", 0, NULL, 'p'},
         {"help",    0, NULL, 'h'},
         {"verbose", 0, NULL, 'v'},
+        {"debug",   0, NULL, 'd'},
         {"version", 0, NULL, 'V'},
         {NULL,      0, NULL, 0}      /* Required at end of array.  */
     };
 
     /* Remember the name of the program, to incorporate in messages.
      * The name is stored in argv[0]. */
-    /*program_name = argv[0]; */
+    program_name = argv[0];
 
     /* Save the start time to measure the programming time */
     start_time = time(NULL);
 
+    /* Flags to later select the correct log level */
+    bool quiet = false, verbose = false, debug = false;
+
+
     do
     {
         next_option =
@@ -680,9 +699,9 @@
                 program_mode = MAIN;
             else
             {
-                fprintf(stderr,
-                        "'-a' and '-m' options can't be used 
simultaneoulsy.\n\n");
-                usage(stderr, E_TUXUP_BADPARAMETER);
+                log_error("'-a' and '-m' options can't be used "\
+                          "simultaneoulsy.");
+                usage(stderr, E_TUXUP_USAGE);
             }
             break;
         case 'a':              /* -a or --all */
@@ -690,17 +709,24 @@
                 program_mode = ALL;
             else
             {
-                fprintf(stderr,
-                        "'-a' and '-m' options can't be used 
simultaneoulsy.\n\n");
-                usage(stderr, E_TUXUP_BADPARAMETER);
+                log_error("'-a' and '-m' options can't be used "\
+                          "simultaneoulsy.");
+                usage(stderr, E_TUXUP_USAGE);
             }
             break;
+        case 'q':              /* -q or --quiet */
+            /* Prepare for logging */
+            quiet = true;
+            break;
         case 'p':              /* -a or --all */
             pretend = 1;
             break;
         case 'v':              /* -v or  --verbose */
-            verbose = 1;
+            verbose = true;
             break;
+            case 'd':              /* -d or  --debug */
+            debug = true;
+            break;
         case 'V':              /* -V or  --version */
             fprintf(stdout, "%s %s, an uploader program for tuxdroid.\n\n",
                     program_name, program_version);
@@ -719,11 +745,18 @@
     while (next_option != -1);
     /* Done with options. OPTIND points to first nonoption argument. */
 
-    /* Print program name and version at program launch */
-    if (verbose)
-        fprintf(stdout, "%s %s, an uploader program for tuxdroid.\n\n",
-                program_name, program_version);
+    /* Set log level */
+    if (quiet)
+        log_set_level(LOG_LEVEL_NONE);
+    else if (debug)
+        log_set_level(LOG_LEVEL_DEBUG);
+    else if (verbose)
+        log_set_level(LOG_LEVEL_INFO);
 
+    /* Print program name and version at program start */
+    log_info("%s %s, an uploader program for tuxdroid.",
+             program_name, program_version);
+
     /* If no program mode has been selected, choose INPUTFILES. */
     if (program_mode == NONE)
         program_mode = INPUTFILES;
@@ -736,15 +769,15 @@
                 strcpy(path, argv[optind]);
             else
             {
-                fprintf(stderr, "too many files or paths specified.\n\n");
-                usage(stderr, E_TUXUP_BADPARAMETER);
+                log_error("Too many files or paths specified.");
+                usage(stderr, E_TUXUP_USAGE);
             }
         }
     }
     else
     {
-        fprintf(stderr, "no file nor path specified.\n\n");
-        usage(stderr, E_TUXUP_BADPARAMETER);
+        log_error("No file nor path specified.");
+        usage(stderr, E_TUXUP_USAGE);
     }
 
     /* Select which files to program */
@@ -787,7 +820,6 @@
     /* Print time elapsed for programming. */
     end_time = time(NULL);
     if (!pretend)
-        printf("Time elapsed: %2.0f seconds.\n",
-               difftime(end_time, start_time));
+        log_notice("Time elapsed: %2.0f seconds.", difftime(end_time, 
start_time));
     return ret;
 }

Modified: firmware/tuxup/trunk/usb-connection.c
===================================================================
--- firmware/tuxup/trunk/usb-connection.c       2008-09-17 09:13:05 UTC (rev 
1865)
+++ firmware/tuxup/trunk/usb-connection.c       2008-09-17 10:24:55 UTC (rev 
1866)
@@ -25,10 +25,8 @@
 #include <syslog.h>
 
 #include "usb-connection.h"
+#include "log.h"
 
-/* Whether to display verbose messages. */
-extern int verbose;
-
 #define PRINT_DATA 0
 
 /**
@@ -74,8 +72,7 @@
     if (!(dev_h = usb_open(dev)))
         return NULL;
 
-    if (verbose)
-        printf("Device opened\n");
+    log_info("Device opened");
 
     err = usb_claim_interface(dev_h, USB_COMMAND);
     if (err != 0)
@@ -84,14 +81,13 @@
         err = usb_claim_interface(dev_h, USB_COMMAND);
         if (err != 0)
         {
-            fprintf(stderr, "Claim interface failed: %s\n", usb_strerror());
+            log_error("Claim interface failed: %s\n", usb_strerror());
             usb_close(dev_h);
             return NULL;
         }
     }
 
-    if (verbose)
-        printf("USB interface claimed successfully \n");
+    log_info("USB interface claimed successfully \n");
 
     return dev_h;
 }
@@ -127,7 +123,7 @@
     if (status < 0)
     {
         /* error on usb_interrupt_write() */
-        fprintf(stderr, "usb_interrupt_write error: status = %d :: %s \n",
+        log_error("usb_interrupt_write error: status = %d :: %s \n",
                 status, usb_strerror());
     }
     else
@@ -161,7 +157,7 @@
                                 size, USB_R_TIMEOUT);
     if (status < 0)
     {
-        fprintf(stderr, "usb_interrupt_read error: status = %d :: %s \n",
+        log_error("usb_interrupt_read error: status = %d :: %s \n",
                 status, usb_strerror());
     }
     else


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to