From 6af0f110bbd879fe4c49c2ebd64715a6e692e7c0 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 13 Dec 2010 14:36:00 +0100 Subject: debug-output eingebaut, sofern man mit -DDEBUG kompiliert --- aufgabe4/main.c | 2 +- aufgabe4/queue.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) (limited to 'aufgabe4') diff --git a/aufgabe4/main.c b/aufgabe4/main.c index 3f6edec..979ee4b 100644 --- a/aufgabe4/main.c +++ b/aufgabe4/main.c @@ -61,7 +61,7 @@ pid_t fork_child(funcptr work, funcptr cleanup) { } /* parent process */ - printf("forked\n"); + printf("forked, pid = %d\n", pid); fflush(stdout); return pid; } diff --git a/aufgabe4/queue.c b/aufgabe4/queue.c index e376331..b882255 100644 --- a/aufgabe4/queue.c +++ b/aufgabe4/queue.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include #include @@ -90,6 +92,31 @@ void queue_init() { } } +#ifdef DEBUG +static void vlog(char *fmt, va_list args) { + char timebuf[64]; + + pid_t p = getpid(); + /* Get current time */ + time_t t = time(NULL); + /* Convert time to local time (determined by the locale) */ + struct tm *tmp = localtime(&t); + /* Generate time prefix */ + strftime(timebuf, sizeof(timebuf), "%X - ", tmp); + printf("[%d] %s", p, timebuf); + vprintf(fmt, args); + fflush(stdout); +} + +static void log(char *fmt, ...) { + va_list args; + + va_start(args, fmt); + vlog(fmt, args); + va_end(args); +} +#endif + /* * Hängt den übergebenen Wert in die Queue. * @@ -110,7 +137,11 @@ struct msg *queue_write(uint8_t dir, uint8_t data) { } struct msg *curmsg = shmdata + (shmheader->cur * sizeof(struct msg)); - struct msg *nextmsg = shmdata + (next * sizeof(struct msg)); + +#ifdef DEBUG + log("queue_write(), next = %d, nextmsg = %p, cur = %d, cur->dir = %d\n", + next, nextmsg, shmheader->cur, curmsg->dir); +#endif memcpy(nextmsg, &msg, sizeof(struct msg)); if (curmsg->dir == D_INVALID) shmheader->cur = next; @@ -125,6 +156,10 @@ struct msg *queue_write(uint8_t dir, uint8_t data) { */ uint8_t queue_get_dir() { struct msg *curmsg = shmdata + (shmheader->cur * sizeof(struct msg)); +#ifdef DEBUG + log("queue_get_dir(), cur = %d, cur->dir = %d\n", + shmheader->cur, curmsg->dir); +#endif return curmsg->dir; } @@ -137,6 +172,11 @@ uint8_t queue_get_data() { uint8_t data; struct msg *curmsg = shmdata + (shmheader->cur * sizeof(struct msg)); + +#ifdef DEBUG + log("queue_get_data(), cur = %d, cur->dir = %d\n", + shmheader->cur, curmsg->dir); +#endif data = curmsg->data; lock(); -- cgit v1.2.3-70-g09d2