Commit 58798295 authored by Rusty Russell's avatar Rusty Russell

tdb2: simplify logging levels, rename TDB_DEBUG_* to TDB_LOG_*

It was never clear to me which levels should be used for what cases.
I can only usefully distinguish three at the moment:
(1) TDB errors, which render the TDB unreliable.
(2) TDB user errors, caused by API misuse.
(3) TDB notifications of strange behaviour, from which we have recovered.
parent 0aa58f6e
This diff is collapsed.
...@@ -118,7 +118,7 @@ static int remove_from_list(struct tdb_context *tdb, ...@@ -118,7 +118,7 @@ static int remove_from_list(struct tdb_context *tdb,
#ifdef CCAN_TDB2_DEBUG #ifdef CCAN_TDB2_DEBUG
if (tdb_read_off(tdb, off) != r_off) { if (tdb_read_off(tdb, off) != r_off) {
tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"remove_from_list: %llu bad prev in list %llu", "remove_from_list: %llu bad prev in list %llu",
(long long)r_off, (long long)b_off); (long long)r_off, (long long)b_off);
return -1; return -1;
...@@ -136,7 +136,7 @@ static int remove_from_list(struct tdb_context *tdb, ...@@ -136,7 +136,7 @@ static int remove_from_list(struct tdb_context *tdb,
#ifdef CCAN_TDB2_DEBUG #ifdef CCAN_TDB2_DEBUG
if (tdb_read_off(tdb, off) & TDB_OFF_MASK != r_off) { if (tdb_read_off(tdb, off) & TDB_OFF_MASK != r_off) {
tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"remove_from_list: %llu bad list %llu", "remove_from_list: %llu bad list %llu",
(long long)r_off, (long long)b_off); (long long)r_off, (long long)b_off);
return -1; return -1;
...@@ -176,7 +176,7 @@ static int enqueue_in_free(struct tdb_context *tdb, ...@@ -176,7 +176,7 @@ static int enqueue_in_free(struct tdb_context *tdb,
new.next + offsetof(struct tdb_free_record, new.next + offsetof(struct tdb_free_record,
magic_and_prev)) magic_and_prev))
!= magic) { != magic) {
tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"enqueue_in_free: %llu bad head" "enqueue_in_free: %llu bad head"
" prev %llu", " prev %llu",
(long long)new.next, (long long)b_off); (long long)new.next, (long long)b_off);
...@@ -333,7 +333,7 @@ static int coalesce(struct tdb_context *tdb, ...@@ -333,7 +333,7 @@ static int coalesce(struct tdb_context *tdb,
goto err; goto err;
if (frec_len(&rec) != data_len) { if (frec_len(&rec) != data_len) {
tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"coalesce: expected data len %zu not %zu", "coalesce: expected data len %zu not %zu",
(size_t)data_len, (size_t)frec_len(&rec)); (size_t)data_len, (size_t)frec_len(&rec));
goto err; goto err;
...@@ -414,7 +414,7 @@ again: ...@@ -414,7 +414,7 @@ again:
if (frec_magic(r) != TDB_FREE_MAGIC) { if (frec_magic(r) != TDB_FREE_MAGIC) {
tdb_access_release(tdb, r); tdb_access_release(tdb, r);
tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"lock_and_alloc: %llu non-free 0x%llx", "lock_and_alloc: %llu non-free 0x%llx",
(long long)off, (long long)r->magic_and_prev); (long long)off, (long long)r->magic_and_prev);
goto unlock_err; goto unlock_err;
...@@ -567,7 +567,7 @@ int set_header(struct tdb_context *tdb, ...@@ -567,7 +567,7 @@ int set_header(struct tdb_context *tdb,
if (rec_key_length(rec) != keylen if (rec_key_length(rec) != keylen
|| rec_data_length(rec) != datalen || rec_data_length(rec) != datalen
|| rec_extra_padding(rec) != actuallen - (keylen + datalen)) { || rec_extra_padding(rec) != actuallen - (keylen + datalen)) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"Could not encode k=%llu,d=%llu,a=%llu", "Could not encode k=%llu,d=%llu,a=%llu",
(long long)keylen, (long long)datalen, (long long)keylen, (long long)datalen,
(long long)actuallen); (long long)actuallen);
...@@ -588,7 +588,7 @@ static int tdb_expand(struct tdb_context *tdb, tdb_len_t size) ...@@ -588,7 +588,7 @@ static int tdb_expand(struct tdb_context *tdb, tdb_len_t size)
/* Need to hold a hash lock to expand DB: transactions rely on it. */ /* Need to hold a hash lock to expand DB: transactions rely on it. */
if (!(tdb->flags & TDB_NOLOCK) if (!(tdb->flags & TDB_NOLOCK)
&& !tdb->allrecord_lock.count && !tdb_has_hash_locks(tdb)) { && !tdb->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_expand: must hold lock during expand"); "tdb_expand: must hold lock during expand");
return -1; return -1;
} }
......
...@@ -704,7 +704,7 @@ int next_in_hash(struct tdb_context *tdb, int ltype, ...@@ -704,7 +704,7 @@ int next_in_hash(struct tdb_context *tdb, int ltype,
} }
if (rec_magic(&rec) != TDB_USED_MAGIC) { if (rec_magic(&rec) != TDB_USED_MAGIC) {
tdb_logerr(tdb, TDB_ERR_CORRUPT, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_FATAL, TDB_LOG_ERROR,
"next_in_hash:" "next_in_hash:"
" corrupt record at %llu", " corrupt record at %llu",
(long long)off); (long long)off);
......
...@@ -56,7 +56,7 @@ void tdb_mmap(struct tdb_context *tdb) ...@@ -56,7 +56,7 @@ void tdb_mmap(struct tdb_context *tdb)
*/ */
if (tdb->map_ptr == MAP_FAILED) { if (tdb->map_ptr == MAP_FAILED) {
tdb->map_ptr = NULL; tdb->map_ptr = NULL;
tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_WARNING, tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
"tdb_mmap failed for size %lld (%s)", "tdb_mmap failed for size %lld (%s)",
(long long)tdb->map_size, strerror(errno)); (long long)tdb->map_size, strerror(errno));
} }
...@@ -80,7 +80,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) ...@@ -80,7 +80,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
return 0; return 0;
if (tdb->flags & TDB_INTERNAL) { if (tdb->flags & TDB_INTERNAL) {
if (!probe) { if (!probe) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_oob len %lld beyond internal" "tdb_oob len %lld beyond internal"
" malloc size %lld", " malloc size %lld",
(long long)len, (long long)len,
...@@ -93,7 +93,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) ...@@ -93,7 +93,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
return -1; return -1;
if (fstat(tdb->fd, &st) != 0) { if (fstat(tdb->fd, &st) != 0) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"Failed to fstat file: %s", strerror(errno)); "Failed to fstat file: %s", strerror(errno));
tdb_unlock_expand(tdb, F_RDLCK); tdb_unlock_expand(tdb, F_RDLCK);
return -1; return -1;
...@@ -103,7 +103,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) ...@@ -103,7 +103,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
if (st.st_size < (size_t)len) { if (st.st_size < (size_t)len) {
if (!probe) { if (!probe) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_oob len %zu beyond eof at %zu", "tdb_oob len %zu beyond eof at %zu",
(size_t)len, st.st_size); (size_t)len, st.st_size);
} }
...@@ -211,7 +211,7 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -211,7 +211,7 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
const void *buf, tdb_len_t len) const void *buf, tdb_len_t len)
{ {
if (tdb->read_only) { if (tdb->read_only) {
tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
"Write to read-only database"); "Write to read-only database");
return -1; return -1;
} }
...@@ -234,7 +234,7 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -234,7 +234,7 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
if (ret >= 0) if (ret >= 0)
errno = ENOSPC; errno = ENOSPC;
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_write: %zi at %zu len=%zu (%s)", "tdb_write: %zi at %zu len=%zu (%s)",
ret, (size_t)off, (size_t)len, ret, (size_t)off, (size_t)len,
strerror(errno)); strerror(errno));
...@@ -257,7 +257,7 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf, ...@@ -257,7 +257,7 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
} else { } else {
ssize_t r = pread(tdb->fd, buf, len, off); ssize_t r = pread(tdb->fd, buf, len, off);
if (r != len) { if (r != len) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_read failed with %zi at %zu " "tdb_read failed with %zi at %zu "
"len=%zu (%s) map_size=%zu", "len=%zu (%s) map_size=%zu",
r, (size_t)off, (size_t)len, r, (size_t)off, (size_t)len,
...@@ -276,7 +276,7 @@ int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off, ...@@ -276,7 +276,7 @@ int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
if (unlikely((tdb->flags & TDB_CONVERT))) { if (unlikely((tdb->flags & TDB_CONVERT))) {
void *conv = malloc(len); void *conv = malloc(len);
if (!conv) { if (!conv) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"tdb_write: no memory converting" "tdb_write: no memory converting"
" %zu bytes", len); " %zu bytes", len);
return -1; return -1;
...@@ -302,7 +302,7 @@ int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off, ...@@ -302,7 +302,7 @@ int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
int tdb_write_off(struct tdb_context *tdb, tdb_off_t off, tdb_off_t val) int tdb_write_off(struct tdb_context *tdb, tdb_off_t off, tdb_off_t val)
{ {
if (tdb->read_only) { if (tdb->read_only) {
tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
"Write to read-only database"); "Write to read-only database");
return -1; return -1;
} }
...@@ -326,7 +326,7 @@ static void *_tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, ...@@ -326,7 +326,7 @@ static void *_tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset,
/* some systems don't like zero length malloc */ /* some systems don't like zero length malloc */
buf = malloc(prefix + len ? prefix + len : 1); buf = malloc(prefix + len ? prefix + len : 1);
if (!buf) { if (!buf) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_USE_ERROR,
"tdb_alloc_read malloc failed len=%zu", "tdb_alloc_read malloc failed len=%zu",
(size_t)(prefix + len)); (size_t)(prefix + len));
} else if (unlikely(tdb->methods->tread(tdb, offset, buf+prefix, len) } else if (unlikely(tdb->methods->tread(tdb, offset, buf+prefix, len)
...@@ -354,7 +354,7 @@ static int fill(struct tdb_context *tdb, ...@@ -354,7 +354,7 @@ static int fill(struct tdb_context *tdb,
if (ret >= 0) if (ret >= 0)
errno = ENOSPC; errno = ENOSPC;
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"fill failed: %zi at %zu len=%zu (%s)", "fill failed: %zi at %zu len=%zu (%s)",
ret, (size_t)off, (size_t)len, ret, (size_t)off, (size_t)len,
strerror(errno)); strerror(errno));
...@@ -373,7 +373,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition) ...@@ -373,7 +373,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition)
char buf[8192]; char buf[8192];
if (tdb->read_only) { if (tdb->read_only) {
tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
"Expand on read-only database"); "Expand on read-only database");
return -1; return -1;
} }
...@@ -381,7 +381,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition) ...@@ -381,7 +381,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition)
if (tdb->flags & TDB_INTERNAL) { if (tdb->flags & TDB_INTERNAL) {
char *new = realloc(tdb->map_ptr, tdb->map_size + addition); char *new = realloc(tdb->map_ptr, tdb->map_size + addition);
if (!new) { if (!new) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"No memory to expand database"); "No memory to expand database");
return -1; return -1;
} }
...@@ -438,7 +438,7 @@ void *tdb_access_write(struct tdb_context *tdb, ...@@ -438,7 +438,7 @@ void *tdb_access_write(struct tdb_context *tdb,
void *ret = NULL; void *ret = NULL;
if (tdb->read_only) { if (tdb->read_only) {
tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
"Write to read-only database"); "Write to read-only database");
return NULL; return NULL;
} }
......
...@@ -135,7 +135,7 @@ static int tdb_brlock(struct tdb_context *tdb, ...@@ -135,7 +135,7 @@ static int tdb_brlock(struct tdb_context *tdb,
} }
if (rw_type == F_WRLCK && tdb->read_only) { if (rw_type == F_WRLCK && tdb->read_only) {
tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR,
"Write lock attempted on read-only database"); "Write lock attempted on read-only database");
return -1; return -1;
} }
...@@ -143,7 +143,7 @@ static int tdb_brlock(struct tdb_context *tdb, ...@@ -143,7 +143,7 @@ static int tdb_brlock(struct tdb_context *tdb,
/* A 32 bit system cannot open a 64-bit file, but it could have /* A 32 bit system cannot open a 64-bit file, but it could have
* expanded since then: check here. */ * expanded since then: check here. */
if ((size_t)(offset + len) != offset + len) { if ((size_t)(offset + len) != offset + len) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_brlock: lock on giant offset %llu", "tdb_brlock: lock on giant offset %llu",
(long long)(offset + len)); (long long)(offset + len));
return -1; return -1;
...@@ -160,7 +160,7 @@ static int tdb_brlock(struct tdb_context *tdb, ...@@ -160,7 +160,7 @@ static int tdb_brlock(struct tdb_context *tdb,
* EAGAIN is an expected return from non-blocking * EAGAIN is an expected return from non-blocking
* locks. */ * locks. */
if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) { if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_brlock failed (fd=%d) at" "tdb_brlock failed (fd=%d) at"
" offset %zu rw_type=%d flags=%d len=%zu:" " offset %zu rw_type=%d flags=%d len=%zu:"
" %s", " %s",
...@@ -186,7 +186,7 @@ static int tdb_brunlock(struct tdb_context *tdb, ...@@ -186,7 +186,7 @@ static int tdb_brunlock(struct tdb_context *tdb,
} while (ret == -1 && errno == EINTR); } while (ret == -1 && errno == EINTR);
if (ret == -1) { if (ret == -1) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_brunlock failed (fd=%d) at offset %zu" "tdb_brunlock failed (fd=%d) at offset %zu"
" rw_type=%d len=%zu", " rw_type=%d len=%zu",
tdb->fd, (size_t)offset, rw_type, (size_t)len); tdb->fd, (size_t)offset, rw_type, (size_t)len);
...@@ -205,14 +205,14 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb) ...@@ -205,14 +205,14 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb)
int count = 1000; int count = 1000;
if (tdb->allrecord_lock.count != 1) { if (tdb->allrecord_lock.count != 1) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_upgrade failed: count %u too high", "tdb_allrecord_upgrade failed: count %u too high",
tdb->allrecord_lock.count); tdb->allrecord_lock.count);
return -1; return -1;
} }
if (tdb->allrecord_lock.off != 1) { if (tdb->allrecord_lock.off != 1) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_upgrade failed: already upgraded?"); "tdb_allrecord_upgrade failed: already upgraded?");
return -1; return -1;
} }
...@@ -234,7 +234,7 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb) ...@@ -234,7 +234,7 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb)
tv.tv_usec = 1; tv.tv_usec = 1;
select(0, NULL, NULL, NULL, &tv); select(0, NULL, NULL, NULL, &tv);
} }
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_upgrade failed"); "tdb_allrecord_upgrade failed");
return -1; return -1;
} }
...@@ -280,7 +280,7 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype, ...@@ -280,7 +280,7 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
struct tdb_lock_type *new_lck; struct tdb_lock_type *new_lck;
if (offset > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) { if (offset > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_nest_lock: invalid offset %zu ltype=%d", "tdb_nest_lock: invalid offset %zu ltype=%d",
(size_t)offset, ltype); (size_t)offset, ltype);
return -1; return -1;
...@@ -294,7 +294,7 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype, ...@@ -294,7 +294,7 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
new_lck = find_nestlock(tdb, offset); new_lck = find_nestlock(tdb, offset);
if (new_lck) { if (new_lck) {
if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) { if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_nest_lock: offset %zu has read lock", "tdb_nest_lock: offset %zu has read lock",
(size_t)offset); (size_t)offset);
return -1; return -1;
...@@ -307,7 +307,7 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype, ...@@ -307,7 +307,7 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
if (tdb->num_lockrecs if (tdb->num_lockrecs
&& offset >= TDB_HASH_LOCK_START && offset >= TDB_HASH_LOCK_START
&& offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) { && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_nest_lock: already have a hash lock?"); "tdb_nest_lock: already have a hash lock?");
return -1; return -1;
} }
...@@ -316,7 +316,7 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype, ...@@ -316,7 +316,7 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
tdb->lockrecs, tdb->lockrecs,
sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1)); sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
if (new_lck == NULL) { if (new_lck == NULL) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"tdb_nest_lock: unable to allocate %zu lock struct", "tdb_nest_lock: unable to allocate %zu lock struct",
tdb->num_lockrecs + 1); tdb->num_lockrecs + 1);
errno = ENOMEM; errno = ENOMEM;
...@@ -363,7 +363,7 @@ static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype) ...@@ -363,7 +363,7 @@ static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
lck = find_nestlock(tdb, off); lck = find_nestlock(tdb, off);
if ((lck == NULL) || (lck->count == 0)) { if ((lck == NULL) || (lck->count == 0)) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_nest_unlock: no lock for %zu", (size_t)off); "tdb_nest_unlock: no lock for %zu", (size_t)off);
return -1; return -1;
} }
...@@ -449,7 +449,7 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype, ...@@ -449,7 +449,7 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
{ {
/* FIXME: There are no locks on read-only dbs */ /* FIXME: There are no locks on read-only dbs */
if (tdb->read_only) { if (tdb->read_only) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_allrecord_lock: read-only"); "tdb_allrecord_lock: read-only");
return -1; return -1;
} }
...@@ -462,7 +462,7 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype, ...@@ -462,7 +462,7 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.count) {
/* a global lock of a different type exists */ /* a global lock of a different type exists */
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_allrecord_lock: already have %s lock", "tdb_allrecord_lock: already have %s lock",
tdb->allrecord_lock.ltype == F_RDLCK tdb->allrecord_lock.ltype == F_RDLCK
? "read" : "write"); ? "read" : "write");
...@@ -471,14 +471,14 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype, ...@@ -471,14 +471,14 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
if (tdb_has_hash_locks(tdb)) { if (tdb_has_hash_locks(tdb)) {
/* can't combine global and chain locks */ /* can't combine global and chain locks */
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_allrecord_lock: already have chain lock"); "tdb_allrecord_lock: already have chain lock");
return -1; return -1;
} }
if (upgradable && ltype != F_RDLCK) { if (upgradable && ltype != F_RDLCK) {
/* tdb error: you can't upgrade a write lock! */ /* tdb error: you can't upgrade a write lock! */
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_lock: can't upgrade a write lock"); "tdb_allrecord_lock: can't upgrade a write lock");
return -1; return -1;
} }
...@@ -489,7 +489,7 @@ again: ...@@ -489,7 +489,7 @@ again:
if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START, if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
TDB_HASH_LOCK_RANGE)) { TDB_HASH_LOCK_RANGE)) {
if (!(flags & TDB_LOCK_PROBE)) { if (!(flags & TDB_LOCK_PROBE)) {
tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR, tdb_logerr(tdb, tdb->ecode, TDB_LOG_ERROR,
"tdb_allrecord_lock hashes failed"); "tdb_allrecord_lock hashes failed");
} }
return -1; return -1;
...@@ -499,7 +499,7 @@ again: ...@@ -499,7 +499,7 @@ again:
if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE, if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
0, flags)) { 0, flags)) {
if (!(flags & TDB_LOCK_PROBE)) { if (!(flags & TDB_LOCK_PROBE)) {
tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR, tdb_logerr(tdb, tdb->ecode, TDB_LOG_ERROR,
"tdb_allrecord_lock freetables failed"); "tdb_allrecord_lock freetables failed");
} }
tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START,
...@@ -556,7 +556,7 @@ void tdb_unlock_expand(struct tdb_context *tdb, int ltype) ...@@ -556,7 +556,7 @@ void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype) int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
{ {
if (tdb->allrecord_lock.count == 0) { if (tdb->allrecord_lock.count == 0) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_allrecord_unlock: not locked!"); "tdb_allrecord_unlock: not locked!");
return -1; return -1;
} }
...@@ -564,7 +564,7 @@ int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype) ...@@ -564,7 +564,7 @@ int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
/* Upgradable locks are marked as write locks. */ /* Upgradable locks are marked as write locks. */
if (tdb->allrecord_lock.ltype != ltype if (tdb->allrecord_lock.ltype != ltype
&& (!tdb->allrecord_lock.off || ltype != F_RDLCK)) { && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_unlock: have %s lock", "tdb_allrecord_unlock: have %s lock",
tdb->allrecord_lock.ltype == F_RDLCK tdb->allrecord_lock.ltype == F_RDLCK
? "read" : "write"); ? "read" : "write");
...@@ -628,7 +628,7 @@ int tdb_lock_hashes(struct tdb_context *tdb, ...@@ -628,7 +628,7 @@ int tdb_lock_hashes(struct tdb_context *tdb,
} }
if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.count) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_lock_hashes: already have %s allrecordlock", "tdb_lock_hashes: already have %s allrecordlock",
tdb->allrecord_lock.ltype == F_RDLCK tdb->allrecord_lock.ltype == F_RDLCK
? "read" : "write"); ? "read" : "write");
...@@ -636,13 +636,13 @@ int tdb_lock_hashes(struct tdb_context *tdb, ...@@ -636,13 +636,13 @@ int tdb_lock_hashes(struct tdb_context *tdb,
} }
if (tdb_has_free_lock(tdb)) { if (tdb_has_free_lock(tdb)) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_lock_hashes: already have free lock"); "tdb_lock_hashes: already have free lock");
return -1; return -1;
} }
if (tdb_has_expansion_lock(tdb)) { if (tdb_has_expansion_lock(tdb)) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_lock_hashes: already have expansion lock"); "tdb_lock_hashes: already have expansion lock");
return -1; return -1;
} }
...@@ -661,7 +661,7 @@ int tdb_unlock_hashes(struct tdb_context *tdb, ...@@ -661,7 +661,7 @@ int tdb_unlock_hashes(struct tdb_context *tdb,
if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.count) {
if (tdb->allrecord_lock.ltype == F_RDLCK if (tdb->allrecord_lock.ltype == F_RDLCK
&& ltype == F_WRLCK) { && ltype == F_WRLCK) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_unlock_hashes RO allrecord!"); "tdb_unlock_hashes RO allrecord!");
return -1; return -1;
} }
...@@ -691,14 +691,14 @@ int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off, ...@@ -691,14 +691,14 @@ int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.count) {
if (tdb->allrecord_lock.ltype == F_WRLCK) if (tdb->allrecord_lock.ltype == F_WRLCK)
return 0; return 0;
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_lock_free_bucket with RO allrecordlock!"); "tdb_lock_free_bucket with RO allrecordlock!");
return -1; return -1;
} }
#if 0 /* FIXME */ #if 0 /* FIXME */
if (tdb_has_expansion_lock(tdb)) { if (tdb_has_expansion_lock(tdb)) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_lock_free_bucket: already have expansion lock"); "tdb_lock_free_bucket: already have expansion lock");
return -1; return -1;
} }
......
...@@ -331,7 +331,10 @@ struct tdb_context { ...@@ -331,7 +331,10 @@ struct tdb_context {
uint32_t flags; uint32_t flags;
/* Logging function */ /* Logging function */
tdb_logfn_t logfn; void (*logfn)(struct tdb_context *tdb,
enum tdb_log_level level,
void *log_private,
const char *message);
void *log_private; void *log_private;
/* Hash function. */ /* Hash function. */
...@@ -550,7 +553,7 @@ bool tdb_needs_recovery(struct tdb_context *tdb); ...@@ -550,7 +553,7 @@ bool tdb_needs_recovery(struct tdb_context *tdb);
/* tdb.c: */ /* tdb.c: */
void COLD tdb_logerr(struct tdb_context *tdb, void COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR ecode, enum TDB_ERROR ecode,
enum tdb_debug_level level, enum tdb_log_level level,
const char *fmt, ...); const char *fmt, ...);
#ifdef TDB_TRACE #ifdef TDB_TRACE
......
...@@ -175,7 +175,7 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) ...@@ -175,7 +175,7 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
chains = tally_new(HISTO_HEIGHT); chains = tally_new(HISTO_HEIGHT);
if (!ftables || !hashes || !freet || !keys || !data || !extra if (!ftables || !hashes || !freet || !keys || !data || !extra
|| !uncoal || !buckets || !chains) { || !uncoal || !buckets || !chains) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"tdb_summary: failed to allocate tally structures"); "tdb_summary: failed to allocate tally structures");
goto unlock; goto unlock;
} }
......
...@@ -49,8 +49,6 @@ static uint64_t random_number(struct tdb_context *tdb) ...@@ -49,8 +49,6 @@ static uint64_t random_number(struct tdb_context *tdb)
fd = open("/dev/urandom", O_RDONLY); fd = open("/dev/urandom", O_RDONLY);
if (fd >= 0) { if (fd >= 0) {
if (read_all(fd, &ret, sizeof(ret))) { if (read_all(fd, &ret, sizeof(ret))) {
tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
"tdb_open: random from /dev/urandom");
close(fd); close(fd);
return ret; return ret;
} }
...@@ -65,9 +63,6 @@ static uint64_t random_number(struct tdb_context *tdb) ...@@ -65,9 +63,6 @@ static uint64_t random_number(struct tdb_context *tdb)
char reply[1 + sizeof(uint64_t)]; char reply[1 + sizeof(uint64_t)];
int r = read(fd, reply, sizeof(reply)); int r = read(fd, reply, sizeof(reply));
if (r > 1) { if (r > 1) {
tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
"tdb_open: %u random bytes from"
" /dev/egd-pool", r-1);
/* Copy at least some bytes. */ /* Copy at least some bytes. */
memcpy(&ret, reply+1, r - 1); memcpy(&ret, reply+1, r - 1);
if (reply[0] == sizeof(uint64_t) if (reply[0] == sizeof(uint64_t)
...@@ -83,7 +78,7 @@ static uint64_t random_number(struct tdb_context *tdb) ...@@ -83,7 +78,7 @@ static uint64_t random_number(struct tdb_context *tdb)
/* Fallback: pid and time. */ /* Fallback: pid and time. */
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec; ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE, tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
"tdb_open: random from getpid and time"); "tdb_open: random from getpid and time");
return ret; return ret;
} }
...@@ -141,7 +136,7 @@ static int tdb_new_database(struct tdb_context *tdb, ...@@ -141,7 +136,7 @@ static int tdb_new_database(struct tdb_context *tdb,
tdb->map_size = sizeof(newdb); tdb->map_size = sizeof(newdb);
tdb->map_ptr = malloc(tdb->map_size); tdb->map_ptr = malloc(tdb->map_size);
if (!tdb->map_ptr) { if (!tdb->map_ptr) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"tdb_new_database: failed to allocate"); "tdb_new_database: failed to allocate");
return -1; return -1;
} }
...@@ -158,7 +153,7 @@ static int tdb_new_database(struct tdb_context *tdb, ...@@ -158,7 +153,7 @@ static int tdb_new_database(struct tdb_context *tdb,
if (rlen != sizeof(newdb)) { if (rlen != sizeof(newdb)) {
if (rlen >= 0) if (rlen >= 0)
errno = ENOSPC; errno = ENOSPC;
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_new_database: %zi writing header: %s", "tdb_new_database: %zi writing header: %s",
rlen, strerror(errno)); rlen, strerror(errno));
return -1; return -1;
...@@ -220,7 +215,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -220,7 +215,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->stats->size = sizeof(attr->stats); tdb->stats->size = sizeof(attr->stats);
break; break;
default: default:
tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
"tdb_open: unknown attribute type %u", "tdb_open: unknown attribute type %u",
attr->base.attr); attr->base.attr);
goto fail; goto fail;
...@@ -229,7 +224,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -229,7 +224,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
} }
if ((open_flags & O_ACCMODE) == O_WRONLY) { if ((open_flags & O_ACCMODE) == O_WRONLY) {
tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
"tdb_open: can't open tdb %s write-only", name); "tdb_open: can't open tdb %s write-only", name);
goto fail; goto fail;
} }
...@@ -259,7 +254,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -259,7 +254,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
if ((tdb->fd = open(name, open_flags, mode)) == -1) { if ((tdb->fd = open(name, open_flags, mode)) == -1) {
/* errno set by open(2) */ /* errno set by open(2) */
saved_errno = errno; saved_errno = errno;
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: could not open file %s: %s", "tdb_open: could not open file %s: %s",
name, strerror(errno)); name, strerror(errno));
goto fail; goto fail;
...@@ -283,13 +278,13 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -283,13 +278,13 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
goto fail; goto fail;
} }
} else if (rlen < 0) { } else if (rlen < 0) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: error %s reading %s", "tdb_open: error %s reading %s",
strerror(errno), name); strerror(errno), name);
goto fail; goto fail;
} else if (rlen < sizeof(hdr) } else if (rlen < sizeof(hdr)
|| strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) { || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: %s is not a tdb file", name); "tdb_open: %s is not a tdb file", name);
goto fail; goto fail;
} }
...@@ -299,7 +294,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -299,7 +294,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->flags |= TDB_CONVERT; tdb->flags |= TDB_CONVERT;
else { else {
/* wrong version */ /* wrong version */
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: %s is unknown version 0x%llx", "tdb_open: %s is unknown version 0x%llx",
name, (long long)hdr.version); name, (long long)hdr.version);
goto fail; goto fail;
...@@ -312,7 +307,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -312,7 +307,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test)); hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
if (hdr.hash_test != hash_test) { if (hdr.hash_test != hash_test) {
/* wrong hash variant */ /* wrong hash variant */
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: %s uses a different hash function", "tdb_open: %s uses a different hash function",
name); name);
goto fail; goto fail;
...@@ -320,7 +315,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -320,7 +315,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
if (fstat(tdb->fd, &st) == -1) { if (fstat(tdb->fd, &st) == -1) {
saved_errno = errno; saved_errno = errno;
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: could not stat open %s: %s", "tdb_open: could not stat open %s: %s",
name, strerror(errno)); name, strerror(errno));
goto fail; goto fail;
...@@ -329,7 +324,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -329,7 +324,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
/* Is it already in the open list? If so, fail. */ /* Is it already in the open list? If so, fail. */
if (tdb_already_open(st.st_dev, st.st_ino)) { if (tdb_already_open(st.st_dev, st.st_ino)) {
/* FIXME */ /* FIXME */
tdb_logerr(tdb, TDB_ERR_NESTING, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_NESTING, TDB_LOG_USE_ERROR,
"tdb_open: %s (%d,%d) is already open in this" "tdb_open: %s (%d,%d) is already open in this"
" process", " process",
name, (int)st.st_dev, (int)st.st_ino); name, (int)st.st_dev, (int)st.st_ino);
...@@ -338,7 +333,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -338,7 +333,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->name = strdup(name); tdb->name = strdup(name);
if (!tdb->name) { if (!tdb->name) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"tdb_open: failed to allocate name"); "tdb_open: failed to allocate name");
goto fail; goto fail;
} }
...@@ -401,9 +396,9 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -401,9 +396,9 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
free((char *)tdb->name); free((char *)tdb->name);
if (tdb->fd != -1) if (tdb->fd != -1)
if (close(tdb->fd) != 0) if (close(tdb->fd) != 0)
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: failed to close tdb->fd" "tdb_open: failed to close tdb->fd"
" on error!"); " on error: %s", strerror(errno));
free(tdb); free(tdb);
errno = saved_errno; errno = saved_errno;
return NULL; return NULL;
...@@ -562,7 +557,7 @@ int tdb_append(struct tdb_context *tdb, ...@@ -562,7 +557,7 @@ int tdb_append(struct tdb_context *tdb,
/* Slow path. */ /* Slow path. */
newdata = malloc(key.dsize + old_dlen + dbuf.dsize); newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
if (!newdata) { if (!newdata) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL, tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"tdb_append: failed to allocate %zu bytes", "tdb_append: failed to allocate %zu bytes",
(size_t)(key.dsize+old_dlen+dbuf.dsize)); (size_t)(key.dsize+old_dlen+dbuf.dsize));
goto fail; goto fail;
...@@ -717,7 +712,7 @@ const char *tdb_errorstr(const struct tdb_context *tdb) ...@@ -717,7 +712,7 @@ const char *tdb_errorstr(const struct tdb_context *tdb)
void COLD tdb_logerr(struct tdb_context *tdb, void COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR ecode, enum TDB_ERROR ecode,
enum tdb_debug_level level, enum tdb_log_level level,
const char *fmt, ...) const char *fmt, ...)
{ {
char *message; char *message;
...@@ -738,7 +733,7 @@ void COLD tdb_logerr(struct tdb_context *tdb, ...@@ -738,7 +733,7 @@ void COLD tdb_logerr(struct tdb_context *tdb,
message = malloc(len + 1); message = malloc(len + 1);
if (!message) { if (!message) {
tdb->logfn(tdb, TDB_DEBUG_ERROR, tdb->log_private, tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
"out of memory formatting message:"); "out of memory formatting message:");
tdb->logfn(tdb, level, tdb->log_private, fmt); tdb->logfn(tdb, level, tdb->log_private, fmt);
return; return;
......
...@@ -68,8 +68,7 @@ enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, ...@@ -68,8 +68,7 @@ enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 }; enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 };
/* logging uses one of the following levels */ /* logging uses one of the following levels */
enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR, enum tdb_log_level {TDB_LOG_ERROR = 0, TDB_LOG_USE_ERROR, TDB_LOG_WARNING};
TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
typedef struct tdb_data { typedef struct tdb_data {
unsigned char *dptr; unsigned char *dptr;
...@@ -80,7 +79,6 @@ struct tdb_context; ...@@ -80,7 +79,6 @@ struct tdb_context;
/* FIXME: Make typesafe */ /* FIXME: Make typesafe */
typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *); typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
typedef void (*tdb_logfn_t)(struct tdb_context *, enum tdb_debug_level, void *, const char *);
typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed, typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed,
void *priv); void *priv);
...@@ -98,7 +96,10 @@ struct tdb_attribute_base { ...@@ -98,7 +96,10 @@ struct tdb_attribute_base {
struct tdb_attribute_log { struct tdb_attribute_log {
struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */ struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
tdb_logfn_t log_fn; void (*log_fn)(struct tdb_context *tdb,
enum tdb_log_level level,
void *log_private,
const char *message);
void *log_private; void *log_private;
}; };
......
...@@ -13,14 +13,13 @@ union tdb_attribute tap_log_attr = { ...@@ -13,14 +13,13 @@ union tdb_attribute tap_log_attr = {
}; };
void tap_log_fn(struct tdb_context *tdb, void tap_log_fn(struct tdb_context *tdb,
enum tdb_debug_level level, void *priv, enum tdb_log_level level, void *priv,
const char *message) const char *message)
{ {
if (suppress_logging) if (suppress_logging)
return; return;
diag("tdb log level %u: %s%s", level, log_prefix, message); diag("tdb log level %u: %s%s", level, log_prefix, message);
if (level != TDB_DEBUG_TRACE)
tap_log_messages++; tap_log_messages++;
} }
...@@ -10,7 +10,7 @@ extern unsigned tap_log_messages; ...@@ -10,7 +10,7 @@ extern unsigned tap_log_messages;
extern union tdb_attribute tap_log_attr; extern union tdb_attribute tap_log_attr;
void tap_log_fn(struct tdb_context *tdb, void tap_log_fn(struct tdb_context *tdb,
enum tdb_debug_level level, void *priv, enum tdb_log_level level, void *priv,
const char *message); const char *message);
static inline bool data_equal(struct tdb_data a, struct tdb_data b) static inline bool data_equal(struct tdb_data a, struct tdb_data b)
......
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
#include "logging.h" #include "logging.h"
/* FIXME: Check these! */ /* FIXME: Check these! */
#define INITIAL_TDB_MALLOC "tdb.c", 182, FAILTEST_MALLOC #define INITIAL_TDB_MALLOC "tdb.c", 177, FAILTEST_MALLOC
#define LOGGING_MALLOC "tdb.c", 739, FAILTEST_MALLOC #define LOGGING_MALLOC "tdb.c", 734, FAILTEST_MALLOC
#define URANDOM_OPEN "tdb.c", 49, FAILTEST_OPEN #define URANDOM_OPEN "tdb.c", 49, FAILTEST_OPEN
#define URANDOM_READ "tdb.c", 29, FAILTEST_READ #define URANDOM_READ "tdb.c", 29, FAILTEST_READ
......
...@@ -12,7 +12,7 @@ static int log_count = 0; ...@@ -12,7 +12,7 @@ static int log_count = 0;
/* Normally we get a log when setting random seed. */ /* Normally we get a log when setting random seed. */
static void my_log_fn(struct tdb_context *tdb, static void my_log_fn(struct tdb_context *tdb,
enum tdb_debug_level level, void *priv, enum tdb_log_level level, void *priv,
const char *message) const char *message)
{ {
log_count++; log_count++;
......
...@@ -50,15 +50,12 @@ static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *p ...@@ -50,15 +50,12 @@ static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *p
{ {
va_list ap; va_list ap;
if (level != TDB_DEBUG_TRACE)
error_count++;
va_start(ap, format); va_start(ap, format);
vfprintf(stdout, format, ap); vfprintf(stdout, format, ap);
va_end(ap); va_end(ap);
fflush(stdout); fflush(stdout);
#if 0 #if 0
if (level != TDB_DEBUG_TRACE) { {
char *ptr; char *ptr;
signal(SIGUSR1, SIG_IGN); signal(SIGUSR1, SIG_IGN);
asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid()); asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment