From: Ian Jackson Date: Sat, 10 Oct 2009 12:16:00 +0000 (+0100) Subject: routesearch: sort out debugging output X-Git-Tag: 5.0^2~57 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=commitdiff_plain;h=f6949486bad107662b74a0bed72e7041d633c794 routesearch: sort out debugging output --- diff --git a/yarrg/rscommon.h b/yarrg/rscommon.h index 0a6f306..b4e7803 100644 --- a/yarrg/rscommon.h +++ b/yarrg/rscommon.h @@ -12,11 +12,13 @@ DF(check) \ DF(lp) -#define debug stdout -#define DEBUG_DEV "/dev/stdout" +#define debug debug_file #include "common.h" +extern FILE *debug_file; +#define DEBUG_DEV "/dev/stdout" /* just for glpk */ + #define COUNTER_LIST \ CTR(commodities_loaded) \ @@ -44,9 +46,9 @@ * chk1 and chk2 are blocks using sqe and int sqr; */ \ const char *sql_must_call_string= #call; \ int sqr; \ - if (DEBUGP(sql2)) fprintf(stderr,"SQL %s", sql_must_call_string); \ + if (DEBUGP(sql2)) fprintf(debug,"SQL %s", sql_must_call_string); \ sqr= (call); \ - if (DEBUGP(sql2)) fprintf(stderr," = %d\n", sqr); \ + if (DEBUGP(sql2)) fprintf(debug," = %d\n", sqr); \ if (sqr) sql_fatal("(unknown)", sqr, sql_must_call_string); \ }) \ @@ -131,6 +133,9 @@ extern char **archnames; extern int *islandid2arch; +extern FILE *output; + + #define NEW(ptr) ((ptr)= mmalloc(sizeof(*ptr))) #define MCALLOC(array, count) ((array)= mcalloc(sizeof(*(array)) * (count))) diff --git a/yarrg/rsmain.c b/yarrg/rsmain.c index c76e7da..545ece0 100644 --- a/yarrg/rsmain.c +++ b/yarrg/rsmain.c @@ -9,6 +9,10 @@ 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 @@ -16,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; @@ -37,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++); @@ -58,7 +92,7 @@ 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")) { MCALLOC(results, argc); @@ -115,7 +149,7 @@ int main(int argc, const char **argv) { int pos; #define OUT(absperl) \ - fprintf(stderr,"\n================== " #absperl " ==================\n"); \ + fprintf(output,"\n================== " #absperl " ==================\n"); \ for (pos=0; pospr; \ @@ -125,24 +159,31 @@ int main(int argc, const char **argv) { for (nports=0; nports=0; nports++); \ int finisle= ports[nports-1]; int finarch= isle2arch(finisle); \ int midarch= route2midarch(ports,nports); \ - fprintf(stderr, \ + 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