-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_handle.c
191 lines (165 loc) · 4.08 KB
/
debug_handle.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* debug_handle.c
* This is to set up debug level and handle debug functions.
* Created on: Aug 6, 2015
* Author: tientham
*/
#include "serial_ip.h"
char hostname[MAXHOSTNAME_LEN+1];
char *myprogname = NULL;
FILE *debugfp = NULL;
int debug_level = 0; /* disabled */
/*
Location: debug_handle.c
This function is to dump a line for the buffer then write it to debug file.
*/
void debug_linedump(char *buf, int len, FILE *stream)
{
int i; /* gp int */
char *p; /* buf ptr */
if ((stream == NULL) || (buf == NULL))
return;
if (len < 1) return;
p = buf; /* point to buffer */
for (i = 0; i < len; i++,p++) /* output hex part of dump */
fprintf(stream,"%02x ", (unsigned char) *p); /*%02x prints at least 2 digits, if there is 1 digit input, append 0 before.*/
for (i = len+1; i <= CHUNK; i++) /* pad w/spaces if (len < CHUNK) */
fprintf(stream," ");
fprintf(stream," "); /* add spaces for separation */
p = buf; /* point to buffer */
for (i = 0; i < len; i++,p++) /* output character part of dump */
fprintf(stream,"%c",isprint(*p) ? *p : '.');
fprintf(stream,"\n"); /* add newline */
if (! ferror(stream))
fflush(stream);
}
/*
Location: debug_handle.c
This is to write the error message for the current system error to the debug log.
*/
void debug_perror(char *str)
{
extern int errno;
if (errno > 0) {
if ((str == NULL) || (*str == '\0')) {
write_to_debuglog(DBG_ERR,"%s",strerror(errno));
} else {
write_to_debuglog(DBG_ERR,"%s: %s",str,strerror(errno));
}
}
}
/*
Location: debug_handle.c
This is to open the debug log in append mode.
returns the stream pointer or NULL on error.
*/
FILE *open_debug(char *file, int level, char *program)
{
extern char hostname[];
extern FILE *debugfp;
extern char *myprogname;
extern int debug_level;
FILE *fp;
//time_t seconds;
char *p;
if ((file != NULL) && (*file != '\0')) {
fp = fopen(file,"a");
if (fp != NULL) {
debugfp = fp; /* save in global variable */
}
} else {
debugfp = fp = NULL; /* direct debug output to syslog */
}
debug_level = level; /* save these in global variables */
myprogname = program;
/*
get our host name and truncate it at the first '.'
*/
if (gethostname(hostname,MAXHOSTNAME_LEN) == 0) {
if ((p = strchr(hostname,'.')) != NULL) {
*p = '\0';
}
} else {
strcpy(hostname,"localhost");
}
return(fp);
}
/*
Location: debug_handle.c
Depend on the serial_ip.conf, we can choose 1 of 2 style for systemlog with syslog() or the debug log
This function doesnot return.
*/
void write_to_debuglog(int level, char *fmt, ...)
{
extern char hostname[];
extern FILE *debugfp;
extern char *myprogname;
extern int debug_level;
static char str[1024];
va_list args;
/* sanity checks */
if (level > debug_level) return;
if (fmt == NULL) return;
va_start(args,fmt);
/* debug pointer # NULL when the server has already run.*/
if (debugfp != NULL) {
fprintf(debugfp,"%s %s %s[%d]: ", get_timestamp(),hostname, myprogname, getpid());
vfprintf(debugfp, fmt, args);
fputc('\n', debugfp);
if (! ferror(debugfp)) {
fflush(debugfp);
} else {
close_debug();
}
} else {
/*Particularly using for the inizialization period.*/
vsnprintf(str, sizeof(str), fmt, args);
if (level == DBG_ERR) {
syslog(LOG_ERR,"%s",str);
} else if (level <= DBG_INF) {
syslog(LOG_INFO,"%s",str);
} else {
syslog(LOG_DEBUG,"%s",str);
}
}
va_end(args);
}
/*
Location: debug_handle.c
This function is close debug log.
Non return.
*/
void close_debug(void)
{
extern FILE *debugfp;
extern char *myprogname;
extern int debug_level;
if (debugfp != NULL) {
fclose(debugfp);
debugfp = NULL;
}
debug_level = 0;
myprogname = NULL;
}
/*
Location: debug_handle.c
This function is set level for debug mode.
Non return.
*/
void set_debug_level(int new_level)
{
extern int debug_level;
if ((new_level >= DBG_LV0) && (new_level <= DBG_LV9)) {
debug_level = new_level;
}
}
/*
Location: debug_handle.c
This function is get level for debug mode.
Non return.
*/
int get_debug_level()
{
extern int debug_level;
return(debug_level);
}