Added utility functions to duplicate all output to a logfile.

This commit is contained in:
Jef Driesen 2008-01-02 12:11:31 +00:00
parent 8b52082a2f
commit a004a94c2a
8 changed files with 143 additions and 79 deletions

View File

@ -12,13 +12,13 @@
#include <time.h> // nanosleep #include <time.h> // nanosleep
#include "serial.h" #include "serial.h"
#include "utils.h"
#include <stdio.h> // perror
#define TRACE(expr) \ #define TRACE(expr) \
{ \ { \
int error = errno; \ int error = errno; \
fprintf (stderr, "TRACE %s:%d: ", __FILE__, __LINE__); \ message ("TRACE (%s:%d, %s): %s (%d)\n", __FILE__, __LINE__, \
perror (expr); \ expr, serial_errmsg (), serial_errcode ()); \
errno = error; \ errno = error; \
} }

View File

@ -2,16 +2,13 @@
#include <windows.h> #include <windows.h>
#include "serial.h" #include "serial.h"
#include "utils.h"
#include <stdio.h>
#define TRACE(expr) \ #define TRACE(expr) \
{ \ { \
DWORD error = GetLastError (); \ DWORD error = GetLastError (); \
fprintf (stderr, "TRACE %s:%d: ", __FILE__, __LINE__); \ message ("TRACE (%s:%d, %s): %s (%d)\n", __FILE__, __LINE__, \
if (expr) \ expr, serial_errmsg (), serial_errcode ()); \
fprintf (stderr, "%s: %s\n", expr, serial_errmsg ()); \
else \
fprintf (stderr, "%s\n", serial_errmsg ()); \
SetLastError (error); \ SetLastError (error); \
} }

View File

@ -1,16 +1,16 @@
#include <stdio.h> // fprintf
#include <string.h> // memcmp, memcpy #include <string.h> // memcmp, memcpy
#include <stdlib.h> // malloc, free #include <stdlib.h> // malloc, free
#include "suunto.h" #include "suunto.h"
#include "serial.h" #include "serial.h"
#include "utils.h"
#define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define WARNING(expr) \ #define WARNING(expr) \
{ \ { \
fprintf (stderr, "%s:%d: %s\n", __FILE__, __LINE__, expr); \ message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \
} }
#define EXITCODE(rc, n) \ #define EXITCODE(rc, n) \
@ -318,11 +318,11 @@ suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char dat
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
printf ("VyperRead(0x%04x,%d)=\"", address, len); message ("VyperRead(0x%04x,%d)=\"", address, len);
for (unsigned int i = 0; i < len; ++i) { for (unsigned int i = 0; i < len; ++i) {
printf("%02x", data[i]); message("%02x", data[i]);
} }
printf("\"\n"); message("\"\n");
#endif #endif
nbytes += len; nbytes += len;
@ -357,7 +357,7 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
printf("VyperPrepareWrite();\n"); message("VyperPrepareWrite();\n");
#endif #endif
// Write the package. // Write the package.
@ -374,11 +374,11 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
printf ("VyperWrite(0x%04x,%d,\"", address, len); message ("VyperWrite(0x%04x,%d,\"", address, len);
for (unsigned int i = 0; i < len; ++i) { for (unsigned int i = 0; i < len; ++i) {
printf ("%02x", data[i]); message ("%02x", data[i]);
} }
printf ("\");\n"); message ("\");\n");
#endif #endif
nbytes += len; nbytes += len;
@ -475,11 +475,11 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size,
WARNING ("Null package received."); WARNING ("Null package received.");
#ifndef NDEBUG #ifndef NDEBUG
suunto_vyper_reverse (data, nbytes); suunto_vyper_reverse (data, nbytes);
printf ("Vyper%sProfile=\"", init ? "First" : ""); message ("Vyper%sProfile=\"", init ? "First" : "");
for (unsigned int i = 0; i < nbytes; ++i) { for (unsigned int i = 0; i < nbytes; ++i) {
printf("%02x", data[i]); message("%02x", data[i]);
} }
printf("\"\n"); message("\"\n");
#endif #endif
return 0; return 0;
} }
@ -499,11 +499,11 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size,
suunto_vyper_reverse (data, nbytes); suunto_vyper_reverse (data, nbytes);
#ifndef NDEBUG #ifndef NDEBUG
printf ("Vyper%sProfile=\"", init ? "First" : ""); message ("Vyper%sProfile=\"", init ? "First" : "");
for (unsigned int i = 0; i < nbytes; ++i) { for (unsigned int i = 0; i < nbytes; ++i) {
printf("%02x", data[i]); message("%02x", data[i]);
} }
printf("\"\n"); message("\"\n");
#endif #endif
return nbytes; return nbytes;

View File

@ -1,16 +1,16 @@
#include <stdio.h> // fprintf
#include <string.h> // memcmp, memcpy #include <string.h> // memcmp, memcpy
#include <stdlib.h> // malloc, free #include <stdlib.h> // malloc, free
#include "suunto.h" #include "suunto.h"
#include "serial.h" #include "serial.h"
#include "utils.h"
#define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define WARNING(expr) \ #define WARNING(expr) \
{ \ { \
fprintf (stderr, "%s:%d: %s\n", __FILE__, __LINE__, expr); \ message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \
} }
#define EXITCODE(rc, n) \ #define EXITCODE(rc, n) \
@ -206,7 +206,7 @@ suunto_vyper2_read_version (vyper2 *device, unsigned char data[], unsigned int s
return rc; return rc;
#ifndef NDEBUG #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 #endif
return SUUNTO_SUCCESS; return SUUNTO_SUCCESS;
@ -227,7 +227,7 @@ suunto_vyper2_reset_maxdepth (vyper2 *device)
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
printf ("Vyper2ResetMaxDepth()\n"); message ("Vyper2ResetMaxDepth()\n");
#endif #endif
return SUUNTO_SUCCESS; return SUUNTO_SUCCESS;
@ -261,11 +261,11 @@ suunto_vyper2_read_memory (vyper2 *device, unsigned int address, unsigned char d
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
printf ("Vyper2Read(0x%04x,%d)=\"", address, len); message ("Vyper2Read(0x%04x,%d)=\"", address, len);
for (unsigned int i = 0; i < len; ++i) { for (unsigned int i = 0; i < len; ++i) {
printf("%02x", data[i]); message("%02x", data[i]);
} }
printf("\"\n"); message("\"\n");
#endif #endif
nbytes += len; nbytes += len;
@ -305,11 +305,11 @@ suunto_vyper2_write_memory (vyper2 *device, unsigned int address, const unsigned
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
printf ("Vyper2Write(0x%04x,%d,\"", address, len); message ("Vyper2Write(0x%04x,%d,\"", address, len);
for (unsigned int i = 0; i < len; ++i) { for (unsigned int i = 0; i < len; ++i) {
printf ("%02x", data[i]); message ("%02x", data[i]);
} }
printf ("\");\n"); message ("\");\n");
#endif #endif
nbytes += len; nbytes += len;

View File

@ -1,47 +1,53 @@
#include <stdio.h> // fprintf #include <stdio.h> // fopen, fwrite, fclose
#include "suunto.h" #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) int test_dump_memory (const char* name, const char* filename)
{ {
unsigned char data[SUUNTO_VYPER2_MEMORY_SIZE] = {0}; unsigned char data[SUUNTO_VYPER2_MEMORY_SIZE] = {0};
vyper2 *device = NULL; vyper2 *device = NULL;
printf ("suunto_vyper2_open\n"); message ("suunto_vyper2_open\n");
int rc = suunto_vyper2_open (&device, name); int rc = suunto_vyper2_open (&device, name);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Error opening serial port.\n"); WARNING ("Error opening serial port.");
return rc; return rc;
} }
printf ("suunto_vyper2_read_version\n"); message ("suunto_vyper2_read_version\n");
unsigned char version[4] = {0}; unsigned char version[4] = {0};
rc = suunto_vyper2_read_version (device, version, sizeof (version)); rc = suunto_vyper2_read_version (device, version, sizeof (version));
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot identify computer.\n"); WARNING ("Cannot identify computer.");
suunto_vyper2_close (device); suunto_vyper2_close (device);
return rc; return rc;
} }
printf ("suunto_vyper2_read_memory\n"); message ("suunto_vyper2_read_memory\n");
rc = suunto_vyper2_read_memory (device, 0x00, data, sizeof (data)); rc = suunto_vyper2_read_memory (device, 0x00, data, sizeof (data));
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); WARNING ("Cannot read memory.");
suunto_vyper2_close (device); suunto_vyper2_close (device);
return rc; return rc;
} }
printf ("Dumping data\n"); message ("Dumping data\n");
FILE* fp = fopen (filename, "wb"); FILE* fp = fopen (filename, "wb");
if (fp != NULL) { if (fp != NULL) {
fwrite (data, sizeof (unsigned char), sizeof (data), fp); fwrite (data, sizeof (unsigned char), sizeof (data), fp);
fclose (fp); fclose (fp);
} }
printf ("suunto_vyper2_close\n"); message ("suunto_vyper2_close\n");
rc = suunto_vyper2_close (device); rc = suunto_vyper2_close (device);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot close device."); WARNING ("Cannot close device.");
return rc; return rc;
} }
@ -70,6 +76,8 @@ const char* errmsg (int rc)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
message_set_logfile ("VYPER2.LOG");
#ifdef _WIN32 #ifdef _WIN32
const char* name = "COM1"; const char* name = "COM1";
#else #else
@ -82,9 +90,11 @@ int main(int argc, char *argv[])
int a = test_dump_memory (name, "VYPER2.DMP"); int a = test_dump_memory (name, "VYPER2.DMP");
printf ("\nSUMMARY\n"); message ("\nSUMMARY\n");
printf ("-------\n"); message ("-------\n");
printf ("test_dump_memory: %s\n", errmsg (a)); message ("test_dump_memory: %s\n", errmsg (a));
message_set_logfile (NULL);
return 0; return 0;
} }

View File

@ -1,82 +1,87 @@
#include <stdio.h> // fprintf #include <stdio.h> // fopen, fwrite, fclose
#include "suunto.h" #include "suunto.h"
#include "serial.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) int test_dump_sdm16 (const char* name, const char* filename)
{ {
unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0}; unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0};
vyper *device = NULL; vyper *device = NULL;
printf ("suunto_vyper_open\n"); message ("suunto_vyper_open\n");
int rc = suunto_vyper_open (&device, name); int rc = suunto_vyper_open (&device, name);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Error opening serial port.\n",__FILE__,__LINE__); WARNING ("Error opening serial port.");
return rc; return rc;
} }
printf ("suunto_vyper_detect_interface\n"); message ("suunto_vyper_detect_interface\n");
rc = suunto_vyper_detect_interface (device); rc = suunto_vyper_detect_interface (device);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Interface not found.\n",__FILE__,__LINE__); WARNING ("Interface not found.");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
printf ("suunto_vyper_read_memory\n"); message ("suunto_vyper_read_memory\n");
rc = suunto_vyper_read_memory (device, 0x24, data + 0x24, 1); rc = suunto_vyper_read_memory (device, 0x24, data + 0x24, 1);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Cannot identify computer.\n",__FILE__,__LINE__); WARNING ("Cannot identify computer.");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
rc = suunto_vyper_read_memory (device, 0x1E, data + 0x1E, 14); rc = suunto_vyper_read_memory (device, 0x1E, data + 0x1E, 14);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); WARNING ("Cannot read memory.");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
rc = suunto_vyper_read_memory (device, 0x2C, data + 0x2C, 32); rc = suunto_vyper_read_memory (device, 0x2C, data + 0x2C, 32);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); WARNING ("Cannot read memory.");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
rc = suunto_vyper_read_memory (device, 0x53, data + 0x53, 30); rc = suunto_vyper_read_memory (device, 0x53, data + 0x53, 30);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); WARNING ("Cannot read memory.");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
printf ("suunto_vyper_read_dive\n"); message ("suunto_vyper_read_dive\n");
int ndives = 0; int ndives = 0;
int offset = 0x71; int offset = 0x71;
do { 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)); rc = suunto_vyper_read_dive (device, data + offset, sizeof (data) - offset, (ndives == 0));
if (rc < 0) { if (rc < 0) {
fprintf (stderr, "Cannot read dive.\n"); WARNING ("Cannot read dive.");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; 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++; ndives++;
offset += rc; offset += rc;
} while (rc > 0); } while (rc > 0);
printf ("Dumping data\n"); message ("Dumping data\n");
FILE *fp = fopen (filename, "wb"); FILE *fp = fopen (filename, "wb");
if (fp != NULL) { if (fp != NULL) {
fwrite (data, sizeof (unsigned char), sizeof (data), fp); fwrite (data, sizeof (unsigned char), sizeof (data), fp);
fclose (fp); fclose (fp);
} }
printf ("suunto_vyper_close\n"); message ("suunto_vyper_close\n");
rc = suunto_vyper_close (device); rc = suunto_vyper_close (device);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot close device."); WARNING ("Cannot close device.");
return rc; return rc;
} }
@ -88,40 +93,40 @@ int test_dump_memory (const char* name, const char* filename)
unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0}; unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0};
vyper *device = NULL; vyper *device = NULL;
printf ("suunto_vyper_open\n"); message ("suunto_vyper_open\n");
int rc = suunto_vyper_open (&device, name); int rc = suunto_vyper_open (&device, name);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Error opening serial port.\n",__FILE__,__LINE__); WARNING ("Error opening serial port.");
return rc; return rc;
} }
printf ("suunto_vyper_detect_interface\n"); message ("suunto_vyper_detect_interface\n");
rc = suunto_vyper_detect_interface (device); rc = suunto_vyper_detect_interface (device);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Interface not found.\n",__FILE__,__LINE__); WARNING ("Interface not found.");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
printf ("suunto_vyper_read_memory\n"); message ("suunto_vyper_read_memory\n");
rc = suunto_vyper_read_memory (device, 0x00, data, sizeof (data)); rc = suunto_vyper_read_memory (device, 0x00, data, sizeof (data));
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); WARNING ("Cannot read memory.");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
printf ("Dumping data\n"); message ("Dumping data\n");
FILE* fp = fopen (filename, "wb"); FILE* fp = fopen (filename, "wb");
if (fp != NULL) { if (fp != NULL) {
fwrite (data, sizeof (unsigned char), sizeof (data), fp); fwrite (data, sizeof (unsigned char), sizeof (data), fp);
fclose (fp); fclose (fp);
} }
printf ("suunto_vyper_close\n"); message ("suunto_vyper_close\n");
rc = suunto_vyper_close (device); rc = suunto_vyper_close (device);
if (rc != SUUNTO_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot close device."); WARNING ("Cannot close device.");
return rc; return rc;
} }
@ -150,6 +155,8 @@ const char* errmsg (int rc)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
message_set_logfile ("VYPER.LOG");
#ifdef _WIN32 #ifdef _WIN32
const char* name = "COM1"; const char* name = "COM1";
#else #else
@ -160,13 +167,15 @@ int main(int argc, char *argv[])
name = argv[1]; name = argv[1];
} }
int a = test_dump_sdm16 (name, "PROFILE.SDM"); int a = test_dump_sdm16 (name, "VYPER.SDM");
int b = test_dump_memory (name, "PROFILE.DMP"); int b = test_dump_memory (name, "VYPER.DMP");
printf ("\nSUMMARY\n"); message ("\nSUMMARY\n");
printf ("-------\n"); message ("-------\n");
printf ("test_dump_sdm16: %s\n", errmsg (a)); message ("test_dump_sdm16: %s\n", errmsg (a));
printf ("test_dump_memory: %s\n", errmsg (b)); message ("test_dump_memory: %s\n", errmsg (b));
message_set_logfile (NULL);
return 0; return 0;
} }

32
utils.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdarg.h>
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");
}

16
utils.h Normal file
View File

@ -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 */