Skip to content

Commit 4363cd9

Browse files
author
Garrett Smith
committed
Cleanup trace facility
1 parent afa6569 commit 4363cd9

File tree

9 files changed

+94
-107
lines changed

9 files changed

+94
-107
lines changed

NOTES

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ Source code could use some reformatting:
216216
- Improved white spacing
217217
- Misc cleanup
218218

219-
** TODO traceEvent to trace_info, etc
219+
** DONE traceEvent to trace_info, etc
220+
** TODO Clean up variable names
220221
** TODO Exit on impossible conditions
221222

222223
malloc, e.g. should cause an immediate exit.

test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void check_read_val(uint val, uint expected, char *key) {
4646
int main(int argc, char *argv[]) {
4747

4848
char *file = get_file_arg(argc, argv);
49-
traceLevel = 0;
49+
set_trace_level(0);
5050

5151
tsdb_handler db;
5252
int ret;

tsdb_api.c

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ int tsdb_open(char *tsdb_path, tsdb_handler *handler,
4343
handler->read_only_mode = read_only_mode;
4444

4545
if ((ret = db_create(&handler->db, NULL, 0)) != 0) {
46-
traceEvent(TRACE_ERROR, "Error while creating DB handler [%s]",
47-
db_strerror(ret));
46+
trace_error("Error while creating DB handler [%s]", db_strerror(ret));
4847
return -1;
4948
}
5049

@@ -56,9 +55,8 @@ int tsdb_open(char *tsdb_path, tsdb_handler *handler,
5655
DB_BTREE,
5756
(read_only_mode ? 0 : DB_CREATE),
5857
mode)) != 0) {
59-
traceEvent(TRACE_ERROR,
60-
"Error while opening DB %s [%s][r/o=%u,mode=%o]",
61-
tsdb_path, db_strerror(ret), read_only_mode, mode);
58+
trace_error("Error while opening DB %s [%s][r/o=%u,mode=%o]",
59+
tsdb_path, db_strerror(ret), read_only_mode, mode);
6260
return -1;
6361
}
6462

@@ -105,12 +103,9 @@ int tsdb_open(char *tsdb_path, tsdb_handler *handler,
105103

106104
handler->values_len = handler->num_values_per_entry * sizeof(tsdb_value);
107105

108-
traceEvent(TRACE_INFO, "lowest_free_index: %u",
109-
handler->lowest_free_index);
110-
traceEvent(TRACE_INFO, "rrd_slot_time_duration: %u",
111-
handler->rrd_slot_time_duration);
112-
traceEvent(TRACE_INFO, "num_values_per_entry: %u",
113-
handler->num_values_per_entry);
106+
trace_info("lowest_free_index: %u", handler->lowest_free_index);
107+
trace_info("rrd_slot_time_duration: %u", handler->rrd_slot_time_duration);
108+
trace_info("num_values_per_entry: %u", handler->num_values_per_entry);
114109

115110
memset(&handler->state_compress, 0, sizeof(handler->state_compress));
116111
memset(&handler->state_decompress, 0, sizeof(handler->state_decompress));
@@ -125,15 +120,15 @@ static void map_raw_delete(tsdb_handler *handler,
125120
DBT key_data;
126121

127122
if (handler->read_only_mode) {
128-
traceEvent(TRACE_WARNING, "Unable to delete value (read-only mode)");
123+
trace_warning("Unable to delete value (read-only mode)");
129124
return;
130125
}
131126

132127
memset(&key_data, 0, sizeof(key_data));
133128
key_data.data = key, key_data.size = key_len;
134129

135130
if (handler->db->del(handler->db, NULL, &key_data, 0) != 0) {
136-
traceEvent(TRACE_WARNING, "Error while deleting key");
131+
trace_warning("Error while deleting key");
137132
}
138133
}
139134

@@ -155,7 +150,7 @@ static void map_raw_set(tsdb_handler *handler,
155150
DBT key_data, data;
156151

157152
if (handler->read_only_mode) {
158-
traceEvent(TRACE_WARNING, "Unable to set value (read-only mode)");
153+
trace_warning("Unable to set value (read-only mode)");
159154
return;
160155
}
161156

@@ -165,7 +160,7 @@ static void map_raw_set(tsdb_handler *handler,
165160
data.data = value, data.size = value_len;
166161

167162
if (handler->db->put(handler->db, NULL, &key_data, &data, 0) != 0) {
168-
traceEvent(TRACE_WARNING, "Error while map_set(%u, %u)", key, value);
163+
trace_warning("Error while map_set(%u, %u)", key, value);
169164
}
170165
}
171166

@@ -197,7 +192,7 @@ static void tsdb_flush_chunk(tsdb_handler *handler) {
197192
new_len = handler->chunk.chunk_mem_len + 400 /* Static value */;
198193
compressed = (char*)malloc(new_len);
199194
if (!compressed) {
200-
traceEvent(TRACE_WARNING, "Not enough memory (%u bytes)", new_len);
195+
trace_warning("Not enough memory (%u bytes)", new_len);
201196
return;
202197
}
203198

@@ -214,16 +209,15 @@ static void tsdb_flush_chunk(tsdb_handler *handler) {
214209
compressed, fragment_size,
215210
&handler->state_compress);
216211

217-
traceEvent(TRACE_NORMAL,
218-
"Compression %u -> %u [fragment %u] [%.1f %%]",
212+
trace_info("Compression %u -> %u [fragment %u] [%.1f %%]",
219213
fragment_size, compressed_len, i,
220214
((float)(compressed_len*100))/((float)fragment_size));
221215

222216
snprintf(str, sizeof(str), "%u-%u", handler->chunk.begin_epoch, i);
223217

224218
map_raw_set(handler, str, strlen(str), compressed, compressed_len);
225219
} else {
226-
traceEvent(TRACE_INFO, "Skipping fragment %u (unchanged)", i);
220+
trace_info("Skipping fragment %u (unchanged)", i);
227221
}
228222
}
229223

@@ -247,7 +241,7 @@ void tsdb_close(tsdb_handler *handler) {
247241
tsdb_flush_chunk(handler);
248242

249243
if (!handler->read_only_mode) {
250-
traceEvent(TRACE_INFO, "Flushing database changes...");
244+
trace_info("Flushing database changes...");
251245
}
252246

253247
handler->db->close(handler->db, 0);
@@ -345,11 +339,10 @@ void tsdb_drop_key(tsdb_handler *handler,
345339

346340
if (drop_map_hash_index(handler, hash_name,
347341
epoch_value, &hash_idx) == 0) {
348-
traceEvent(TRACE_INFO, "Index %s mapped to hash %u",
349-
hash_name, hash_idx);
342+
trace_info("Index %s mapped to hash %u", hash_name, hash_idx);
350343
unreserve_hash_index(handler, hash_idx);
351344
} else {
352-
traceEvent(TRACE_WARNING, "Unable to drop key %s", hash_name);
345+
trace_warning("Unable to drop key %s", hash_name);
353346
}
354347
}
355348

@@ -364,7 +357,7 @@ static void set_map_hash_index(tsdb_handler *handler, char *idx,
364357
mapping.hash_idx = value;
365358
map_raw_set(handler, str, strlen(str), &mapping, sizeof(mapping));
366359

367-
traceEvent(TRACE_INFO, "[SET] Mapping %s -> %u", idx, value);
360+
trace_info("[SET] Mapping %s -> %u", idx, value);
368361
}
369362

370363
int tsdb_goto_epoch(tsdb_handler *handler,
@@ -393,10 +386,10 @@ int tsdb_goto_epoch(tsdb_handler *handler,
393386

394387
if (rc == -1) {
395388
if (!create_if_needed) {
396-
traceEvent(TRACE_INFO, "Unable to goto epoch %u", epoch_value);
389+
trace_info("Unable to goto epoch %u", epoch_value);
397390
return -1;
398391
} else {
399-
traceEvent(TRACE_INFO, "Moving to goto epoch %u", epoch_value);
392+
trace_info("Moving to goto epoch %u", epoch_value);
400393
}
401394

402395
/* Create the entry */
@@ -408,8 +401,8 @@ int tsdb_goto_epoch(tsdb_handler *handler,
408401
(u_int8_t*)malloc(handler->chunk.chunk_mem_len);
409402

410403
if (handler->chunk.chunk_mem == NULL) {
411-
traceEvent(TRACE_WARNING, "Not enough memory (%u bytes)",
412-
handler->chunk.chunk_mem_len);
404+
trace_warning("Not enough memory (%u bytes)",
405+
handler->chunk.chunk_mem_len);
413406
return -2;
414407
}
415408

@@ -428,8 +421,8 @@ int tsdb_goto_epoch(tsdb_handler *handler,
428421

429422
ptr = (u_int8_t*)malloc(handler->chunk.chunk_mem_len + len);
430423
if (ptr == NULL) {
431-
traceEvent(TRACE_WARNING, "Not enough memory (%u bytes)",
432-
handler->chunk.chunk_mem_len+len);
424+
trace_warning("Not enough memory (%u bytes)",
425+
handler->chunk.chunk_mem_len+len);
433426
free(value);
434427
return -2;
435428
}
@@ -442,8 +435,7 @@ int tsdb_goto_epoch(tsdb_handler *handler,
442435
len = qlz_decompress(value, &ptr[offset],
443436
&handler->state_decompress);
444437

445-
traceEvent(TRACE_NORMAL,
446-
"Decompression %u -> %u [fragment %u] [%.1f %%]",
438+
trace_info("Decompression %u -> %u [fragment %u] [%.1f %%]",
447439
value_len, len, fragment_id,
448440
((float)(len*100))/((float)value_len));
449441

@@ -458,29 +450,29 @@ int tsdb_goto_epoch(tsdb_handler *handler,
458450
&value, &value_len) == -1)
459451
break; /* No more fragments */
460452
}
461-
453+
462454
handler->chunk.begin_epoch = epoch_value;
463455
handler->chunk.num_hash_indexes =
464456
handler->chunk.chunk_mem_len / handler->values_len;
465457

466-
traceEvent(TRACE_INFO, "Moved to goto epoch %u", epoch_value);
458+
trace_info("Moved to goto epoch %u", epoch_value);
467459
}
468460

469461
handler->chunk.growable = growable;
470-
462+
471463
return 0;
472464
}
473465

474466
static int mapIndexToHash(tsdb_handler *handler, char *idx,
475467
u_int32_t *value, u_int8_t create_idx_if_needed) {
476468
/* Check if this is a known value */
477469
if (get_map_hash_index(handler, idx, value) == 0) {
478-
traceEvent(TRACE_INFO, "Index %s mapped to hash %u", idx, *value);
470+
trace_info("Index %s mapped to hash %u", idx, *value);
479471
return 0;
480472
}
481473

482474
if (!create_idx_if_needed) {
483-
traceEvent(TRACE_INFO, "Unable to find index %s", idx);
475+
trace_info("Unable to find index %s", idx);
484476
return -1;
485477
}
486478

@@ -503,10 +495,10 @@ static int getOffset(tsdb_handler *handler, char *hash_name,
503495

504496
if (mapIndexToHash(handler, hash_name, &hash_index,
505497
create_idx_if_needed) == -1) {
506-
traceEvent(TRACE_INFO, "Unable to find index %s", hash_name);
498+
trace_info("Unable to find index %s", hash_name);
507499
return -1;
508500
} else {
509-
traceEvent(TRACE_INFO, "%s mapped to idx %u", hash_name, hash_index);
501+
trace_info("%s mapped to idx %u", hash_name, hash_index);
510502
}
511503

512504
if (handler->chunk.load_page_on_demand || handler->read_only_mode) {
@@ -527,8 +519,8 @@ static int getOffset(tsdb_handler *handler, char *hash_name,
527519
handler->chunk.chunk_mem =
528520
(u_int8_t*)malloc(handler->chunk.chunk_mem_len);
529521
if (handler->chunk.chunk_mem == NULL) {
530-
traceEvent(TRACE_WARNING, "Not enough memory (%u bytes)",
531-
handler->chunk.chunk_mem_len);
522+
trace_warning("Not enough memory (%u bytes)",
523+
handler->chunk.chunk_mem_len);
532524
return -2;
533525
}
534526

@@ -548,10 +540,10 @@ static int getOffset(tsdb_handler *handler, char *hash_name,
548540

549541
if (hash_index >= handler->chunk.num_hash_indexes) {
550542
if (!handler->chunk.growable) {
551-
traceEvent(TRACE_ERROR, "Index %u out of range %u...%u",
552-
hash_index,
553-
0,
554-
handler->chunk.num_hash_indexes);
543+
trace_error("Index %u out of range %u...%u",
544+
hash_index,
545+
0,
546+
handler->chunk.num_hash_indexes);
555547
return -1;
556548
} else {
557549
u_int32_t to_add = CHUNK_GROWTH * handler->values_len;
@@ -564,30 +556,30 @@ static int getOffset(tsdb_handler *handler, char *hash_name,
564556
memset(&ptr[handler->chunk.chunk_mem_len],
565557
handler->default_unknown_value, to_add);
566558
handler->chunk.num_hash_indexes += CHUNK_GROWTH;
567-
handler->chunk.chunk_mem = ptr,
559+
handler->chunk.chunk_mem = ptr,
568560
handler->chunk.chunk_mem_len = new_len;
569561

570-
traceEvent(TRACE_INFO, "Grown table to %u elements",
562+
trace_info("Grown table to %u elements",
571563
handler->chunk.num_hash_indexes);
572564

573565
goto redo_getOffset;
574566
} else {
575-
traceEvent(TRACE_WARNING,
576-
"Not enough memory (%u bytes): unable to grow table",
577-
new_len);
567+
trace_warning("Not enough memory (%u bytes): unable to grow "
568+
"table", new_len);
578569
return -2;
579570
}
580571
}
581572
}
582573

583-
/* All hashes of one day are one attached to the other: fast insert,
574+
/* All hashes of one day are one attached to the other: fast insert,
584575
slow data extraction */
585576
*offset = handler->values_len * hash_index;
586577

587-
if (*offset >= handler->chunk.chunk_mem_len)
588-
traceEvent(TRACE_ERROR, "INTERNAL ERROR [Id: %s/%u][Offset: %u/%u]",
589-
hash_name, hash_index, *offset,
590-
handler->chunk.chunk_mem_len);
578+
if (*offset >= handler->chunk.chunk_mem_len) {
579+
trace_error("INTERNAL ERROR [Id: %s/%u][Offset: %u/%u]",
580+
hash_name, hash_index, *offset,
581+
handler->chunk.chunk_mem_len);
582+
}
591583

592584
return 0;
593585
}
@@ -605,7 +597,7 @@ int tsdb_set(tsdb_handler *handler,
605597

606598
if ((!handler->chunk.load_page_on_demand)
607599
&& (handler->chunk.chunk_mem == NULL)) {
608-
traceEvent(TRACE_ERROR, "Missing epoch");
600+
trace_error("Missing epoch");
609601
return -2;
610602
}
611603

@@ -618,13 +610,13 @@ int tsdb_set(tsdb_handler *handler,
618610
/* Mark a fragment as changed */
619611

620612
if (fragment_id > MAX_NUM_FRAGMENTS) {
621-
traceEvent(TRACE_ERROR, "Internal error [%u > %u]",
622-
fragment_id, MAX_NUM_FRAGMENTS);
613+
trace_error("Internal error [%u > %u]",
614+
fragment_id, MAX_NUM_FRAGMENTS);
623615
} else {
624616
handler->chunk.fragment_changed[fragment_id] = 1;
625617
}
626618
}
627-
619+
628620
return rc;
629621
}
630622

@@ -642,7 +634,7 @@ int tsdb_get(tsdb_handler *handler,
642634

643635
if ((!handler->chunk.load_page_on_demand)
644636
&& (handler->chunk.chunk_mem == NULL)) {
645-
traceEvent(TRACE_ERROR, "Missing epoch");
637+
trace_error("Missing epoch");
646638
return -2;
647639
}
648640

tsdb_create.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ typedef struct {
88
} create_args;
99

1010
static void init_trace(int verbose) {
11-
traceLevel = verbose ? 99 : 0;
11+
set_trace_level(verbose ? 99 : 0);
1212
}
1313

1414
static void help(int code) {

tsdb_get.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static void check_file_exists(const char *path) {
124124
}
125125

126126
static void init_trace(int verbose) {
127-
traceLevel = verbose ? 99 : 0;
127+
set_trace_level(verbose ? 99 : 0);
128128
}
129129

130130
static void open_db(char *file, tsdb_handler *db) {

tsdb_info.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,10 @@ static void print_db_info(char *file) {
6464
tsdb_close(&db);
6565
}
6666

67-
static void init_trace() {
68-
traceLevel = 0;
69-
}
70-
7167
int main(int argc, char *argv[]) {
7268
info_args args;
7369

74-
init_trace();
70+
set_trace_level(0);
7571
process_args(argc, argv, &args);
7672
check_file_exists(args.file);
7773
print_db_info(args.file);

tsdb_set.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static void set_tsdb_values(set_args *args) {
115115
}
116116

117117
static void init_trace(int verbose) {
118-
traceLevel = verbose ? 99 : 0;
118+
set_trace_level(verbose ? 99 : 0);
119119
}
120120

121121
int main(int argc, char *argv[]) {

0 commit comments

Comments
 (0)