Skip to content

Commit

Permalink
Merge pull request #93 from ibm-genwqe/add_logfile
Browse files Browse the repository at this point in the history
Adding zlib logfile support to remove the stderr dependency
  • Loading branch information
fhaverkamp committed Mar 17, 2016
2 parents fc4ea28 + 1b32efe commit 108e975
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
14 changes: 7 additions & 7 deletions lib/hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,11 @@ static inline int __check_stream_end(z_streamp strm)

hw_trace("Accumulated input data:\n");
if (zlib_hw_trace_enabled())
ddcb_hexdump(stderr, e.d, e.avail_in);
ddcb_hexdump(zlib_log, e.d, e.avail_in);

/* Now let us have a look what we have here */
while (1) {
/* fprintf(stderr, "STATE: %s\n", state_str[e.state]); */
/* fprintf(zlib_log, "STATE: %s\n", state_str[e.state]); */
switch (e.state) {
case READ_HDR:
rc = get_bits(&e, 3, &d);
Expand Down Expand Up @@ -1298,13 +1298,13 @@ int h_inflate(z_streamp strm, int flush)
rc = Z_OK;
#ifdef CONFIG_CIRCUMVENTION_FOR_Z_STREAM_END /* EXPERIMENTAL for MongoDB PoC */
/*
* fprintf(stderr, "SCRATCH\n");
* ddcb_hexdump(stderr, h->wsp->tree,
* fprintf(zlib_log, "SCRATCH\n");
* ddcb_hexdump(zlib_log, h->wsp->tree,
* __in_hdr_scratch_len(h));
* fprintf(stderr, "NEXT_IN\n");
* ddcb_hexdump(stderr, strm->next_in,
* fprintf(zlib_log, "NEXT_IN\n");
* ddcb_hexdump(zlib_log, strm->next_in,
* MIN(strm->avail_in, (unsigned int)0x20));
* fprintf(stderr,
* fprintf(zlib_log,
* " in_hdr_scratch_len = %d\n"
* " proc_bits = %d\n",
* __in_hdr_scratch_len(h), h->proc_bits);
Expand Down
23 changes: 18 additions & 5 deletions lib/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
/* Good values are something like 8KiB or 16KiB */
#define CONFIG_INFLATE_THRESHOLD (16 * 1024) /* 0: disabled */

int zlib_trace = 0x0;

int zlib_trace = 0x0; /* no trace by default */
FILE *zlib_log = NULL; /* default is stderr, unless overwritten */
int zlib_accelerator = DDCB_TYPE_GENWQE;
int zlib_card = -1; /* Using redundant now as default */

Expand Down Expand Up @@ -235,8 +235,16 @@ static void _init(void)
{
int rc;
const char *trace, *inflate_impl, *deflate_impl, *method;
const char *zlib_logfile = NULL;
char *inflate_threshold;

zlib_logfile = getenv("ZLIB_LOGFILE");
if (zlib_logfile != NULL) {
zlib_log = fopen(zlib_logfile, "a+");
if (zlib_log == NULL)
zlib_log = stderr;
} else zlib_log = stderr;

trace = getenv("ZLIB_TRACE");
if (trace != NULL)
zlib_trace = strtol(trace, (char **)NULL, 0);
Expand Down Expand Up @@ -761,8 +769,8 @@ int deflate(z_streamp strm, int flush)
}

pr_trace("[%p] deflate: flush=%d %s next_in=%p avail_in=%d "
"next_out=%p avail_out=%d total_out=%ld crc/adler=%08lx impl=%d\n",
strm, flush, flush_to_str(flush), strm->next_in,
"next_out=%p avail_out=%d total_out=%ld crc/adler=%08lx "
"impl=%d\n", strm, flush, flush_to_str(flush), strm->next_in,
strm->avail_in, strm->next_out, strm->avail_out,
strm->total_out, strm->adler, w->impl);

Expand All @@ -776,7 +784,8 @@ int deflate(z_streamp strm, int flush)
rc = z_deflate(strm, flush);
break;
default:
pr_trace("[%p] deflate: impl (%d) is not valid for me\n", strm, w->impl);
pr_trace("[%p] deflate: impl (%d) is not valid for me\n",
strm, w->impl);
break;
}
strm->state = (void *)w;
Expand Down Expand Up @@ -1518,5 +1527,9 @@ static void _done(void)

zedc_hw_done();
zedc_sw_done();

if (zlib_log != stderr)
fclose(zlib_log);

return;
}
15 changes: 9 additions & 6 deletions lib/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/

#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
#include <zaddons.h>

Expand All @@ -39,44 +40,46 @@
# define __unused __attribute__((unused))
#endif

extern FILE *zlib_log;

#define zlib_trace_enabled() (zlib_trace & 0x1)
#define zlib_hw_trace_enabled() (zlib_trace & 0x2)
#define zlib_sw_trace_enabled() (zlib_trace & 0x4)
#define zlib_gather_statistics() (zlib_trace & 0x8)

/* Use in case of an error */
#define pr_err(fmt, ...) do { \
fprintf(stderr, "%s:%u: Error: " fmt, \
fprintf(zlib_log, "%s:%u: Error: " fmt, \
__FILE__, __LINE__, ## __VA_ARGS__); \
} while (0)

/* Use in case of an warning */
#define pr_warn(fmt, ...) do { \
fprintf(stderr, "%s:%u: Warning: " fmt, \
fprintf(zlib_log, "%s:%u: Warning: " fmt, \
__FILE__, __LINE__, ## __VA_ARGS__); \
} while (0)

/* Informational printouts */
#define pr_info(fmt, ...) do { \
fprintf(stderr, "Info: " fmt, ## __VA_ARGS__); \
fprintf(zlib_log, "Info: " fmt, ## __VA_ARGS__); \
} while (0)

/* Trace zlib wrapper code */
#define pr_trace(fmt, ...) do { \
if (zlib_trace_enabled()) \
fprintf(stderr, "### " fmt, ## __VA_ARGS__); \
fprintf(zlib_log, "### " fmt, ## __VA_ARGS__); \
} while (0)

/* Trace zlib hardware implementation */
#define hw_trace(fmt, ...) do { \
if (zlib_hw_trace_enabled()) \
fprintf(stderr, "hhh " fmt, ## __VA_ARGS__); \
fprintf(zlib_log, "hhh " fmt, ## __VA_ARGS__); \
} while (0)

/* Trace zlib software implementation */
#define sw_trace(fmt, ...) do { \
if (zlib_sw_trace_enabled()) \
fprintf(stderr, "sss " fmt, ## __VA_ARGS__); \
fprintf(zlib_log, "sss " fmt, ## __VA_ARGS__); \
} while (0)

#define Z_UNSUPPORTED (-7)
Expand Down

0 comments on commit 108e975

Please sign in to comment.