# HG changeset patch # User jbe # Date 1363548523 -3600 # Node ID fadfe4f267e296fd8b1f964d099c32bf716a0830 # Parent 88322e31107be50eeaae5e57e08aa07a880a9ffe Optional logging in "lf_update_suggestion_order" diff -r 88322e31107b -r fadfe4f267e2 lf_update_suggestion_order.c --- a/lf_update_suggestion_order.c Sun Mar 17 20:06:30 2013 +0100 +++ b/lf_update_suggestion_order.c Sun Mar 17 20:28:43 2013 +0100 @@ -4,6 +4,8 @@ #include #include +static int logging = 0; + static char *escapeLiteral(PGconn *conn, const char *str, size_t len) { // provides compatibility for PostgreSQL versions prior 9.0 // in future: return PQescapeLiteral(conn, str, len); @@ -267,12 +269,12 @@ // trivial case, when there are no tuples: if (!tuple_count) { if (final) { - printf("No suggestions found, but marking initiative as finally calculated.\n"); + if (logging) printf("No suggestions found, but marking initiative as finally calculated.\n"); err = write_ranks(db, escaped_initiative_id, final); - printf("Done.\n"); + if (logging) printf("Done.\n"); return err; } else { - printf("Nothing to do.\n"); + if (logging) printf("Nothing to do.\n"); return 0; } } @@ -292,7 +294,7 @@ old_member_id = member_id; } // print candidate count: - printf("Candidate count: %i\n", candidate_count); + if (logging) printf("Candidate count: %i\n", candidate_count); // allocate memory for candidates[] array: candidates = malloc(candidate_count * sizeof(struct candidate)); if (!candidates) { @@ -305,7 +307,7 @@ // free memory of tree structure (tdestroy() is not available on all platforms): while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)compare_id); // print ballot count: - printf("Ballot count: %i\n", ballot_count); + if (logging) printf("Ballot count: %i\n", ballot_count); // allocate memory for ballots[] array: ballots = calloc(ballot_count, sizeof(struct ballot)); if (!ballots) { @@ -378,7 +380,7 @@ for (i=0; iseat = candidate_count - i; - printf("Assigning rank #%i to suggestion #%s.\n", candidate_count - i, candidate->key); + if (logging) printf("Assigning rank #%i to suggestion #%s.\n", candidate_count - i, candidate->key); } // free ballots[] array: @@ -394,12 +396,12 @@ // write results to database: if (final) { - printf("Writing final ranks to database.\n"); + if (logging) printf("Writing final ranks to database.\n"); } else { - printf("Writing ranks to database.\n"); + if (logging) printf("Writing ranks to database.\n"); } err = write_ranks(db, escaped_initiative_id, final); - printf("Done.\n"); + if (logging) printf("Done.\n"); // free candidates[] array: free(candidates); @@ -423,7 +425,7 @@ FILE *out; out = argc == 1 ? stderr : stdout; fprintf(out, "\n"); - fprintf(out, "Usage: %s \n", argv[0]); + fprintf(out, "Usage: %s [-v|--verbose] \n", argv[0]); fprintf(out, "\n"); fprintf(out, " is specified by PostgreSQL's libpq,\n"); fprintf(out, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n"); @@ -434,15 +436,23 @@ } { size_t len = 0; - for (i=1; i= 2 && + (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--verbose")) + ) { + argb = 2; + logging = 1; + } + for (i=argb; i1) strcat(conninfo, " "); + for (i=argb; iargb) strcat(conninfo, " "); strcat(conninfo, argv[i]); } } @@ -473,7 +483,7 @@ PQclear(res); } else { count = PQntuples(res); - printf("Number of initiatives to process: %i\n", count); + if (logging) printf("Number of initiatives to process: %i\n", count); for (i=0; i