liquid_feedback_core
changeset 374:fadfe4f267e2
Optional logging in "lf_update_suggestion_order"
author | jbe |
---|---|
date | Sun Mar 17 20:28:43 2013 +0100 (2013-03-17) |
parents | 88322e31107b |
children | 3f7a89ad996d |
files | lf_update_suggestion_order.c |
line diff
1.1 --- a/lf_update_suggestion_order.c Sun Mar 17 20:06:30 2013 +0100 1.2 +++ b/lf_update_suggestion_order.c Sun Mar 17 20:28:43 2013 +0100 1.3 @@ -4,6 +4,8 @@ 1.4 #include <libpq-fe.h> 1.5 #include <search.h> 1.6 1.7 +static int logging = 0; 1.8 + 1.9 static char *escapeLiteral(PGconn *conn, const char *str, size_t len) { 1.10 // provides compatibility for PostgreSQL versions prior 9.0 1.11 // in future: return PQescapeLiteral(conn, str, len); 1.12 @@ -267,12 +269,12 @@ 1.13 // trivial case, when there are no tuples: 1.14 if (!tuple_count) { 1.15 if (final) { 1.16 - printf("No suggestions found, but marking initiative as finally calculated.\n"); 1.17 + if (logging) printf("No suggestions found, but marking initiative as finally calculated.\n"); 1.18 err = write_ranks(db, escaped_initiative_id, final); 1.19 - printf("Done.\n"); 1.20 + if (logging) printf("Done.\n"); 1.21 return err; 1.22 } else { 1.23 - printf("Nothing to do.\n"); 1.24 + if (logging) printf("Nothing to do.\n"); 1.25 return 0; 1.26 } 1.27 } 1.28 @@ -292,7 +294,7 @@ 1.29 old_member_id = member_id; 1.30 } 1.31 // print candidate count: 1.32 - printf("Candidate count: %i\n", candidate_count); 1.33 + if (logging) printf("Candidate count: %i\n", candidate_count); 1.34 // allocate memory for candidates[] array: 1.35 candidates = malloc(candidate_count * sizeof(struct candidate)); 1.36 if (!candidates) { 1.37 @@ -305,7 +307,7 @@ 1.38 // free memory of tree structure (tdestroy() is not available on all platforms): 1.39 while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)compare_id); 1.40 // print ballot count: 1.41 - printf("Ballot count: %i\n", ballot_count); 1.42 + if (logging) printf("Ballot count: %i\n", ballot_count); 1.43 // allocate memory for ballots[] array: 1.44 ballots = calloc(ballot_count, sizeof(struct ballot)); 1.45 if (!ballots) { 1.46 @@ -378,7 +380,7 @@ 1.47 for (i=0; i<candidate_count; i++) { 1.48 struct candidate *candidate = loser(i, ballots, ballot_count); 1.49 candidate->seat = candidate_count - i; 1.50 - printf("Assigning rank #%i to suggestion #%s.\n", candidate_count - i, candidate->key); 1.51 + if (logging) printf("Assigning rank #%i to suggestion #%s.\n", candidate_count - i, candidate->key); 1.52 } 1.53 1.54 // free ballots[] array: 1.55 @@ -394,12 +396,12 @@ 1.56 1.57 // write results to database: 1.58 if (final) { 1.59 - printf("Writing final ranks to database.\n"); 1.60 + if (logging) printf("Writing final ranks to database.\n"); 1.61 } else { 1.62 - printf("Writing ranks to database.\n"); 1.63 + if (logging) printf("Writing ranks to database.\n"); 1.64 } 1.65 err = write_ranks(db, escaped_initiative_id, final); 1.66 - printf("Done.\n"); 1.67 + if (logging) printf("Done.\n"); 1.68 1.69 // free candidates[] array: 1.70 free(candidates); 1.71 @@ -423,7 +425,7 @@ 1.72 FILE *out; 1.73 out = argc == 1 ? stderr : stdout; 1.74 fprintf(out, "\n"); 1.75 - fprintf(out, "Usage: %s <conninfo>\n", argv[0]); 1.76 + fprintf(out, "Usage: %s [-v|--verbose] <conninfo>\n", argv[0]); 1.77 fprintf(out, "\n"); 1.78 fprintf(out, "<conninfo> is specified by PostgreSQL's libpq,\n"); 1.79 fprintf(out, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n"); 1.80 @@ -434,15 +436,23 @@ 1.81 } 1.82 { 1.83 size_t len = 0; 1.84 - for (i=1; i<argc; i++) len += strlen(argv[i]) + 1; 1.85 + int argb = 1; 1.86 + if ( 1.87 + argc >= 2 && 1.88 + (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--verbose")) 1.89 + ) { 1.90 + argb = 2; 1.91 + logging = 1; 1.92 + } 1.93 + for (i=argb; i<argc; i++) len += strlen(argv[i]) + 1; 1.94 conninfo = malloc(len * sizeof(char)); 1.95 if (!conninfo) { 1.96 fprintf(stderr, "Error: Could not allocate memory for conninfo string.\n"); 1.97 abort(); 1.98 } 1.99 conninfo[0] = 0; 1.100 - for (i=1; i<argc; i++) { 1.101 - if (i>1) strcat(conninfo, " "); 1.102 + for (i=argb; i<argc; i++) { 1.103 + if (i>argb) strcat(conninfo, " "); 1.104 strcat(conninfo, argv[i]); 1.105 } 1.106 } 1.107 @@ -473,7 +483,7 @@ 1.108 PQclear(res); 1.109 } else { 1.110 count = PQntuples(res); 1.111 - printf("Number of initiatives to process: %i\n", count); 1.112 + if (logging) printf("Number of initiatives to process: %i\n", count); 1.113 for (i=0; i<count; i++) { 1.114 char *initiative_id, *escaped_initiative_id; 1.115 int final; 1.116 @@ -481,7 +491,7 @@ 1.117 PGresult *res2; 1.118 initiative_id = PQgetvalue(res, i, 0); 1.119 final = (PQgetvalue(res, i, 1)[0] == 't') ? 1 : 0; 1.120 - printf("Processing initiative_id: %s\n", initiative_id); 1.121 + if (logging) printf("Processing initiative_id: %s\n", initiative_id); 1.122 escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id)); 1.123 if (!escaped_initiative_id) { 1.124 fprintf(stderr, "Could not escape literal in memory.\n"); 1.125 @@ -515,8 +525,11 @@ 1.126 1.127 // cleanup and exit 1.128 PQfinish(db); 1.129 - if (!err) printf("Successfully terminated.\n"); 1.130 - else fprintf(stderr, "Exiting with error code #%i.\n", err); 1.131 + if (!err) { 1.132 + if (logging) printf("Successfully terminated.\n"); 1.133 + } else { 1.134 + fprintf(stderr, "Exiting with error code %i.\n", err); 1.135 + } 1.136 return err; 1.137 1.138 }