consolidated log message callbacks
[dcpu16] / vm-dcpu16.c
index f7f4c156f7e9d2b443c7c64ec507fbb85c9f0c70..cc06b111b0eb3630da346967996e0a6bf2f2479e 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <string.h>
 #include <strings.h>
@@ -54,6 +55,11 @@ void sigint_handler_(int sig) {
 }
 
 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
+#ifdef DEBUG
+#define DEBUG_PRINTF(...) do { if (opt_.verbose > 4) fprintf(stderr, __VA_ARGS__); } while (0)
+#else /* DEBUG */
+#define DEBUG_PRINTF(...) do { } while (0)
+#endif /* DEBUG */
 
 static
 void usage_(char *prog, unsigned int full) {
@@ -205,9 +211,13 @@ struct rfb_instance_ {
     struct dcpu16_hw *attached_keyboard;
 };
 
-/* locate or allocate the next display with an un-occupied framebuffer */
+enum rfbscreen_next_what_ {
+    NEXT_DISPLAY,
+    NEXT_KEYBOARD,
+};
+/* locate next rfb not paired to requested type */
 static
-struct rfb_instance_ *rfbScreen_next_available_display_(struct dynamic_array *rfbScreens, int argc, char *argv[]) {
+struct rfb_instance_ *rfbScreen_next_available_(enum rfbscreen_next_what_ what, struct dynamic_array *rfbScreens, int argc, char *argv[]) {
     size_t i;
     struct rfb_instance_ new_instance, *s;
     struct packed_args_ {
@@ -215,55 +225,38 @@ struct rfb_instance_ *rfbScreen_next_available_display_(struct dynamic_array *rf
         char **argv;
     } parg = { argc, argv };
 
-    fprintf(stderr, "DEBUG: rfbScreens->entries:%zu\n", rfbScreens->entries);
-
     for (i = 0; i < rfbScreens->entries; i++) {
+        struct dcpu16_hw *attached;
+
         s = (struct rfb_instance_ *)DYNARRAY_ITEM(*rfbScreens, i);
-        if (s->attached_display == NULL)
+        switch (what) {
+            case NEXT_DISPLAY:  attached = s->attached_display; break;
+            case NEXT_KEYBOARD: attached = s->attached_keyboard; break;
+        }
+        if (attached == NULL)
             return s;
     }
 
+    /* no available rfb, create new */
     if (dcpu16_hw_module_lem1802.ctl(NULL, "new_rfbScreen", &parg, &new_instance.screen)) {
-        fprintf(stderr, "failed to allocate new rfbScreen");
+        fprintf(stderr, "failed to allocate new rfbScreen\n");
         return NULL;
     }
 
-    new_instance.screen->port += rfbScreens->entries;
-    new_instance.screen->ipv6port += rfbScreens->entries;
-
     new_instance.attached_display = NULL;
     new_instance.attached_keyboard = NULL;
-    s = dynarray_add(rfbScreens, &new_instance);
-    return s;
-}
 
-/* locate or allocate the next display with an un-occupied keyboard */
-static
-struct rfb_instance_ *rfbScreen_next_available_keyboard_(struct dynamic_array *rfbScreens, int argc, char *argv[]) {
-    size_t i;
-    struct rfb_instance_ new_instance, *s;
-    struct packed_args_ {
-        int argc;
-        char **argv;
-    } parg = { argc, argv };
-
-    for (i = 0; i < rfbScreens->entries; i++) {
-        s = (struct rfb_instance_ *)DYNARRAY_ITEM(*rfbScreens, i);
-        if (s->attached_keyboard == NULL)
-            return s;
-    }
+    new_instance.screen->port += rfbScreens->entries;
+    new_instance.screen->ipv6port += rfbScreens->entries;
 
-    if (dcpu16_hw_module_lem1802.ctl(NULL, "new_rfbScreen", &parg, &new_instance.screen)) {
-        fprintf(stderr, "failed to allocate new rfbScreen");
-        return NULL;
-    }
+    DEBUG_PRINTF("%s>> port:%u\n", __func__, new_instance.screen->port);
 
-    new_instance.attached_display = NULL;
-    new_instance.attached_keyboard = NULL;
     s = dynarray_add(rfbScreens, &new_instance);
+
     return s;
 }
 
+
 /* begin serving a screen */
 void rfbScreen_start(rfbScreenInfoPtr s) {
     rfbInitServer(s);
@@ -557,27 +550,25 @@ COMMAND_IMPL(run) {
                     ts_cycle_waste = ts_cycle_rem;
         } else {
             /* negative, we've already blown our time */
-#if 0
-            fprintf(stderr, "cycle time overrun %ld.%09lds\n", ts_cycle_waste.tv_sec, ts_cycle_waste.tv_nsec);
-#endif
+            DEBUG_PRINTF("cycle time overrun %ld.%09lds\n", ts_cycle_waste.tv_sec, ts_cycle_waste.tv_nsec);
         }
 
-#if 0
+#ifdef DEBUG
         /* how did we do */
         gettimespecofday(&ts_cycle_end);
         timespec_subtract(&ts_cycle_rem, &ts_cycle_end_target, &ts_cycle_end);
-        fprintf(stderr, "projected end: %ld.%09ld  actual end: %ld.%09ld  diff: %ld.%09ld\n",
+        DEBUG_PRINTF("projected end: %ld.%09ld  actual end: %ld.%09ld  diff: %ld.%09ld\n",
                 ts_cycle_end_target.tv_sec, ts_cycle_end_target.tv_nsec,
                 ts_cycle_end.tv_sec, ts_cycle_end.tv_nsec,
                 ts_cycle_rem.tv_sec, ts_cycle_rem.tv_nsec);
-#endif
+#endif /* DEBUG */
 
     }
 
     run_cycle_end = vm->cycle_;
     gettimespecofday(&ts_run_end);
     timespec_subtract(&ts_run_diff, &ts_run_end, &ts_run_start);
-    fprintf(stderr, "ran %lld cycles in %ld.%09lds\n",
+    VERBOSE_PRINTF("ran %lld cycles in %ld.%09lds\n",
             run_cycle_end - run_cycle_start,
             ts_run_diff.tv_sec, ts_run_diff.tv_nsec);
 
@@ -633,7 +624,7 @@ COMMAND_IMPL(display) {
         char *argv[] = { "vm-dcpu16", NULL };
         struct rfb_instance_ *s;
 
-        s = rfbScreen_next_available_display_(&rfbScreens_, argc, argv);
+        s = rfbScreen_next_available_(NEXT_DISPLAY, &rfbScreens_, argc, argv);
         if (s == NULL) {
             fprintf(stderr, "failed to initialize vnc\n");
             dcpu16_hw_del(&hw);
@@ -648,6 +639,8 @@ COMMAND_IMPL(display) {
         s->attached_display = hw;
         rfbScreen_start(s->screen);
         renderer_data = s->screen;
+
+        DEBUG_PRINTF("attached display to rfb (frameBuffer:%p)\n", s->screen->frameBuffer);
     }
 #endif /* HAVE_LIBVNCSERVER */
 
@@ -706,7 +699,7 @@ COMMAND_IMPL(keyboard) {
     int argc = 1;
     char *argv[] = { "vm-dcpu16", NULL };
 
-    s = rfbScreen_next_available_keyboard_(&rfbScreens_, argc, argv);
+    s = rfbScreen_next_available_(NEXT_KEYBOARD, &rfbScreens_, argc, argv);
     if (s == NULL) {
         fprintf(stderr, "failed to initialize vnc\n");
         dcpu16_hw_del(&hw);
@@ -784,6 +777,28 @@ COMMAND_HELP(help) {
     fprintf(f, "Displays a list of available commands, or detailed help on a specific command.\n");
 }
 
+static
+void msg_verbose_filter_(unsigned int level, char *fmt, ...) {
+    static const char * const levels[] = { "error", "info", "debug" };
+    FILE *f = (level <= MSG_ERROR) ? stderr : stdout;
+    va_list ap;
+
+    if (level + 2 > opt_.verbose)
+        return;
+
+    if (level < sizeof levels / sizeof *levels)
+        fprintf(f, "[%s %s] ", "dcpu16", levels[level]);
+    else
+        fprintf(f, "[%s %u] ", "dcpu16", level);
+
+    va_start(ap, fmt);
+    vfprintf(f, fmt, ap);
+    va_end(ap);
+
+    fprintf(f, "\n");
+
+    fflush(f);
+}
 
 int main(int argc, char **argv) {
     const char prompt_fmt[] = "PC:%04x> ";
@@ -809,15 +824,11 @@ int main(int argc, char **argv) {
                 exit(EX_USAGE);
         }
     }
-    if (opt_.verbose < 1) {
-        dcpu16_warn_cb_set(NULL);
-        dcpu16_trace_cb_set(NULL);
-    } else if (opt_.verbose < 2) {
-        dcpu16_trace_cb_set(NULL);
-    }
     argc -= optind;
     argv += optind;
 
+    dcpu16_msg_set_default(msg_verbose_filter_);
+
     if ((vm = dcpu16_new()) == NULL) {
         fprintf(stderr, "could not allocate new dcpu16 instance\n");
         exit(EX_UNAVAILABLE);