chiark / gitweb /
routesearch: sort out debugging output
[ypp-sc-tools.db-test.git] / yarrg / rsmain.c
index a498022649042309a704165ad2ce4e3a6abf7fd2..545ece002f6ae250e077f95ff4ab1d1179253373 100644 (file)
@@ -2,11 +2,17 @@
 
 #include "rscommon.h"
 
+#include <ctype.h>
+
 int o_quiet= 0;
 double max_mass=-1, max_volu=-1, max_capi=-1;
 double distance_loss_factor_per_league;
 int max_dist= -1;
 
+FILE *debug_file;
+FILE *output;
+
+
 #define CTR(x) int ctr_##x;
   COUNTER_LIST
 #undef CTR
@@ -14,13 +20,14 @@ int max_dist= -1;
 static PotentialResult ****results;
   /* results[start_isle_ix][finalisle][midisle]-> */
 
+static pid_t debugoutpid;
+
 int main(int argc, const char **argv) {
   const char *arg;
 
 #ifndef debug_flags
   debug_flags= ~( dbg_sql2 );
 #endif
-  sysassert( !setvbuf(debug,0,_IOLBF,0) );
   
   for (;;) {
     arg= *++argv;
@@ -35,6 +42,35 @@ int main(int argc, const char **argv) {
     }
   }
 
+  if (debug_flags) {
+    /* glpk insists on writing stuff to stdout, and it does buffering,
+     * so we route all our debug through it this too */
+    int realstdout;
+    sysassert( (realstdout= dup(1)) > 2 );
+    sysassert( output= fdopen(realstdout,"w") );
+
+    int pfd[2];
+    sysassert(! pipe(pfd) );
+    sysassert( (debugoutpid= fork()) >=0 );
+    if (!debugoutpid) {
+      sysassert( dup2(pfd[0],0)==0 );
+      sysassert( dup2(2,1)==1 );
+      sysassert(! close(pfd[0]) );
+      sysassert(! close(pfd[1]) );
+      sysassert(! execlp("cat","cat",(char*)0) );
+    }
+    sysassert( dup2(pfd[1],1)==1 );
+    sysassert(! close(pfd[0]) );
+    sysassert(! close(pfd[1]) );
+
+    debug_file= stdout;
+  } else {
+    output= stdout;
+    debug_file= stderr;
+  }
+
+  sysassert( !setvbuf(debug,0,_IOLBF,0) );
+
   max_mass= atof(*argv++);
   max_volu= atof(*argv++);
   max_capi= atof(*argv++);
@@ -56,24 +92,41 @@ int main(int argc, const char **argv) {
       ia[ni++]= atoi(arg);
 
     double val= value_route(ni, ia, 0);
-    printf("route value is %g\n", val);
+    fprintf(output, "route value is %g\n", val);
   } else if (!strcmp(arg,"search")) {
-    results= mcalloc(sizeof(*results)*argc);
+    MCALLOC(results, argc);
+
     max_dist= atoi(*argv++);
+    nhighscores_absolute= atoi(*argv++);
+    nhighscores_perleague= atoi(*argv++);
+    const char *final_isle_spec= *argv++;
+
+    MCALLOC(highscores_absolute, nhighscores_absolute);
+    MCALLOC(highscores_perleague, nhighscores_perleague);
+
     int resultsix= 0;
-    while ((arg= *argv++)) {
-      search(atoi(arg), &results[resultsix]);
+    while ((arg= argv[resultsix])) {
+      int init_isle= atoi(arg);
+      
+      int final_isle;
+      if (!strcmp(final_isle_spec,"circ")) final_isle= init_isle;
+      else if (!strcmp(final_isle_spec,"any")) final_isle= -1;
+      else final_isle= atoi(final_isle_spec);
+      assert(final_isle);
+
+      search(init_isle, final_isle, &results[resultsix]);
       resultsix++;
     }
 
     int i, midarch, finarch;
     for (i=0; i<resultsix; i++) {
-      fprintf(stderr,"================== start #%d ==================\n",i);
+      fprintf(stderr,"============== start #%d %s [PARTIAL] ==============\n",
+             i, argv[i]);
       PotentialResult ***strat_resultsix= results[i];
       if (!strat_resultsix) continue;
       fprintf(stderr,"  ");
       for (midarch=0; midarch<narches; midarch++) {
-       fprintf(stderr,"| mid %d ",midarch);
+       fprintf(stderr,"|   mid %d  ",midarch);
       }
       fprintf(stderr,"\n");
       for (finarch=0; finarch<narches; finarch++) {
@@ -83,25 +136,54 @@ int main(int argc, const char **argv) {
        for (midarch=0; midarch<narches; midarch++) {
          PotentialResult *result= strat_finarch[midarch];
          if (!result) {
-           fprintf(stderr,"|       ");
+           fprintf(stderr,"|          ");
          } else {
-           if (result->absolute < 100) fprintf(stderr,"|  .");
-           else fprintf(stderr,"|%3d",(int)(result->absolute / 100));
+           fprintf(stderr,"|%5d",(int)(result->absolute));
            fprintf(stderr," ");
-           if (result->perleague < 10) fprintf(stderr,"  .");
-           else fprintf(stderr,"%3d",(int)(result->perleague / 10));
+           fprintf(stderr,"%4d",(int)(result->perleague));
          }
        }
        fprintf(stderr,"\n");
       }
     }
+
+    int pos;
+#define OUT(absperl)                                                         \
+    fprintf(output,"\n================== " #absperl " ==================\n"); \
+    for (pos=0; pos<nhighscores_##absperl; pos++) {                          \
+      HighScoreEntry *hs= &highscores_##absperl[pos];                        \
+      PotentialResult *pr= hs->pr;                                           \
+      if (!pr) continue;                                                     \
+      const int *const ports= pr->absperl##_ports;                           \
+      int nports;                                                            \
+      for (nports=0; nports<MAX_ROUTELEN && ports[nports]>=0; nports++);      \
+      int finisle= ports[nports-1]; int finarch= isle2arch(finisle);         \
+      int midarch= route2midarch(ports,nports);                                      \
+      fprintf(output,                                                        \
+             " @%2d #%2d | start%3d mid%d f%d:%3d | %5d %5d %4d |",     \
+             pos, nhighscores_##absperl - 1 - pos,                           \
+             ports[0], midarch, finarch,finisle,                     \
+             (int)hs->value, (int)pr->absolute, (int)pr->perleague);         \
+      for (i=0; i<nports; i++) fprintf(output," %d",ports[i]);               \
+      fprintf(output,"\n");                                                  \
+    }
+    OUT(absolute)
+    OUT(perleague)
+    fprintf(output,"\n");
+    
   } else {
     abort();
   }
 
-#define CTR(x) fprintf(stderr,"  %-30s %10d\n",#x,ctr_##x);
+#define CTR(x) fprintf(output,"  %-30s %10d\n",#x,ctr_##x);
   COUNTER_LIST
 #undef CTR
 
+  if (debug_flags) {
+    sysassert(! fclose(debug) );
+    waitpid_check_exitstatus(debugoutpid,"debug cat",1);
+  }
+  sysassert(! fclose(output) );
+
   return 0;
 }