libwmc: simpler logging and error handling
This commit is contained in:
+7
-8
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+1
-11
@@ -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 */
|
||||
|
||||
+22
-73
@@ -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;
|
||||
if (level & LOGL_ERR)
|
||||
prefix = "err";
|
||||
else if (level & LOGL_DEBUG)
|
||||
prefix = "dbg";
|
||||
|
||||
if (n >= 0) {
|
||||
fprintf (stderr, "<%s> [%s:%u] %s(): %s\n", prefix, file, line, func, message);
|
||||
free (message);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
free_error (WmcError *error)
|
||||
{
|
||||
if (error) {
|
||||
if (error->message)
|
||||
free (error->message);
|
||||
memset (error, 0, sizeof (*error));
|
||||
free (error);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wmc_clear_error (WmcError **error)
|
||||
{
|
||||
if (error) {
|
||||
free_error (*error);
|
||||
*error = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wmc_free_error (WmcError *error)
|
||||
{
|
||||
free_error (error);
|
||||
}
|
||||
|
||||
|
||||
+25
-28
@@ -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
|
||||
enum {
|
||||
LOGL_ERR = 0x00000001,
|
||||
LOGL_WARN = 0x00000002,
|
||||
LOGL_INFO = 0x00000004,
|
||||
LOGL_DEBUG = 0x00000008
|
||||
};
|
||||
|
||||
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)));
|
||||
|
||||
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 */
|
||||
|
||||
@@ -208,16 +208,11 @@ void
|
||||
test_com_port_init (void *f, void *data)
|
||||
{
|
||||
TestComData *d = data;
|
||||
WmcError *error = NULL;
|
||||
gboolean success;
|
||||
int ret;
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user