diff options
author | Dan Williams <dcbw@redhat.com> | 2011-09-27 15:31:45 -0500 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2011-09-27 15:33:53 -0500 |
commit | 368b80886912cf9f03ed535bba1c426466461195 (patch) | |
tree | 3939408c918e0fde931cd348178075c3382854bf /libwmc | |
parent | 8710820156f51d1861b329e6fc12e706304ddda9 (diff) |
libwmc: simpler logging and error handling
Diffstat (limited to 'libwmc')
-rw-r--r-- | libwmc/src/com.c | 15 | ||||
-rw-r--r-- | libwmc/src/com.h | 12 | ||||
-rw-r--r-- | libwmc/src/errors.c | 93 | ||||
-rw-r--r-- | libwmc/src/errors.h | 53 | ||||
-rw-r--r-- | libwmc/tests/test-wmc-com.c | 17 |
5 files changed, 60 insertions, 130 deletions
diff --git a/libwmc/src/com.c b/libwmc/src/com.c index 751dd4a7..2f4d3b3c 100644 --- a/libwmc/src/com.c +++ b/libwmc/src/com.c @@ -23,16 +23,16 @@ #include "com.h" #include "errors.h" -wbool -wmc_port_setup (int fd, WmcError **error) +int +wmc_port_setup (int fd) { struct termios stbuf; errno = 0; memset (&stbuf, 0, sizeof (stbuf)); if (tcgetattr (fd, &stbuf) != 0) { - wmc_error_set (error, WMC_SERIAL_ERROR, WMC_SERIAL_ERROR_CONFIG_FAILED, - "tcgetattr() error: %d", errno); + wmc_err (0, "tcgetattr() error: %d", errno); + return -WMC_ERROR_SERIAL_CONFIG_FAILED; } stbuf.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB); @@ -47,11 +47,10 @@ wmc_port_setup (int fd, WmcError **error) errno = 0; if (tcsetattr (fd, TCSANOW, &stbuf) < 0) { - wmc_error_set (error, WMC_SERIAL_ERROR, WMC_SERIAL_ERROR_CONFIG_FAILED, - "tcsetattr() error: %d", errno); - return FALSE; + wmc_err (0, "tcgetattr() error: %d", errno); + return -WMC_ERROR_SERIAL_CONFIG_FAILED; } - return TRUE; + return 0; } diff --git a/libwmc/src/com.h b/libwmc/src/com.h index ffcf5d54..219a0169 100644 --- a/libwmc/src/com.h +++ b/libwmc/src/com.h @@ -18,16 +18,6 @@ #ifndef LIBWMC_COM_H #define LIBWMC_COM_H -#include "errors.h" - -enum { - WMC_SERIAL_ERROR = 1000 -}; - -enum { - WMC_SERIAL_ERROR_CONFIG_FAILED = 1 -}; - -wbool wmc_port_setup (int fd, WmcError **error); +int wmc_port_setup (int fd); #endif /* LIBWMC_COM_H */ diff --git a/libwmc/src/errors.c b/libwmc/src/errors.c index a1d87ba1..0403b229 100644 --- a/libwmc/src/errors.c +++ b/libwmc/src/errors.c @@ -19,91 +19,40 @@ #include <stdlib.h> #include <string.h> -WmcError * -wmc_error_new (u_int32_t domain, - u_int32_t code, - const char *format, - ...) -{ - WmcError *error; - va_list args; - int n; - - wmc_return_val_if_fail (format != NULL, NULL); - wmc_return_val_if_fail (format[0] != '\0', NULL); - - error = malloc (sizeof (WmcError)); - wmc_assert (error != NULL); - - error->domain = domain; - error->code = code; - - va_start (args, format); - n = vasprintf (&error->message, format, args); - va_end (args); - - if (n < 0) { - free (error); - return NULL; - } - - return error; -} - void -wmc_error_set (WmcError **error, - u_int32_t domain, - u_int32_t code, - const char *format, - ...) +_wmc_log (const char *file, + int line, + const char *func, + int level, + int domain, + const char *format, + ...) { va_list args; + char *message = NULL; int n; + const char *prefix = "info"; - if (error == NULL) - return; - wmc_return_if_fail (*error == NULL); + wmc_return_if_fail (format != NULL); wmc_return_if_fail (format[0] != '\0'); - *error = malloc (sizeof (WmcError)); - wmc_assert (*error != NULL); + /* level & domain ignored for now */ - (*error)->domain = domain; - (*error)->code = code; + if (getenv ("WMC_DEBUG") == NULL) + return; va_start (args, format); - n = vasprintf (&(*error)->message, format, args); + n = vasprintf (&message, format, args); va_end (args); - if (n < 0) { - free (*error); - *error = NULL; - } -} - -static void -free_error (WmcError *error) -{ - if (error) { - if (error->message) - free (error->message); - memset (error, 0, sizeof (*error)); - free (error); - } -} + if (level & LOGL_ERR) + prefix = "err"; + else if (level & LOGL_DEBUG) + prefix = "dbg"; -void -wmc_clear_error (WmcError **error) -{ - if (error) { - free_error (*error); - *error = NULL; + if (n >= 0) { + fprintf (stderr, "<%s> [%s:%u] %s(): %s\n", prefix, file, line, func, message); + free (message); } } -void -wmc_free_error (WmcError *error) -{ - free_error (error); -} - diff --git a/libwmc/src/errors.h b/libwmc/src/errors.h index aebe40f2..f9346df9 100644 --- a/libwmc/src/errors.h +++ b/libwmc/src/errors.h @@ -23,35 +23,18 @@ #include <assert.h> #include <stdio.h> #include <stdarg.h> -#include <stdio.h> - -typedef u_int8_t wbool; -#ifndef TRUE -#define TRUE (u_int8_t) 1 -#endif -#ifndef FALSE -#define FALSE (u_int8_t) 0 -#endif - -typedef struct { - u_int32_t domain; - u_int32_t code; - char *message; -} WmcError; - -WmcError *wmc_error_new (u_int32_t domain, - u_int32_t code, - const char *format, - ...) __attribute__((__format__ (__printf__, 3, 4))); -void wmc_error_set (WmcError **error, - u_int32_t domain, - u_int32_t code, - const char *format, - ...) __attribute__((__format__ (__printf__, 4, 5))); +enum { + LOGL_ERR = 0x00000001, + LOGL_WARN = 0x00000002, + LOGL_INFO = 0x00000004, + LOGL_DEBUG = 0x00000008 +}; -void wmc_clear_error (WmcError **error); -void wmc_free_error (WmcError *error); +enum { + WMC_SUCCESS = 0, + WMC_ERROR_SERIAL_CONFIG_FAILED = 1, +}; #define wmc_assert assert @@ -71,4 +54,18 @@ void wmc_free_error (WmcError *error); } \ } -#endif /* LIBWMC_COM_H */ +void _wmc_log (const char *file, + int line, + const char *func, + int domain, + int level, + const char *format, + ...) __attribute__((__format__ (__printf__, 6, 7))); + +#define wmc_dbg(domain, ...) \ + _wmc_log (__FILE__, __LINE__, __func__, domain, LOGL_DEBUG, ## __VA_ARGS__ ) + +#define wmc_err(domain, ...) \ + _wmc_log (__FILE__, __LINE__, __func__, domain, LOGL_ERR, ## __VA_ARGS__ ) + +#endif /* LIBWMC_ERRORS_H */ diff --git a/libwmc/tests/test-wmc-com.c b/libwmc/tests/test-wmc-com.c index a2e1e448..643c0993 100644 --- a/libwmc/tests/test-wmc-com.c +++ b/libwmc/tests/test-wmc-com.c @@ -208,16 +208,11 @@ void test_com_port_init (void *f, void *data) { TestComData *d = data; - WmcError *error = NULL; - gboolean success; - - success = wmc_port_setup (d->fd, &error); - if (!success) { - g_warning ("%s: error setting up port: (%d) %s", - d->port, - error ? error->code : -1, - error && error->message ? error->message : "(unknown)"); - } - g_assert (success); + int ret; + + ret = wmc_port_setup (d->fd); + if (ret < 0) + g_warning ("%s: error setting up serial port: (%d)", d->port, ret); + g_assert_cmpint (ret, ==, 0); } |