1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright 2020 IBM Corporation
4  *
5  */
6 
7 #ifndef _NXU_DBG_H_
8 #define _NXU_DBG_H_
9 
10 #include <sys/file.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <time.h>
14 #include <pthread.h>
15 
16 extern FILE * nx_gzip_log;
17 extern int nx_gzip_trace;
18 extern unsigned int nx_gzip_inflate_impl;
19 extern unsigned int nx_gzip_deflate_impl;
20 extern unsigned int nx_gzip_inflate_flags;
21 extern unsigned int nx_gzip_deflate_flags;
22 
23 extern int nx_dbg;
24 pthread_mutex_t mutex_log;
25 
26 #define nx_gzip_trace_enabled()       (nx_gzip_trace & 0x1)
27 #define nx_gzip_hw_trace_enabled()    (nx_gzip_trace & 0x2)
28 #define nx_gzip_sw_trace_enabled()    (nx_gzip_trace & 0x4)
29 #define nx_gzip_gather_statistics()   (nx_gzip_trace & 0x8)
30 #define nx_gzip_per_stream_stat()     (nx_gzip_trace & 0x10)
31 
32 #define prt(fmt, ...) do { \
33 	pthread_mutex_lock(&mutex_log);					\
34 	flock(nx_gzip_log->_fileno, LOCK_EX);				\
35 	time_t t; struct tm *m; time(&t); m = localtime(&t);		\
36 	fprintf(nx_gzip_log, "[%04d/%02d/%02d %02d:%02d:%02d] "		\
37 		"pid %d: " fmt,	\
38 		(int)m->tm_year + 1900, (int)m->tm_mon+1, (int)m->tm_mday, \
39 		(int)m->tm_hour, (int)m->tm_min, (int)m->tm_sec,	\
40 		(int)getpid(), ## __VA_ARGS__);				\
41 	fflush(nx_gzip_log);						\
42 	flock(nx_gzip_log->_fileno, LOCK_UN);				\
43 	pthread_mutex_unlock(&mutex_log);				\
44 } while (0)
45 
46 /* Use in case of an error */
47 #define prt_err(fmt, ...) do { if (nx_dbg >= 0) {			\
48 	prt("%s:%u: Error: "fmt,					\
49 		__FILE__, __LINE__, ## __VA_ARGS__);			\
50 }} while (0)
51 
52 /* Use in case of an warning */
53 #define prt_warn(fmt, ...) do {	if (nx_dbg >= 1) {			\
54 	prt("%s:%u: Warning: "fmt,					\
55 		__FILE__, __LINE__, ## __VA_ARGS__);			\
56 }} while (0)
57 
58 /* Informational printouts */
59 #define prt_info(fmt, ...) do {	if (nx_dbg >= 2) {			\
60 	prt("Info: "fmt, ## __VA_ARGS__);				\
61 }} while (0)
62 
63 /* Trace zlib wrapper code */
64 #define prt_trace(fmt, ...) do { if (nx_gzip_trace_enabled()) {		\
65 	prt("### "fmt, ## __VA_ARGS__);					\
66 }} while (0)
67 
68 /* Trace statistics */
69 #define prt_stat(fmt, ...) do {	if (nx_gzip_gather_statistics()) {	\
70 	prt("### "fmt, ## __VA_ARGS__);					\
71 }} while (0)
72 
73 /* Trace zlib hardware implementation */
74 #define hw_trace(fmt, ...) do {						\
75 		if (nx_gzip_hw_trace_enabled())				\
76 			fprintf(nx_gzip_log, "hhh " fmt, ## __VA_ARGS__); \
77 	} while (0)
78 
79 /* Trace zlib software implementation */
80 #define sw_trace(fmt, ...) do {						\
81 		if (nx_gzip_sw_trace_enabled())				\
82 			fprintf(nx_gzip_log, "sss " fmt, ## __VA_ARGS__); \
83 	} while (0)
84 
85 
86 /**
87  * str_to_num - Convert string into number and copy with endings like
88  *              KiB for kilobyte
89  *              MiB for megabyte
90  *              GiB for gigabyte
91  */
92 uint64_t str_to_num(char *str);
93 void nx_lib_debug(int onoff);
94 
95 #endif	/* _NXU_DBG_H_ */
96