From: Martin Ling Date: Sun, 24 Aug 2014 12:59:32 +0000 (+0100) Subject: Revise debug macros to work in strict C99. X-Git-Tag: libserialport-0.1.1~109 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=7890cef6cf4f8496888f2a86593f9eff55244456;p=libserialport.git Revise debug macros to work in strict C99. --- diff --git a/libserialport_internal.h b/libserialport_internal.h index 78b727b..962fc20 100644 --- a/libserialport_internal.h +++ b/libserialport_internal.h @@ -155,15 +155,25 @@ extern const struct std_baudrate std_baudrates[]; extern void (*sp_debug_handler)(const char *format, ...); /* Debug output macros. */ -#define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0) -#define DEBUG_ERROR(err, fmt, ...) DEBUG("%s returning " #err ": " fmt, __func__, ##__VA_ARGS__) -#define DEBUG_FAIL(fmt, ...) do { \ +#define DEBUG_FMT(fmt, ...) do { \ + if (sp_debug_handler) \ + sp_debug_handler(fmt ".\n", __VA_ARGS__); \ +} while (0) +#define DEBUG(msg) DEBUG_FMT(msg, NULL) +#define DEBUG_ERROR(err, msg) DEBUG_FMT("%s returning " #err ": " msg, __func__) +#define DEBUG_FAIL(msg) do { \ char *errmsg = sp_last_error_message(); \ - DEBUG("%s returning SP_ERR_FAIL: "fmt": %s", __func__,##__VA_ARGS__,errmsg); \ + DEBUG_FMT("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \ sp_free_error_message(errmsg); \ } while (0); -#define RETURN() do { DEBUG("%s returning", __func__); return; } while(0) -#define RETURN_CODE(x) do { DEBUG("%s returning " #x, __func__); return x; } while (0) +#define RETURN() do { \ + DEBUG_FMT("%s returning", __func__); \ + return; \ +} while(0) +#define RETURN_CODE(x) do { \ + DEBUG_FMT("%s returning " #x, __func__); \ + return x; \ +} while (0) #define RETURN_CODEVAL(x) do { \ switch (x) { \ case SP_OK: RETURN_CODE(SP_OK); \ @@ -174,26 +184,33 @@ extern void (*sp_debug_handler)(const char *format, ...); } \ } while (0) #define RETURN_OK() RETURN_CODE(SP_OK); -#define RETURN_ERROR(err, ...) do { DEBUG_ERROR(err, __VA_ARGS__); return err; } while (0) -#define RETURN_FAIL(...) do { DEBUG_FAIL(__VA_ARGS__); return SP_ERR_FAIL; } while (0) +#define RETURN_ERROR(err, msg) do { \ + DEBUG_ERROR(err, msg); \ + return err; \ +} while (0) +#define RETURN_FAIL(msg) do { \ + DEBUG_FAIL(msg); \ + return SP_ERR_FAIL; \ +} while (0) #define RETURN_INT(x) do { \ int _x = x; \ - DEBUG("%s returning %d", __func__, _x); \ + DEBUG_FMT("%s returning %d", __func__, _x); \ return _x; \ } while (0) #define RETURN_STRING(x) do { \ char *_x = x; \ - DEBUG("%s returning %s", __func__, _x); \ + DEBUG_FMT("%s returning %s", __func__, _x); \ return _x; \ } while (0) #define RETURN_POINTER(x) do { \ void *_x = x; \ - DEBUG("%s returning %p", __func__, _x); \ + DEBUG_FMT("%s returning %p", __func__, _x); \ return _x; \ } while (0) #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0) #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0) -#define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__) +#define TRACE(fmt, ...) DEBUG_FMT("%s(" fmt ") called", __func__, __VA_ARGS__) +#define TRACE_VOID() DEBUG_FMT("%s() called", __func__) #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0) diff --git a/linux.c b/linux.c index 9ccd3b3..dbff4c4 100644 --- a/linux.c +++ b/linux.c @@ -35,12 +35,12 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port) int i, count; if (strncmp(port->name, "/dev/", 5)) - RETURN_ERROR(SP_ERR_ARG, "Device name not recognized (%s)", port->name); + RETURN_ERROR(SP_ERR_ARG, "Device name not recognized."); snprintf(file_name, sizeof(file_name), "/sys/class/tty/%s", dev); count = readlink(file_name, file_name, sizeof(file_name)); if (count <= 0 || count >= (int) sizeof(file_name)-1) - RETURN_ERROR(SP_ERR_ARG, "Device not found (%s)", port->name); + RETURN_ERROR(SP_ERR_ARG, "Device not found."); file_name[count] = 0; if (strstr(file_name, "bluetooth")) port->transport = SP_TRANSPORT_BLUETOOTH; @@ -189,7 +189,7 @@ SP_PRIV enum sp_return list_ports(struct sp_port ***list) if (strstr(target, "virtual")) continue; snprintf(name, sizeof(name), "/dev/%s", entry.d_name); - DEBUG("Found device %s", name); + DEBUG_FMT("Found device %s", name); if (strstr(target, "serial8250")) { /* The serial8250 driver has a hardcoded number of ports. * The only way to tell which actually exist on a given system @@ -214,7 +214,7 @@ SP_PRIV enum sp_return list_ports(struct sp_port ***list) } #endif } - DEBUG("Found port %s", name); + DEBUG_FMT("Found port %s", name); *list = list_append(*list, name); if (!list) { SET_ERROR(ret, SP_ERR_MEM, "list append failed"); diff --git a/serialport.c b/serialport.c index f8de843..4dc1b83 100644 --- a/serialport.c +++ b/serialport.c @@ -70,7 +70,7 @@ SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port * if (!portname) RETURN_ERROR(SP_ERR_ARG, "Null port name"); - DEBUG("Building structure for port %s", portname); + DEBUG_FMT("Building structure for port %s", portname); if (!(port = malloc(sizeof(struct sp_port)))) RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed"); @@ -405,7 +405,7 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) if (flags > (SP_MODE_READ | SP_MODE_WRITE)) RETURN_ERROR(SP_ERR_ARG, "Invalid flags"); - DEBUG("Opening port %s", port->name); + DEBUG_FMT("Opening port %s", port->name); #ifdef _WIN32 DWORD desired_access = 0, flags_and_attributes = 0, errors; @@ -564,7 +564,7 @@ SP_API enum sp_return sp_close(struct sp_port *port) CHECK_OPEN_PORT(); - DEBUG("Closing port %s", port->name); + DEBUG_FMT("Closing port %s", port->name); #ifdef _WIN32 /* Returns non-zero upon success, 0 upon failure. */ @@ -603,7 +603,8 @@ SP_API enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers) const char *buffer_names[] = {"no", "input", "output", "both"}; - DEBUG("Flushing %s buffers on port %s", buffer_names[buffers], port->name); + DEBUG_FMT("Flushing %s buffers on port %s", + buffer_names[buffers], port->name); #ifdef _WIN32 DWORD flags = 0; @@ -637,7 +638,7 @@ SP_API enum sp_return sp_drain(struct sp_port *port) CHECK_OPEN_PORT(); - DEBUG("Draining port %s", port->name); + DEBUG_FMT("Draining port %s", port->name); #ifdef _WIN32 /* Returns non-zero upon success, 0 upon failure. */ @@ -678,9 +679,11 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, RETURN_ERROR(SP_ERR_ARG, "Null buffer"); if (timeout) - DEBUG("Writing %d bytes to port %s, timeout %d ms", count, port->name, timeout); + DEBUG_FMT("Writing %d bytes to port %s, timeout %d ms", + count, port->name, timeout); else - DEBUG("Writing %d bytes to port %s, no timeout", count, port->name); + DEBUG_FMT("Writing %d bytes to port %s, no timeout", + count, port->name); if (count == 0) RETURN_INT(0); @@ -709,7 +712,7 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, if (GetLastError() == ERROR_IO_PENDING) { DEBUG("Waiting for write to complete"); GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE); - DEBUG("Write completed, %d/%d bytes written", bytes_written, count); + DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count); RETURN_INT(bytes_written); } else { RETURN_FAIL("WriteFile() failed"); @@ -792,7 +795,7 @@ SP_API enum sp_return sp_nonblocking_write(struct sp_port *port, if (!buf) RETURN_ERROR(SP_ERR_ARG, "Null buffer"); - DEBUG("Writing up to %d bytes to port %s", count, port->name); + DEBUG_FMT("Writing up to %d bytes to port %s", count, port->name); if (count == 0) RETURN_INT(0); @@ -873,9 +876,11 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, RETURN_ERROR(SP_ERR_ARG, "Null buffer"); if (timeout) - DEBUG("Reading %d bytes from port %s, timeout %d ms", count, port->name, timeout); + DEBUG_FMT("Reading %d bytes from port %s, timeout %d ms", + count, port->name, timeout); else - DEBUG("Reading %d bytes from port %s, no timeout", count, port->name); + DEBUG_FMT("Reading %d bytes from port %s, no timeout", + count, port->name); if (count == 0) RETURN_INT(0); @@ -894,7 +899,7 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, if (GetLastError() == ERROR_IO_PENDING) { DEBUG("Waiting for read to complete"); GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE); - DEBUG("Read completed, %d/%d bytes read", bytes_read, count); + DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count); } else { RETURN_FAIL("ReadFile() failed"); } @@ -984,7 +989,7 @@ SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, if (!buf) RETURN_ERROR(SP_ERR_ARG, "Null buffer"); - DEBUG("Reading up to %d bytes from port %s", count, port->name); + DEBUG_FMT("Reading up to %d bytes from port %s", count, port->name); #ifdef _WIN32 DWORD bytes_read; @@ -1034,7 +1039,7 @@ SP_API enum sp_return sp_input_waiting(struct sp_port *port) CHECK_OPEN_PORT(); - DEBUG("Checking input bytes waiting on port %s", port->name); + DEBUG_FMT("Checking input bytes waiting on port %s", port->name); #ifdef _WIN32 DWORD errors; @@ -1057,7 +1062,7 @@ SP_API enum sp_return sp_output_waiting(struct sp_port *port) CHECK_OPEN_PORT(); - DEBUG("Checking output bytes waiting on port %s", port->name); + DEBUG_FMT("Checking output bytes waiting on port %s", port->name); #ifdef _WIN32 DWORD errors; @@ -1374,7 +1379,7 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data, TRACE("%p, %p, %p", port, data, config); - DEBUG("Getting configuration for port %s", port->name); + DEBUG_FMT("Getting configuration for port %s", port->name); #ifdef _WIN32 if (!GetCommState(port->hdl, &data->dcb)) @@ -1591,7 +1596,7 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data, TRACE("%p, %p, %p", port, data, config); - DEBUG("Setting configuration for port %s", port->name); + DEBUG_FMT("Setting configuration for port %s", port->name); #ifdef _WIN32 if (config->baudrate >= 0) { @@ -2141,7 +2146,7 @@ SP_API enum sp_return sp_get_signals(struct sp_port *port, if (!signals) RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); - DEBUG("Getting control signals for port %s", port->name); + DEBUG_FMT("Getting control signals for port %s", port->name); *signals = 0; #ifdef _WIN32 @@ -2206,7 +2211,7 @@ SP_API enum sp_return sp_end_break(struct sp_port *port) SP_API int sp_last_error_code(void) { - TRACE(""); + TRACE_VOID(); #ifdef _WIN32 RETURN_INT(GetLastError()); #else @@ -2216,7 +2221,7 @@ SP_API int sp_last_error_code(void) SP_API char *sp_last_error_message(void) { - TRACE(""); + TRACE_VOID(); #ifdef _WIN32 LPVOID message;