[med-svn] [htslib] 06/10: Replaced fprintf(stderr, ...) calls with hts_log_<level> calls.

Andreas Tille tille at debian.org
Wed Jul 19 19:54:43 UTC 2017


This is an automated email from the git hooks/post-receive script.

tille pushed a commit to branch master
in repository htslib.

commit 5c155810344f86948980c2109241410772ceb83e
Author: Anders Kaplan <anders.kaplan at magasin1.se>
Date:   Mon Jul 3 22:31:52 2017 +0200

    Replaced fprintf(stderr, ...) calls with hts_log_<level> calls.
---
 cram/cram_codecs.c   | 26 +++++++++---------
 cram/cram_decode.c   | 54 +++++++++++++++++--------------------
 cram/cram_encode.c   | 47 +++++++++++++-------------------
 cram/cram_external.c |  3 +--
 cram/cram_index.c    |  2 +-
 cram/cram_io.c       | 76 ++++++++++++++++++++++++----------------------------
 cram/cram_stats.c    |  4 +--
 cram/mFILE.c         |  3 ++-
 cram/sam_header.c    |  3 ++-
 9 files changed, 100 insertions(+), 118 deletions(-)

diff --git a/cram/cram_codecs.c b/cram/cram_codecs.c
index 332085a..4fe9574 100644
--- a/cram/cram_codecs.c
+++ b/cram/cram_codecs.c
@@ -558,7 +558,7 @@ cram_codec *cram_beta_decode_init(char *data, int size,
     else if (option == E_BYTE_ARRAY || option == E_BYTE)
 	c->decode = cram_beta_decode_char;
     else {
-	fprintf(stderr, "BYTE_ARRAYs not supported by this codec\n");
+	hts_log_error("BYTE_ARRAYs not supported by this codec");
 	return NULL;
     }
     c->free   = cram_beta_decode_free;
@@ -748,7 +748,7 @@ cram_codec *cram_subexp_decode_init(char *data, int size,
     char *cp = data;
 
     if (option != E_INT) {
-	fprintf(stderr, "This codec only supports INT encodings\n");
+	hts_log_error("This codec only supports INT encodings");
 	return NULL;
     }
 
@@ -764,7 +764,7 @@ cram_codec *cram_subexp_decode_init(char *data, int size,
     cp += safe_itf8_get(cp, data + size, &c->subexp.k);
 
     if (cp - data != size || c->subexp.k < 0) {
-	fprintf(stderr, "Malformed subexp header stream\n");
+	hts_log_error("Malformed subexp header stream");
 	free(c);
 	return NULL;
     }
@@ -814,7 +814,7 @@ cram_codec *cram_gamma_decode_init(char *data, int size,
     char *cp = data;
 
     if (option != E_INT) {
-	fprintf(stderr, "This codec only supports INT encodings\n");
+	hts_log_error("This codec only supports INT encodings");
 	return NULL;
     }
 
@@ -985,13 +985,13 @@ cram_codec *cram_huffman_decode_init(char *data, int size,
     int l;
 
     if (option == E_BYTE_ARRAY_BLOCK) {
-	fprintf(stderr, "BYTE_ARRAYs not supported by this codec\n");
+	hts_log_error("BYTE_ARRAYs not supported by this codec");
 	return NULL;
     }
 
     cp += safe_itf8_get(cp, data_end, &ncodes);
     if (ncodes < 0) {
-        fprintf(stderr, "Invalid number of symbols in huffman stream\n");
+	hts_log_error("Invalid number of symbols in huffman stream");
         return NULL;
     }
     if (ncodes >= SIZE_MAX / sizeof(*codes)) {
@@ -1023,13 +1023,13 @@ cram_codec *cram_huffman_decode_init(char *data, int size,
     }
 
     if (l < 1) {
-	fprintf(stderr, "Malformed huffman header stream\n");
+	hts_log_error("Malformed huffman header stream");
 	free(h);
 	return NULL;
     }
     cp += safe_itf8_get(cp, data_end, &i);
     if (i != ncodes) {
-	fprintf(stderr, "Malformed huffman header stream\n");
+	hts_log_error("Malformed huffman header stream");
 	free(h);
 	return NULL;
     }
@@ -1051,7 +1051,7 @@ cram_codec *cram_huffman_decode_init(char *data, int size,
 	    max_len = codes[i].len;
     }
     if (l < 1 || cp - data != size || max_len >= ncodes) {
-	fprintf(stderr, "Malformed huffman header stream\n");
+	hts_log_error("Malformed huffman header stream");
 	free(h);
 	return NULL;
     }
@@ -1511,7 +1511,7 @@ cram_codec *cram_byte_array_len_decode_init(char *data, int size,
     return c;
 
  malformed:
-    fprintf(stderr, "Malformed byte_array_len header stream\n");
+    hts_log_error("Malformed byte_array_len header stream");
  no_codec:
     free(c);
     return NULL;
@@ -1855,7 +1855,7 @@ cram_codec *cram_decoder_init(enum cram_encoding codec,
     if (codec >= E_NULL && codec < E_NUM_CODECS && decode_init[codec]) {
 	return decode_init[codec](data, size, option, version);
     } else {
-	fprintf(stderr, "Unimplemented codec of type %s\n", cram_encoding2str(codec));
+	hts_log_error("Unimplemented codec of type %s", cram_encoding2str(codec));
 	return NULL;
     }
 }
@@ -1890,7 +1890,7 @@ cram_codec *cram_encoder_init(enum cram_encoding codec,
 	    r->out = NULL;
 	return r;
     } else {
-	fprintf(stderr, "Unimplemented codec of type %s\n", cram_encoding2str(codec));
+	hts_log_error("Unimplemented codec of type %s", cram_encoding2str(codec));
 	abort();
     }
 }
@@ -1928,7 +1928,7 @@ int cram_codec_to_id(cram_codec *c, int *id2) {
 	bnum1 = -2;
 	break;
     default:
-	fprintf(stderr, "Unknown codec type %d\n", c->codec);
+	hts_log_error("Unknown codec type %d", c->codec);
 	bnum1 = -1;
     }
 
diff --git a/cram/cram_decode.c b/cram/cram_decode.c
index bb7bf48..513cb51 100644
--- a/cram/cram_decode.c
+++ b/cram/cram_decode.c
@@ -334,8 +334,7 @@ cram_block_compression_hdr *cram_decode_compression_header(cram_fd *fd,
 	}
 
 	default:
-	    fprintf(stderr, "Unrecognised preservation map key %c%c\n",
-		    cp[-2], cp[-1]);
+	    hts_log_warning("Unrecognised preservation map key %c%c", cp[-2], cp[-1]);
 	    // guess byte;
 	    cp++;
 	    break;
@@ -605,8 +604,9 @@ cram_block_compression_hdr *cram_decode_compression_header(cram_fd *fd,
 	    }
 	} else if (key[0] == 'T' && key[1] == 'M') {
 	} else if (key[0] == 'T' && key[1] == 'V') {
-	} else
-	    fprintf(stderr, "Unrecognised key: %.2s\n", key);
+	} else {
+	    hts_log_warning("Unrecognised key: %.2s", key);
+	}
 
 	cp += size;
 
@@ -1274,8 +1274,7 @@ static int cram_decode_seq(cram_fd *fd, cram_container *c, cram_slice *s,
 	pos += prev_pos;
 
 	if (pos <= 0) {
-	    fprintf(stderr, "Error: feature position %d before start of read.\n",
-		    pos);
+	    hts_log_error("Feature position %d before start of read", pos);
 	    return -1;
 	}
 
@@ -1288,8 +1287,7 @@ static int cram_decode_seq(cram_fd *fd, cram_container *c, cram_slice *s,
 		    static int whinged = 0;
 		    int rlen;
 		    if (!whinged)
-			fprintf(stderr, "Ref pos outside of ref "
-				"sequence boundary\n");
+			hts_log_warning("Ref pos outside of ref sequence boundary");
 		    whinged = 1;
 		    rlen = bfd->ref[cr->ref_id].len - ref_pos;
 		    // May miss MD/NM cases where both seq/ref are N, but this is a
@@ -1743,7 +1741,7 @@ static int cram_decode_seq(cram_fd *fd, cram_container *c, cram_slice *s,
 	}
 
 	default:
-            fprintf(stderr, "Error: Unknown feature code '%c'\n", op);
+	    hts_log_error("Unknown feature code '%c'", op);
 	    return -1;
 	}
     }
@@ -1758,7 +1756,7 @@ static int cram_decode_seq(cram_fd *fd, cram_container *c, cram_slice *s,
 		static int whinged = 0;
 		int rlen;
 		if (!whinged)
-		    fprintf(stderr, "Ref pos outside of ref sequence boundary\n");
+		    hts_log_warning("Ref pos outside of ref sequence boundary");
 		whinged = 1;
 		rlen = bfd->ref[cr->ref_id].len - ref_pos;
 		// May miss MD/NM cases where both seq/ref are N, but this is a
@@ -2142,7 +2140,7 @@ static int cram_decode_slice_xref(cram_slice *s, int required_fields) {
 		if (s->crecs[cr->mate_line].flags & BAM_FREVERSE)
 		    cr->flags |= BAM_FMREVERSE;
 	    } else {
-		fprintf(stderr, "Mate line out of bounds: %d vs [0, %d]\n",
+		hts_log_error("Mate line out of bounds: %d vs [0, %d]",
 			cr->mate_line, s->hdr->num_records-1);
 	    }
 
@@ -2267,8 +2265,7 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 	if (embed_ref) {
 	    cram_block *b;
 	    if (s->hdr->ref_base_id < 0) {
-		fprintf(stderr, "No reference specified and "
-			"no embedded reference is available.\n");
+		hts_log_error("No reference specified and no embedded reference is available");
 		return -1;
 	    }
 	    b = cram_get_block_by_id(s, s->hdr->ref_base_id);
@@ -2280,7 +2277,7 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 	    s->ref_start = s->hdr->ref_seq_start;
 	    s->ref_end   = s->hdr->ref_seq_start + s->hdr->ref_seq_span-1;
 	    if (s->ref_end - s->ref_start > b->uncomp_size) {
-		fprintf(stderr, "Embedded reference is too small.\n");
+		hts_log_error("Embedded reference is too small");
 		return -1;
 	    }
 	} else if (!fd->no_ref) {
@@ -2298,7 +2295,7 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 
 	    /* Sanity check */
 	    if (s->ref_start < 0) {
-		fprintf(stderr, "Slice starts before base 1.\n");
+		hts_log_warning("Slice starts before base 1");
 		s->ref_start = 0;
 	    }
 	    pthread_mutex_lock(&fd->ref_lock);
@@ -2315,7 +2312,7 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 
     if ((fd->required_fields & SAM_SEQ) &&
 	s->ref == NULL && s->hdr->ref_seq_id >= 0 && !fd->no_ref) {
-	fprintf(stderr, "Unable to fetch reference #%d %d..%d\n",
+	hts_log_error("Unable to fetch reference #%d %d..%d",
 		s->hdr->ref_seq_id, s->hdr->ref_seq_start,
 		s->hdr->ref_seq_start + s->hdr->ref_seq_span-1);
 	return -1;
@@ -2335,14 +2332,14 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 	    if (s->hdr->ref_seq_start >= s->ref_start) {
 		start = s->hdr->ref_seq_start - s->ref_start;
 	    } else {
-		fprintf(stderr, "Slice starts before base 1.\n");
+		hts_log_warning("Slice starts before base 1");
 		start = 0;
 	    }
 
 	    if (s->hdr->ref_seq_span <= s->ref_end - s->ref_start + 1) {
 		len = s->hdr->ref_seq_span;
 	    } else {
-		fprintf(stderr, "Slice ends beyond reference end.\n");
+		hts_log_warning("Slice ends beyond reference end");
 		len = s->ref_end - s->ref_start + 1;
 	    }
 
@@ -2368,10 +2365,10 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 	if ((!s->ref && s->hdr->ref_base_id < 0)
 	    || memcmp(digest, s->hdr->md5, 16) != 0) {
 	    char M[33];
-	    fprintf(stderr, "ERROR: md5sum reference mismatch for ref "
-		    "%d pos %d..%d\n", ref_id, s->ref_start, s->ref_end);
-	    fprintf(stderr, "CRAM: %s\n", md5_print(s->hdr->md5, M));
-	    fprintf(stderr, "Ref : %s\n", md5_print(digest, M));
+	    hts_log_error("MD5 checksum reference mismatch for ref %d pos %d..%d",
+		ref_id, s->ref_start, s->ref_end);
+	    hts_log_error("CRAM: %s", md5_print(s->hdr->md5, M));
+	    hts_log_error("Ref : %s", md5_print(digest, M));
 	    return -1;
 	}
     }
@@ -2468,7 +2465,7 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 	    cr->ref_id = ref_id; // Forced constant in CRAM 1.0
 	}
 	if (cr->ref_id < -1 || cr->ref_id >= bfd->nref) {
-	    fprintf(stderr, "Requested unknown reference ID %d\n", cr->ref_id);
+	    hts_log_error("Requested unknown reference ID %d", cr->ref_id);
             return -1;
 	}
 
@@ -2479,7 +2476,7 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 				     (char *)&cr->len, &out_sz);
 	    if (r) return r;
 	    if (cr->len < 0) {
-	        fprintf(stderr, "Read has negative length\n");
+		hts_log_error("Read has negative length");
 		return -1;
 	    }
 	}
@@ -2666,8 +2663,7 @@ int cram_decode_slice(cram_fd *fd, cram_container *c, cram_slice *s,
 
 	if (!(bf & BAM_FUNMAP)) {
             if ((ds & CRAM_AP) && cr->apos <= 0) {
-                fprintf(stderr,
-			"Read has alignment position %d but no unmapped flag\n",
+		hts_log_error("Read has alignment position %d but no unmapped flag",
 			cr->apos);
 		return -1;
 	    }
@@ -3106,7 +3102,7 @@ static cram_slice *cram_next_slice(cram_fd *fd, cram_container **cp) {
 
 	if (cram_decode_slice_mt(fd, c, s, fd->header) != 0) {
 	    //	if (cram_decode_slice(fd, c, s, fd->header) != 0) {
-	    fprintf(stderr, "Failure to decode slice\n");
+	    hts_log_error("Failure to decode slice");
 	    cram_free_slice(s);
 	    c->slice = NULL;
 	    return NULL;
@@ -3135,7 +3131,7 @@ static cram_slice *cram_next_slice(cram_fd *fd, cram_container **cp) {
 	res = hts_tpool_next_result_wait(fd->rqueue);
 
 	if (!res || !hts_tpool_result_data(res)) {
-	    fprintf(stderr, "hts_tpool_next_result failure\n");
+	    hts_log_error("Call to hts_tpool_next_result failed");
 	    return NULL;
 	}
 
@@ -3144,7 +3140,7 @@ static cram_slice *cram_next_slice(cram_fd *fd, cram_container **cp) {
 	s = j->s;
 
 	if (j->exit_code != 0) {
-	    fprintf(stderr, "Slice decode failure\n");
+	    hts_log_error("Slice decode failure");
 	    fd->eof = 0;
 	    hts_tpool_delete_result(res, 1);
 	    return NULL;
diff --git a/cram/cram_encode.c b/cram/cram_encode.c
index 1d9f70c..e667d21 100644
--- a/cram/cram_encode.c
+++ b/cram/cram_encode.c
@@ -239,7 +239,7 @@ cram_block *cram_encode_compression_header(cram_fd *fd, cram_container *c,
 	    }
 
 	    default:
-		fprintf(stderr, "Unknown preservation key '%.2s'\n", key);
+		hts_log_warning("Unknown preservation key '%.2s'", key);
 		break;
 	    }
 
@@ -500,7 +500,7 @@ cram_block *cram_encode_compression_header(cram_fd *fd, cram_container *c,
     BLOCK_APPEND(cb, BLOCK_DATA(map), BLOCK_SIZE(map));
 
     if (fd->verbose)
-	fprintf(stderr, "Wrote compression block header in %d bytes\n",
+	hts_log_info("Wrote compression block header in %d bytes",
 		(int)BLOCK_SIZE(cb));
 
     BLOCK_UPLEN(cb);
@@ -746,8 +746,7 @@ static int cram_encode_slice_read(cram_fd *fd,
 		    
 
 	    default:
-		fprintf(stderr, "unhandled feature code %c\n",
-			f->X.code);
+		hts_log_error("Unhandled feature code %c", f->X.code);
 		return -1;
 	    }
 	}
@@ -1278,7 +1277,7 @@ int cram_encode_container(cram_fd *fd, cram_container *c) {
 
 	ref = cram_get_ref(fd, bam_ref(b), 1, 0);
 	if (!ref && bam_ref(b) >= 0) {
-	    fprintf(stderr, "Failed to load reference #%d\n", bam_ref(b));
+	    hts_log_error("Failed to load reference #%d", bam_ref(b));
 	    return -1;
 	}
 	if ((c->ref_id = bam_ref(b)) >= 0) {
@@ -1322,8 +1321,7 @@ int cram_encode_container(cram_fd *fd, cram_container *c) {
 			cram_ref_decr(fd->refs, c->ref_seq_id);
 
 		    if (!cram_get_ref(fd, bam_ref(b), 1, 0)) {
-			fprintf(stderr, "Failed to load reference #%d\n",
-				bam_ref(b));
+			hts_log_error("Failed to load reference #%d", bam_ref(b));
 			return -1;
 		    }
 
@@ -1408,7 +1406,7 @@ int cram_encode_container(cram_fd *fd, cram_container *c) {
 
     if (multi_ref) {
 	if (fd->verbose)
-	    fprintf(stderr, "Multi-ref container\n");
+	    hts_log_info("Multi-ref container");
 	c->ref_seq_id = -2;
 	c->ref_seq_start = 0;
 	c->ref_seq_span = 0;
@@ -1643,7 +1641,7 @@ int cram_encode_container(cram_fd *fd, cram_container *c) {
     /* Encode slices */
     for (i = 0; i < c->curr_slice; i++) {
 	if (fd->verbose)
-	    fprintf(stderr, "Encode slice %d\n", i);
+	    hts_log_info("Encode slice %d", i);
 
 	if (cram_encode_slice(fd, c, h, c->slices[i]) != 0)
 	    return -1;
@@ -1970,7 +1968,7 @@ static char *cram_encode_aux_1_0(cram_fd *fd, bam_seq_t *b, cram_container *c,
 	    case 'A': case 'C': case 'c': aux+=4; break;
 	    case 'I': case 'i': case 'f': aux+=7; break;
 	    default:
-		fprintf(stderr, "Unhandled type code for NM tag\n");
+		hts_log_error("Unhandled type code for NM tag");
 		return NULL;
 	    }
 	    continue;
@@ -2040,10 +2038,8 @@ static char *cram_encode_aux_1_0(cram_fd *fd, bam_seq_t *b, cram_container *c,
 		blen = 4*count;
 		break;
 	    default:
-		fprintf(stderr, "Unknown sub-type '%c' for aux type 'B'\n",
-			type);
+		hts_log_error("Unknown sub-type '%c' for aux type 'B'", type);
 		return NULL;
-		    
 	    }
 
 	    tmp += itf8_put(tmp, blen+5);
@@ -2058,7 +2054,7 @@ static char *cram_encode_aux_1_0(cram_fd *fd, bam_seq_t *b, cram_container *c,
 	    break;
 	}
 	default:
-	    fprintf(stderr, "Unknown aux type '%c'\n", aux[2]);
+	    hts_log_error("Unknown aux type '%c'", aux[2]);
 	    return NULL;
 	}
     }
@@ -2117,7 +2113,7 @@ static char *cram_encode_aux(cram_fd *fd, bam_seq_t *b, cram_container *c,
 		case 'S': case 's':           aux+=5; break;
 		case 'I': case 'i': case 'f': aux+=7; break;
 		default:
-		    fprintf(stderr, "Unhandled type code for NM tag\n");
+		    hts_log_error("Unhandled type code for NM tag");
 		    return NULL;
 		}
 		continue;
@@ -2243,8 +2239,7 @@ static char *cram_encode_aux(cram_fd *fd, bam_seq_t *b, cram_container *c,
 	    }
 
 	    default:
-		fprintf(stderr, "Unsupported SAM aux type '%c'\n",
-			aux[2]);
+		hts_log_error("Unsupported SAM aux type '%c'", aux[2]);
 		c = NULL;
 	    }
 
@@ -2355,10 +2350,8 @@ static char *cram_encode_aux(cram_fd *fd, bam_seq_t *b, cram_container *c,
 		blen = 4*count;
 		break;
 	    default:
-		fprintf(stderr, "Unknown sub-type '%c' for aux type 'B'\n",
-			type);
+		hts_log_error("Unknown sub-type '%c' for aux type 'B'", type);
 		return NULL;
-		    
 	    }
 
 	    blen += 5; // sub-type & length
@@ -2368,7 +2361,7 @@ static char *cram_encode_aux(cram_fd *fd, bam_seq_t *b, cram_container *c,
 	    break;
 	}
 	default:
-	    fprintf(stderr, "Unknown aux type '%c'\n", aux[2]);
+	    hts_log_error("Unknown aux type '%c'", aux[2]);
 	    return NULL;
 	}
 	tm->blk->m = tm->m;
@@ -2453,7 +2446,7 @@ static cram_container *cram_next_container(cram_fd *fd, bam_seq_t *b) {
 	(bam_ref(b) != c->curr_ref && !c->multi_seq)) {
 	c->ref_seq_span = fd->last_base - c->ref_seq_start + 1;
 	if (fd->verbose)
-	    fprintf(stderr, "Flush container %d/%d..%d\n",
+	    hts_log_info("Flush container %d/%d..%d",
 		    c->ref_seq_id, c->ref_seq_start,
 		    c->ref_seq_start + c->ref_seq_span -1);
 
@@ -2697,8 +2690,7 @@ static int process_one_read(cram_fd *fd, cram_container *c,
 		    char *rp = &ref[apos];
 		    char *qp = &qual[spos];
 		    if (end > cr->len) {
-			fprintf(stderr, "CIGAR and query sequence are of "
-				"different length\n");
+			hts_log_error("CIGAR and query sequence are of different length");
 			return -1;
 		    }
 		    for (l = 0; l < end; l++) {
@@ -2822,13 +2814,12 @@ static int process_one_read(cram_fd *fd, cram_container *c,
 		break;
 
 	    default:
-		fprintf(stderr, "Unknown CIGAR op code %d\n", cig_op);
+		hts_log_error("Unknown CIGAR op code %d", cig_op);
 		return -1;
 	    }
 	}
 	if (cr->len && spos != cr->len) {
-	    fprintf(stderr, "CIGAR and query sequence are of different "
-		    "length\n");
+	    hts_log_error("CIGAR and query sequence are of different length");
 	    return -1;
 	}
 	fake_qual = spos;
@@ -3095,7 +3086,7 @@ int cram_put_bam_seq(cram_fd *fd, bam_seq_t *b) {
 	    fd->last_slice && fd->last_slice < c->max_rec/4+10 &&
 	    !fd->embed_ref) {
 	    if (fd->verbose && !c->multi_seq)
-		fprintf(stderr, "Multi-ref enabled for this container\n");
+		hts_log_info("Multi-ref enabled for this container");
 	    multi_seq = 1;
 	}
 
diff --git a/cram/cram_external.c b/cram/cram_external.c
index be1dc7b..8d87f58 100644
--- a/cram/cram_external.c
+++ b/cram/cram_external.c
@@ -301,8 +301,7 @@ int cram_transcode_rg(cram_fd *in, cram_fd *out,
     cram_block_compression_hdr *ch;
 
     if (nrg != 1) {
-	fprintf(stderr, "[%s] ERROR: not implemented for nrg != 1\n",
-		__func__);
+	hts_log_error("CRAM transcode supports only a single RG");
 	return -2;
     }
 
diff --git a/cram/cram_index.c b/cram/cram_index.c
index 90d7bea..9f65046 100644
--- a/cram/cram_index.c
+++ b/cram/cram_index.c
@@ -232,7 +232,7 @@ int cram_index_load(cram_fd *fd, const char *fn, const char *fn_idx) {
 	//printf("%d/%d..%d\n", e.refid, e.start, e.end);
 
 	if (e.refid < -1) {
-	    fprintf(stderr, "Malformed index file, refid %d\n", e.refid);
+	    hts_log_error("Malformed index file, refid %d", e.refid);
             goto fail;
 	}
 
diff --git a/cram/cram_io.c b/cram/cram_io.c
index e2b416f..d41e76c 100644
--- a/cram/cram_io.c
+++ b/cram/cram_io.c
@@ -554,7 +554,7 @@ char *zlib_mem_inflate(char *cdata, size_t csize, size_t *size) {
     //err = inflateInit(&s);
     err = inflateInit2(&s, 15 + 32);
     if (err != Z_OK) {
-	fprintf(stderr, "zlib inflateInit error: %s\n", s.msg);
+	hts_log_error("Call to zlib inflateInit failed: %s", s.msg);
 	free(data);
 	return NULL;
     }
@@ -570,7 +570,7 @@ char *zlib_mem_inflate(char *cdata, size_t csize, size_t *size) {
 	    break;
 
 	if (err != Z_OK) {
-	    fprintf(stderr, "zlib inflate error: %s\n", s.msg);
+	    hts_log_error("Call to zlib inflate failed: %s", s.msg);
 	    if (data)
 		free(data);
 	    return NULL;
@@ -618,7 +618,7 @@ static char *zlib_mem_deflate(char *data, size_t size, size_t *cdata_size,
 
     err = deflateInit2(&s, level, Z_DEFLATED, 15|16, 9, strat);
     if (err != Z_OK) {
-	fprintf(stderr, "zlib deflateInit2 error: %s\n", s.msg);
+	hts_log_error("Call to zlib deflateInit2 failed: %s", s.msg);
 	return NULL;
     }
 
@@ -627,23 +627,23 @@ static char *zlib_mem_deflate(char *data, size_t size, size_t *cdata_size,
 	s.next_out = &cdata[cdata_pos];
 	s.avail_out = cdata_alloc - cdata_pos;
 	if (cdata_alloc - cdata_pos <= 0) {
-	    fprintf(stderr, "Deflate produced larger output than expected. Abort\n"); 
+	    hts_log_error("Deflate produced larger output than expected");
 	    return NULL;
 	}
 	err = deflate(&s, Z_NO_FLUSH);
 	cdata_pos = cdata_alloc - s.avail_out;
 	if (err != Z_OK) {
-	    fprintf(stderr, "zlib deflate error: %s\n", s.msg);
+	    hts_log_error("Call to zlib deflate failed: %s", s.msg);
 	    break;
 	}
     }
     if (deflate(&s, Z_FINISH) != Z_STREAM_END) {
-	fprintf(stderr, "zlib deflate error: %s\n", s.msg);
+	hts_log_error("Call to zlib deflate failed: %s", s.msg);
     }
     *cdata_size = s.total_out;
 
     if (deflateEnd(&s) != Z_OK) {
-	fprintf(stderr, "zlib deflate error: %s\n", s.msg);
+	hts_log_error("Call to zlib deflate failed: %s", s.msg);
     }
     return (char *)cdata;
 }
@@ -705,7 +705,7 @@ static char *lzma_mem_inflate(char *cdata, size_t csize, size_t *size) {
 
 	r = lzma_code(&strm, LZMA_RUN);
 	if (LZMA_OK != r && LZMA_STREAM_END != r) {
-	    fprintf(stderr, "[E::%s] LZMA decode failure (error %d)\n", __func__, r);
+	    hts_log_error("LZMA decode failure (error %d)", r);
 	    return NULL;
 	}
 
@@ -718,7 +718,7 @@ static char *lzma_mem_inflate(char *cdata, size_t csize, size_t *size) {
     /* finish up any unflushed data; necessary? */
     r = lzma_code(&strm, LZMA_FINISH);
     if (r != LZMA_OK && r != LZMA_STREAM_END) {
-	fprintf(stderr, "r=%d\n", r);
+	hts_log_error("Call to lzma_code failed with error %d", r);
 	return NULL;
     }
 
@@ -817,7 +817,7 @@ cram_block *cram_read_block(cram_fd *fd) {
 
 	crc = crc32(crc, b->data ? b->data : (uc *)"", b->alloc);
 	if (crc != b->crc32) {
-	    fprintf(stderr, "Block CRC32 failure\n");
+	    hts_log_error("Block CRC32 failure");
 	    free(b->data);
 	    free(b);
 	    return NULL;
@@ -966,8 +966,7 @@ int cram_uncompress_block(cram_block *b) {
     }
 #else
     case BZIP2:
-	fprintf(stderr, "Bzip2 compression is not compiled into this "
-		"version.\nPlease rebuild and try again.\n");
+	hts_log_error("Bzip2 compression is not compiled into this version. Please rebuild and try again");
 	return -1;
 #endif
 
@@ -985,8 +984,7 @@ int cram_uncompress_block(cram_block *b) {
 	break;
 #else
     case LZMA:
-	fprintf(stderr, "Lzma compression is not compiled into this "
-		"version.\nPlease rebuild and try again.\n");
+	hts_log_error("Lzma compression is not compiled into this version. Please rebuild and try again");
 	return -1;
 	break;
 #endif
@@ -1404,7 +1402,7 @@ int cram_compress_block(cram_fd *fd, cram_block *b, cram_metrics *metrics,
 	comp = cram_compress_by_method((char *)b->data, b->uncomp_size,
 				       &comp_size, GZIP, level, Z_FILTERED);
 	if (!comp) {
-	    fprintf(stderr, "Compression failed!\n");
+	    hts_log_error("Compression failed");
 	    return -1;
 	}
 	free(b->data);
@@ -1414,7 +1412,7 @@ int cram_compress_block(cram_fd *fd, cram_block *b, cram_metrics *metrics,
     }
 
     if (fd->verbose)
-	fprintf(stderr, "Compressed block ID %d from %d to %d by method %s\n",
+	hts_log_info("Compressed block ID %d from %d to %d by method %s",
 		b->content_id, b->uncomp_size, b->comp_size,
 		cram_block_method2str(b->method));
 
@@ -1594,7 +1592,7 @@ static BGZF *bgzf_open_ref(char *fn, char *mode, int is_md5) {
     }
 
     if (fp->is_compressed == 1 && bgzf_index_load(fp, fn, ".gzi") < 0) {
-	fprintf(stderr, "Unable to load .gzi index '%s.gzi'\n", fn);
+	hts_log_error("Unable to load .gzi index '%s.gzi'", fn);
 	bgzf_close(fp);
 	return NULL;
     }
@@ -1780,8 +1778,7 @@ static void sanitise_SQ_lines(cram_fd *fd) {
 
 	    // Should we also check MD5sums here to ensure the correct
 	    // reference was given?
-	    fprintf(stderr, "WARNING: Header @SQ length mismatch for "
-		    "ref %s, %d vs %d\n",
+	    hts_log_warning("Header @SQ length mismatch for ref %s, %d vs %d",
 		    r->name, fd->header->ref[i].len, (int)r->length);
 
 	    // Fixing the parsed @SQ header will make MD:Z: strings work
@@ -1816,8 +1813,7 @@ int refs2id(refs_t *r, SAM_hdr *h) {
 	if (k != kh_end(r->h_meta)) {
 	    r->ref_id[i] = kh_val(r->h_meta, k);
 	} else {
-	    fprintf(stderr, "Unable to find ref name '%s'\n",
-		    h->ref[i].name);
+	    hts_log_warning("Unable to find ref name '%s'", h->ref[i].name);
 	}
     }
 
@@ -2026,7 +2022,7 @@ static int cram_populate_ref(cram_fd *fd, int id, ref_entry *r) {
     int local_path = 0;
 
     if (fd->verbose)
-	fprintf(stderr, "cram_populate_ref on fd %p, id %d\n", (void *)fd, id);
+	hts_log_info("Running cram_populate_ref on fd %p, id %d", (void *)fd, id);
 
     cache_root[0] = '\0';
 
@@ -2043,7 +2039,7 @@ static int cram_populate_ref(cram_fd *fd, int id, ref_entry *r) {
 	    snprintf(cache,PATH_MAX, "%s%s/hts-ref/%%2s/%%2s/%%s", base, extra);
 	    local_cache = cache;
 	    if (fd->verbose)
-		fprintf(stderr, "Populating local cache: %s\n", local_cache);
+		hts_log_info("Populating local cache: %s", local_cache);
 	}
     }
 
@@ -2057,7 +2053,7 @@ static int cram_populate_ref(cram_fd *fd, int id, ref_entry *r) {
 	goto no_M5;
 
     if (fd->verbose)
-	fprintf(stderr, "Querying ref %s\n", tag->str+3);
+	hts_log_info("Querying ref %s", tag->str+3);
 
     /* Use cache if available */
     if (local_cache && *local_cache) {
@@ -2169,7 +2165,7 @@ static int cram_populate_ref(cram_fd *fd, int id, ref_entry *r) {
 
 	expand_cache_path(path, local_cache, tag->str+3);
 	if (fd->verbose)
-	    fprintf(stderr, "Writing cache file '%s'\n", path);
+	    hts_log_info("Writing cache file '%s'", path);
 	mkdir_prefix(path, 01777);
 
 	do {
@@ -2203,7 +2199,7 @@ static int cram_populate_ref(cram_fd *fd, int id, ref_entry *r) {
 	hts_md5_hex(md5_buf2, md5_buf1);
 
 	if (strncmp(tag->str+3, md5_buf2, 32) != 0) {
-	    fprintf(stderr, "[E::%s] mismatching md5sum for downloaded reference.\n", __func__);
+	    hts_log_error("Mismatching md5sum for downloaded reference");
 	    hclose_abruptly(fp);
 	    unlink(path_tmp);
 	    return -1;
@@ -2330,7 +2326,7 @@ static char *load_ref_portion(BGZF *fp, ref_entry *e, int start, int end) {
 	cp_to = cp+j;
 
 	if (cp_to - seq != end-start+1) {
-	    fprintf(stderr, "Malformed reference file?\n");
+	    hts_log_error("Malformed reference file");
 	    free(seq);
 	    return NULL;
 	}
@@ -2463,19 +2459,19 @@ char *cram_get_ref(cram_fd *fd, int id, int start, int end) {
 
     /* Sanity checking: does this ID exist? */
     if (id >= fd->refs->nref) {
-	fprintf(stderr, "No reference found for id %d\n", id);
+	hts_log_error("No reference found for id %d", id);
 	pthread_mutex_unlock(&fd->ref_lock);
 	return NULL;
     }
 
     if (!fd->refs || !fd->refs->ref_id[id]) {
-	fprintf(stderr, "No reference found for id %d\n", id);
+	hts_log_error("No reference found for id %d", id);
 	pthread_mutex_unlock(&fd->ref_lock);
 	return NULL;
     }
 
     if (!(r = fd->refs->ref_id[id])) {
-	fprintf(stderr, "No reference found for id %d\n", id);
+	hts_log_error("No reference found for id %d", id);
 	pthread_mutex_unlock(&fd->ref_lock);
 	return NULL;
     }
@@ -2495,7 +2491,7 @@ char *cram_get_ref(cram_fd *fd, int id, int start, int end) {
     pthread_mutex_lock(&fd->refs->lock);
     if (r->length == 0) {
 	if (cram_populate_ref(fd, id, r) == -1) {
-	    fprintf(stderr, "Failed to populate reference for id %d\n", id);
+	    hts_log_error("Failed to populate reference for id %d", id);
 	    pthread_mutex_unlock(&fd->refs->lock);
 	    pthread_mutex_unlock(&fd->ref_lock);
 	    return NULL;
@@ -2874,7 +2870,7 @@ cram_container *cram_read_container(cram_fd *fd) {
 	    rd+=4;
 
 	if (crc != c->crc32) {
-	    fprintf(stderr, "Container header CRC32 failure\n");
+	    hts_log_error("Container header CRC32 failure");
 	    cram_free_container(c);
 	    return NULL;
 	}
@@ -3094,7 +3090,7 @@ void *cram_flush_thread(void *arg) {
 
     /* Encode the container blocks and generate compression header */
     if (0 != cram_encode_container(j->fd, j->c)) {
-	fprintf(stderr, "cram_encode_container failed\n");
+	hts_log_error("Call to cram_encode_container failed");
 	return NULL;
     }
 
@@ -3424,13 +3420,13 @@ cram_slice *cram_read_slice(cram_fd *fd) {
 	break;
 
     default:
-	fprintf(stderr, "Unexpected block of type %s\n",
+	hts_log_error("Unexpected block of type %s",
 		cram_content_type2str(b->content_type));
 	goto err;
     }
 
     if (s->hdr->num_blocks < 1) {
-        fprintf(stderr, "Slice does not include any data blocks.\n");
+	hts_log_error("Slice does not include any data blocks");
 	goto err;
     }
 
@@ -3514,8 +3510,7 @@ cram_file_def *cram_read_file_def(cram_fd *fd) {
     }
 
     if (def->major_version > 3) {
-	fprintf(stderr, "CRAM version number mismatch\n"
-		"Expected 1.x, 2.x or 3.x, got %d.%d\n",
+	hts_log_error("CRAM version number mismatch. Expected 1.x, 2.x or 3.x, got %d.%d",
 		def->major_version, def->minor_version);
 	free(def);
 	return NULL;
@@ -4416,14 +4411,13 @@ int cram_set_voption(cram_fd *fd, enum hts_fmt_option opt, va_list args) {
 	int major, minor;
 	char *s = va_arg(args, char *);
 	if (2 != sscanf(s, "%d.%d", &major, &minor)) {
-	    fprintf(stderr, "Malformed version string %s\n", s);
+	    hts_log_error("Malformed version string %s", s);
 	    return -1;
 	}
 	if (!((major == 1 &&  minor == 0) ||
 	      (major == 2 && (minor == 0 || minor == 1)) ||
 	      (major == 3 &&  minor == 0))) {
-	    fprintf(stderr, "Unknown version string; "
-		    "use 1.0, 2.0, 2.1 or 3.0\n");
+	    hts_log_error("Unknown version string; use 1.0, 2.0, 2.1 or 3.0");
 	    errno = EINVAL;
 	    return -1;
 	}
@@ -4483,7 +4477,7 @@ int cram_set_voption(cram_fd *fd, enum hts_fmt_option opt, va_list args) {
 	break;
 
     default:
-	fprintf(stderr, "Unknown CRAM option code %d\n", opt);
+	hts_log_error("Unknown CRAM option code %d", opt);
 	errno = EINVAL;
 	return -1;
     }
diff --git a/cram/cram_stats.c b/cram/cram_stats.c
index e913055..0e56593 100644
--- a/cram/cram_stats.c
+++ b/cram/cram_stats.c
@@ -87,11 +87,11 @@ void cram_stats_del(cram_stats *st, int32_t val) {
 	    if (--kh_val(st->h, k) == 0)
 		kh_del(m_i2i, st->h, k);
 	} else {
-	    fprintf(stderr, "Failed to remove val %d from cram_stats\n", val);
+	    hts_log_warning("Failed to remove val %d from cram_stats", val);
 	    st->nsamp++;
 	}
     } else {
-	fprintf(stderr, "Failed to remove val %d from cram_stats\n", val);
+	hts_log_warning("Failed to remove val %d from cram_stats", val);
 	st->nsamp++;
     }
 }
diff --git a/cram/mFILE.c b/cram/mFILE.c
index 77368b4..dbb8b0a 100644
--- a/cram/mFILE.c
+++ b/cram/mFILE.c
@@ -40,6 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <unistd.h>
 #include <stdarg.h>
 
+#include "hts_internal.h"
 #include "cram/os.h"
 #include "cram/mFILE.h"
 
@@ -307,7 +308,7 @@ mFILE *mfreopen(const char *path, const char *mode_str, FILE *fp) {
 	mf = mfcreate(NULL, 0);
 	if (NULL == mf) return NULL;
     } else {
-        fprintf(stderr, "Must specify either r, w or a for mode\n");
+	hts_log_error("Must specify either r, w or a for mode");
         return NULL;
     }
     mf->fp = fp;
diff --git a/cram/sam_header.c b/cram/sam_header.c
index e2cb55d..3b3daa2 100644
--- a/cram/sam_header.c
+++ b/cram/sam_header.c
@@ -33,6 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <string.h>
 #include <assert.h>
 
+#include "hts_internal.h"
 #include "cram/sam_header.h"
 #include "cram/string_alloc.h"
 
@@ -41,7 +42,7 @@ static void sam_hdr_error(char *msg, char *line, int len, int lno) {
     
     for (j = 0; j < len && line[j] != '\n'; j++)
 	;
-    fprintf(stderr, "%s at line %d: \"%.*s\"\n", msg, lno, j, line);
+    hts_log_error("%s at line %d: \"%.*s\"", msg, lno, j, line);
 }
 
 void sam_hdr_dump(SAM_hdr *hdr) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/htslib.git



More information about the debian-med-commit mailing list