diff --git a/serial_posix.c b/serial_posix.c index 23968f6..cbac93a 100644 --- a/serial_posix.c +++ b/serial_posix.c @@ -12,13 +12,13 @@ #include // nanosleep #include "serial.h" +#include "utils.h" -#include // perror #define TRACE(expr) \ { \ int error = errno; \ - fprintf (stderr, "TRACE %s:%d: ", __FILE__, __LINE__); \ - perror (expr); \ + message ("TRACE (%s:%d, %s): %s (%d)\n", __FILE__, __LINE__, \ + expr, serial_errmsg (), serial_errcode ()); \ errno = error; \ } diff --git a/serial_win32.c b/serial_win32.c index 3ddc5ff..d892269 100644 --- a/serial_win32.c +++ b/serial_win32.c @@ -2,16 +2,13 @@ #include #include "serial.h" +#include "utils.h" -#include #define TRACE(expr) \ { \ DWORD error = GetLastError (); \ - fprintf (stderr, "TRACE %s:%d: ", __FILE__, __LINE__); \ - if (expr) \ - fprintf (stderr, "%s: %s\n", expr, serial_errmsg ()); \ - else \ - fprintf (stderr, "%s\n", serial_errmsg ()); \ + message ("TRACE (%s:%d, %s): %s (%d)\n", __FILE__, __LINE__, \ + expr, serial_errmsg (), serial_errcode ()); \ SetLastError (error); \ } diff --git a/suunto_vyper.c b/suunto_vyper.c index bdc3b42..31b300f 100644 --- a/suunto_vyper.c +++ b/suunto_vyper.c @@ -1,16 +1,16 @@ -#include // fprintf #include // memcmp, memcpy #include // malloc, free #include "suunto.h" #include "serial.h" +#include "utils.h" #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define WARNING(expr) \ { \ - fprintf (stderr, "%s:%d: %s\n", __FILE__, __LINE__, expr); \ + message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ } #define EXITCODE(rc, n) \ @@ -318,11 +318,11 @@ suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char dat return rc; #ifndef NDEBUG - printf ("VyperRead(0x%04x,%d)=\"", address, len); + message ("VyperRead(0x%04x,%d)=\"", address, len); for (unsigned int i = 0; i < len; ++i) { - printf("%02x", data[i]); + message("%02x", data[i]); } - printf("\"\n"); + message("\"\n"); #endif nbytes += len; @@ -357,7 +357,7 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c return rc; #ifndef NDEBUG - printf("VyperPrepareWrite();\n"); + message("VyperPrepareWrite();\n"); #endif // Write the package. @@ -374,11 +374,11 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c return rc; #ifndef NDEBUG - printf ("VyperWrite(0x%04x,%d,\"", address, len); + message ("VyperWrite(0x%04x,%d,\"", address, len); for (unsigned int i = 0; i < len; ++i) { - printf ("%02x", data[i]); + message ("%02x", data[i]); } - printf ("\");\n"); + message ("\");\n"); #endif nbytes += len; @@ -475,11 +475,11 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size, WARNING ("Null package received."); #ifndef NDEBUG suunto_vyper_reverse (data, nbytes); - printf ("Vyper%sProfile=\"", init ? "First" : ""); + message ("Vyper%sProfile=\"", init ? "First" : ""); for (unsigned int i = 0; i < nbytes; ++i) { - printf("%02x", data[i]); + message("%02x", data[i]); } - printf("\"\n"); + message("\"\n"); #endif return 0; } @@ -499,11 +499,11 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size, suunto_vyper_reverse (data, nbytes); #ifndef NDEBUG - printf ("Vyper%sProfile=\"", init ? "First" : ""); + message ("Vyper%sProfile=\"", init ? "First" : ""); for (unsigned int i = 0; i < nbytes; ++i) { - printf("%02x", data[i]); + message("%02x", data[i]); } - printf("\"\n"); + message("\"\n"); #endif return nbytes; diff --git a/suunto_vyper2.c b/suunto_vyper2.c index b82ee28..02d3192 100644 --- a/suunto_vyper2.c +++ b/suunto_vyper2.c @@ -1,16 +1,16 @@ -#include // fprintf #include // memcmp, memcpy #include // malloc, free #include "suunto.h" #include "serial.h" +#include "utils.h" #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define WARNING(expr) \ { \ - fprintf (stderr, "%s:%d: %s\n", __FILE__, __LINE__, expr); \ + message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ } #define EXITCODE(rc, n) \ @@ -206,7 +206,7 @@ suunto_vyper2_read_version (vyper2 *device, unsigned char data[], unsigned int s return rc; #ifndef NDEBUG - printf ("Vyper2ReadVersion()=\"%02x %02x %02x %02x\"\n", data[0], data[1], data[2], data[3]); + message ("Vyper2ReadVersion()=\"%02x %02x %02x %02x\"\n", data[0], data[1], data[2], data[3]); #endif return SUUNTO_SUCCESS; @@ -227,7 +227,7 @@ suunto_vyper2_reset_maxdepth (vyper2 *device) return rc; #ifndef NDEBUG - printf ("Vyper2ResetMaxDepth()\n"); + message ("Vyper2ResetMaxDepth()\n"); #endif return SUUNTO_SUCCESS; @@ -261,11 +261,11 @@ suunto_vyper2_read_memory (vyper2 *device, unsigned int address, unsigned char d return rc; #ifndef NDEBUG - printf ("Vyper2Read(0x%04x,%d)=\"", address, len); + message ("Vyper2Read(0x%04x,%d)=\"", address, len); for (unsigned int i = 0; i < len; ++i) { - printf("%02x", data[i]); + message("%02x", data[i]); } - printf("\"\n"); + message("\"\n"); #endif nbytes += len; @@ -305,11 +305,11 @@ suunto_vyper2_write_memory (vyper2 *device, unsigned int address, const unsigned return rc; #ifndef NDEBUG - printf ("Vyper2Write(0x%04x,%d,\"", address, len); + message ("Vyper2Write(0x%04x,%d,\"", address, len); for (unsigned int i = 0; i < len; ++i) { - printf ("%02x", data[i]); + message ("%02x", data[i]); } - printf ("\");\n"); + message ("\");\n"); #endif nbytes += len; diff --git a/suunto_vyper2_test.c b/suunto_vyper2_test.c index 5872d16..63fab4f 100644 --- a/suunto_vyper2_test.c +++ b/suunto_vyper2_test.c @@ -1,47 +1,53 @@ -#include // fprintf +#include // fopen, fwrite, fclose #include "suunto.h" +#include "utils.h" + +#define WARNING(expr) \ +{ \ + message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ +} int test_dump_memory (const char* name, const char* filename) { unsigned char data[SUUNTO_VYPER2_MEMORY_SIZE] = {0}; vyper2 *device = NULL; - printf ("suunto_vyper2_open\n"); + message ("suunto_vyper2_open\n"); int rc = suunto_vyper2_open (&device, name); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Error opening serial port.\n"); + WARNING ("Error opening serial port."); return rc; } - printf ("suunto_vyper2_read_version\n"); + message ("suunto_vyper2_read_version\n"); unsigned char version[4] = {0}; rc = suunto_vyper2_read_version (device, version, sizeof (version)); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot identify computer.\n"); + WARNING ("Cannot identify computer."); suunto_vyper2_close (device); return rc; } - printf ("suunto_vyper2_read_memory\n"); + message ("suunto_vyper2_read_memory\n"); rc = suunto_vyper2_read_memory (device, 0x00, data, sizeof (data)); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot read memory.\n"); + WARNING ("Cannot read memory."); suunto_vyper2_close (device); return rc; } - printf ("Dumping data\n"); + message ("Dumping data\n"); FILE* fp = fopen (filename, "wb"); if (fp != NULL) { fwrite (data, sizeof (unsigned char), sizeof (data), fp); fclose (fp); } - printf ("suunto_vyper2_close\n"); + message ("suunto_vyper2_close\n"); rc = suunto_vyper2_close (device); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot close device."); + WARNING ("Cannot close device."); return rc; } @@ -70,6 +76,8 @@ const char* errmsg (int rc) int main(int argc, char *argv[]) { + message_set_logfile ("VYPER2.LOG"); + #ifdef _WIN32 const char* name = "COM1"; #else @@ -82,9 +90,11 @@ int main(int argc, char *argv[]) int a = test_dump_memory (name, "VYPER2.DMP"); - printf ("\nSUMMARY\n"); - printf ("-------\n"); - printf ("test_dump_memory: %s\n", errmsg (a)); + message ("\nSUMMARY\n"); + message ("-------\n"); + message ("test_dump_memory: %s\n", errmsg (a)); + + message_set_logfile (NULL); return 0; } diff --git a/suunto_vyper_test.c b/suunto_vyper_test.c index d31cd6a..ec15175 100644 --- a/suunto_vyper_test.c +++ b/suunto_vyper_test.c @@ -1,82 +1,87 @@ -#include // fprintf +#include // fopen, fwrite, fclose #include "suunto.h" #include "serial.h" +#include "utils.h" +#define WARNING(expr) \ +{ \ + message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ +} int test_dump_sdm16 (const char* name, const char* filename) { unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0}; vyper *device = NULL; - printf ("suunto_vyper_open\n"); + message ("suunto_vyper_open\n"); int rc = suunto_vyper_open (&device, name); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "%s:%d: Error opening serial port.\n",__FILE__,__LINE__); + WARNING ("Error opening serial port."); return rc; } - printf ("suunto_vyper_detect_interface\n"); + message ("suunto_vyper_detect_interface\n"); rc = suunto_vyper_detect_interface (device); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "%s:%d: Interface not found.\n",__FILE__,__LINE__); + WARNING ("Interface not found."); suunto_vyper_close (device); return rc; } - printf ("suunto_vyper_read_memory\n"); + message ("suunto_vyper_read_memory\n"); rc = suunto_vyper_read_memory (device, 0x24, data + 0x24, 1); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "%s:%d: Cannot identify computer.\n",__FILE__,__LINE__); + WARNING ("Cannot identify computer."); suunto_vyper_close (device); return rc; } rc = suunto_vyper_read_memory (device, 0x1E, data + 0x1E, 14); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot read memory.\n"); + WARNING ("Cannot read memory."); suunto_vyper_close (device); return rc; } rc = suunto_vyper_read_memory (device, 0x2C, data + 0x2C, 32); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot read memory.\n"); + WARNING ("Cannot read memory."); suunto_vyper_close (device); return rc; } rc = suunto_vyper_read_memory (device, 0x53, data + 0x53, 30); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot read memory.\n"); + WARNING ("Cannot read memory."); suunto_vyper_close (device); return rc; } - printf ("suunto_vyper_read_dive\n"); + message ("suunto_vyper_read_dive\n"); int ndives = 0; int offset = 0x71; do { - fprintf (stderr, "Reading dive #%d.\n", ndives + 1); + message ("Reading dive #%d.\n", ndives + 1); rc = suunto_vyper_read_dive (device, data + offset, sizeof (data) - offset, (ndives == 0)); if (rc < 0) { - fprintf (stderr, "Cannot read dive.\n"); + WARNING ("Cannot read dive."); suunto_vyper_close (device); return rc; } - fprintf (stderr, "Returned %i bytes at offset 0x%04x.\n", rc, offset); + message ("Returned %i bytes at offset 0x%04x.\n", rc, offset); ndives++; offset += rc; } while (rc > 0); - printf ("Dumping data\n"); + message ("Dumping data\n"); FILE *fp = fopen (filename, "wb"); if (fp != NULL) { fwrite (data, sizeof (unsigned char), sizeof (data), fp); fclose (fp); } - printf ("suunto_vyper_close\n"); + message ("suunto_vyper_close\n"); rc = suunto_vyper_close (device); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot close device."); + WARNING ("Cannot close device."); return rc; } @@ -88,40 +93,40 @@ int test_dump_memory (const char* name, const char* filename) unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0}; vyper *device = NULL; - printf ("suunto_vyper_open\n"); + message ("suunto_vyper_open\n"); int rc = suunto_vyper_open (&device, name); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "%s:%d: Error opening serial port.\n",__FILE__,__LINE__); + WARNING ("Error opening serial port."); return rc; } - printf ("suunto_vyper_detect_interface\n"); + message ("suunto_vyper_detect_interface\n"); rc = suunto_vyper_detect_interface (device); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "%s:%d: Interface not found.\n",__FILE__,__LINE__); + WARNING ("Interface not found."); suunto_vyper_close (device); return rc; } - printf ("suunto_vyper_read_memory\n"); + message ("suunto_vyper_read_memory\n"); rc = suunto_vyper_read_memory (device, 0x00, data, sizeof (data)); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot read memory.\n"); + WARNING ("Cannot read memory."); suunto_vyper_close (device); return rc; } - printf ("Dumping data\n"); + message ("Dumping data\n"); FILE* fp = fopen (filename, "wb"); if (fp != NULL) { fwrite (data, sizeof (unsigned char), sizeof (data), fp); fclose (fp); } - printf ("suunto_vyper_close\n"); + message ("suunto_vyper_close\n"); rc = suunto_vyper_close (device); if (rc != SUUNTO_SUCCESS) { - fprintf (stderr, "Cannot close device."); + WARNING ("Cannot close device."); return rc; } @@ -150,6 +155,8 @@ const char* errmsg (int rc) int main(int argc, char *argv[]) { + message_set_logfile ("VYPER.LOG"); + #ifdef _WIN32 const char* name = "COM1"; #else @@ -160,13 +167,15 @@ int main(int argc, char *argv[]) name = argv[1]; } - int a = test_dump_sdm16 (name, "PROFILE.SDM"); - int b = test_dump_memory (name, "PROFILE.DMP"); + int a = test_dump_sdm16 (name, "VYPER.SDM"); + int b = test_dump_memory (name, "VYPER.DMP"); - printf ("\nSUMMARY\n"); - printf ("-------\n"); - printf ("test_dump_sdm16: %s\n", errmsg (a)); - printf ("test_dump_memory: %s\n", errmsg (b)); + message ("\nSUMMARY\n"); + message ("-------\n"); + message ("test_dump_sdm16: %s\n", errmsg (a)); + message ("test_dump_memory: %s\n", errmsg (b)); + + message_set_logfile (NULL); return 0; } diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..8f3a59c --- /dev/null +++ b/utils.c @@ -0,0 +1,32 @@ +#include +#include + +static FILE* g_logfile = NULL; + +int message (const char* fmt, ...) +{ + va_list ap = {0}; + + if (g_logfile) { + va_start (ap, fmt); + vfprintf (g_logfile, fmt, ap); + va_end (ap); + } + + va_start (ap, fmt); + int rc = vfprintf (stdout, fmt, ap); + va_end (ap); + + return rc; +} + +void message_set_logfile (const char* filename) +{ + if (g_logfile) { + fclose (g_logfile); + g_logfile = NULL; + } + + if (filename) + g_logfile = fopen (filename, "w"); +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..4f45528 --- /dev/null +++ b/utils.h @@ -0,0 +1,16 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +int message (const char* fmt, ...); + +void message_set_logfile (const char* filename); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* DEBUG_H */