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