]> sigrok.org Git - libserialport.git/commitdiff
Revise debug macros to work in strict C99.
authorMartin Ling <redacted>
Sun, 24 Aug 2014 12:59:32 +0000 (13:59 +0100)
committerMartin Ling <redacted>
Sun, 24 Aug 2014 13:06:21 +0000 (14:06 +0100)
libserialport_internal.h
linux.c
serialport.c

index 78b727b7a2dbbb7a506786092c77e9a8e0e372f9..962fc20833a61d77c344af61a8f09f53c5efb117 100644 (file)
@@ -155,15 +155,25 @@ extern const struct std_baudrate std_baudrates[];
 extern void (*sp_debug_handler)(const char *format, ...);
 
 /* Debug output macros. */
 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(); \
        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);
        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); \
 #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);
        } \
 } 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; \
 #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; \
        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; \
        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)
        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)
 
 
 #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 9ccd3b384608d8ed4fd62551a4744f9af04cc5bb..dbff4c4459282a376e2035c6e3f005474939d9a4 100644 (file)
--- 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))
        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)
 
        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;
        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);
                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
                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
                }
                        }
 #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");
                *list = list_append(*list, name);
                if (!list) {
                        SET_ERROR(ret, SP_ERR_MEM, "list append failed");
index f8de843d1e40cf48dc92531c82817628b3480702..4dc1b83d0146917fcfc5927db51df905645351f5 100644 (file)
@@ -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");
 
        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");
 
        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");
 
        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;
 
 #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();
 
 
        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. */
 
 #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"};
 
 
        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;
 
 #ifdef _WIN32
        DWORD flags = 0;
@@ -637,7 +638,7 @@ SP_API enum sp_return sp_drain(struct sp_port *port)
 
        CHECK_OPEN_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. */
 
 #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)
                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
        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);
 
        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);
                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");
                        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");
 
        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);
 
        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)
                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
        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);
 
        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);
                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");
                }
                } 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");
 
        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;
 
 #ifdef _WIN32
        DWORD bytes_read;
@@ -1034,7 +1039,7 @@ SP_API enum sp_return sp_input_waiting(struct sp_port *port)
 
        CHECK_OPEN_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;
 
 #ifdef _WIN32
        DWORD errors;
@@ -1057,7 +1062,7 @@ SP_API enum sp_return sp_output_waiting(struct sp_port *port)
 
        CHECK_OPEN_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;
 
 #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);
 
 
        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))
 
 #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);
 
 
        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) {
 
 #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");
 
        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
 
        *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)
 {
 
 SP_API int sp_last_error_code(void)
 {
-       TRACE("");
+       TRACE_VOID();
 #ifdef _WIN32
        RETURN_INT(GetLastError());
 #else
 #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)
 {
 
 SP_API char *sp_last_error_message(void)
 {
-       TRACE("");
+       TRACE_VOID();
 
 #ifdef _WIN32
        LPVOID message;
 
 #ifdef _WIN32
        LPVOID message;