-
Notifications
You must be signed in to change notification settings - Fork 22
/
debug.c
199 lines (188 loc) · 7.32 KB
/
debug.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
192
193
194
195
196
197
198
199
/*
* This file is part of the nqptp distribution (https://github.com/mikebrady/nqptp).
* Copyright (c) 2021 Mike Brady.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Commercial licensing is also available.
*/
#include "debug.h"
#include <inttypes.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
int debuglev = 0;
int debugger_show_elapsed_time = 0;
int debugger_show_relative_time = 0;
int debugger_show_file_and_line = 1;
uint64_t ns_time_at_startup = 0;
uint64_t ns_time_at_last_debug_message;
// always lock use this when accessing the ns_time_at_last_debug_message
static pthread_mutex_t debug_timing_lock = PTHREAD_MUTEX_INITIALIZER;
uint64_t get_absolute_time_in_ns() {
uint64_t time_now_ns;
struct timespec tn;
// CLOCK_REALTIME because PTP uses it.
clock_gettime(CLOCK_REALTIME, &tn);
uint64_t tnnsec = tn.tv_sec;
tnnsec = tnnsec * 1000000000;
uint64_t tnjnsec = tn.tv_nsec;
time_now_ns = tnnsec + tnjnsec;
return time_now_ns;
}
void debug_init(int level, int show_elapsed_time, int show_relative_time, int show_file_and_line) {
ns_time_at_startup = get_absolute_time_in_ns();
ns_time_at_last_debug_message = ns_time_at_startup;
debuglev = level;
debugger_show_elapsed_time = show_elapsed_time;
debugger_show_relative_time = show_relative_time;
debugger_show_file_and_line = show_file_and_line;
}
char *generate_preliminary_string(char *buffer, size_t buffer_length, double tss, double tsl,
const char *filename, const int linenumber, const char *prefix) {
size_t space_remaining = buffer_length;
char *insertion_point = buffer;
if (debugger_show_elapsed_time) {
snprintf(insertion_point, space_remaining, "% 20.9f", tss);
insertion_point = insertion_point + strlen(insertion_point);
space_remaining = space_remaining - strlen(insertion_point);
}
if (debugger_show_relative_time) {
snprintf(insertion_point, space_remaining, "% 20.9f", tsl);
insertion_point = insertion_point + strlen(insertion_point);
space_remaining = space_remaining - strlen(insertion_point);
}
if (debugger_show_file_and_line) {
snprintf(insertion_point, space_remaining, " \"%s:%d\"", filename, linenumber);
insertion_point = insertion_point + strlen(insertion_point);
space_remaining = space_remaining - strlen(insertion_point);
}
if (prefix) {
snprintf(insertion_point, space_remaining, "%s", prefix);
insertion_point = insertion_point + strlen(insertion_point);
space_remaining = space_remaining - strlen(insertion_point);
}
return insertion_point;
}
void _die(const char *filename, const int linenumber, const char *format, ...) {
int oldState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
char b[1024];
b[0] = 0;
char *s;
if (debuglev) {
pthread_mutex_lock(&debug_timing_lock);
uint64_t time_now = get_absolute_time_in_ns();
uint64_t time_since_start = time_now - ns_time_at_startup;
uint64_t time_since_last_debug_message = time_now - ns_time_at_last_debug_message;
ns_time_at_last_debug_message = time_now;
pthread_mutex_unlock(&debug_timing_lock);
s = generate_preliminary_string(b, sizeof(b), 1.0 * time_since_start / 1000000000,
1.0 * time_since_last_debug_message / 1000000000, filename,
linenumber, " *fatal error: ");
} else {
strncpy(b, "fatal error: ", sizeof(b));
s = b + strlen(b);
}
va_list args;
va_start(args, format);
vsnprintf(s, sizeof(b) - (s - b), format, args);
va_end(args);
// syslog(LOG_ERR, "%s", b);
fprintf(stderr, "%s\n", b);
pthread_setcancelstate(oldState, NULL);
exit(EXIT_FAILURE);
}
void _warn(const char *filename, const int linenumber, const char *format, ...) {
int oldState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
char b[1024];
b[0] = 0;
char *s;
if (debuglev) {
pthread_mutex_lock(&debug_timing_lock);
uint64_t time_now = get_absolute_time_in_ns();
uint64_t time_since_start = time_now - ns_time_at_startup;
uint64_t time_since_last_debug_message = time_now - ns_time_at_last_debug_message;
ns_time_at_last_debug_message = time_now;
pthread_mutex_unlock(&debug_timing_lock);
s = generate_preliminary_string(b, sizeof(b), 1.0 * time_since_start / 1000000000,
1.0 * time_since_last_debug_message / 1000000000, filename,
linenumber, " *warning: ");
} else {
strncpy(b, "warning: ", sizeof(b));
s = b + strlen(b);
}
va_list args;
va_start(args, format);
vsnprintf(s, sizeof(b) - (s - b), format, args);
va_end(args);
// syslog(LOG_WARNING, "%s", b);
fprintf(stderr, "%s\n", b);
pthread_setcancelstate(oldState, NULL);
}
void _debug(const char *filename, const int linenumber, int level, const char *format, ...) {
if (level > debuglev)
return;
int oldState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
char b[1024];
b[0] = 0;
pthread_mutex_lock(&debug_timing_lock);
uint64_t time_now = get_absolute_time_in_ns();
uint64_t time_since_start = time_now - ns_time_at_startup;
uint64_t time_since_last_debug_message = time_now - ns_time_at_last_debug_message;
ns_time_at_last_debug_message = time_now;
pthread_mutex_unlock(&debug_timing_lock);
char *s = generate_preliminary_string(b, sizeof(b), 1.0 * time_since_start / 1000000000,
1.0 * time_since_last_debug_message / 1000000000, filename,
linenumber, " ");
va_list args;
va_start(args, format);
vsnprintf(s, sizeof(b) - (s - b), format, args);
va_end(args);
// syslog(LOG_DEBUG, "%s", b);
fprintf(stderr, "%s\n", b);
pthread_setcancelstate(oldState, NULL);
}
void _inform(const char *filename, const int linenumber, const char *format, ...) {
int oldState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
char b[1024];
b[0] = 0;
char *s;
if (debuglev) {
pthread_mutex_lock(&debug_timing_lock);
uint64_t time_now = get_absolute_time_in_ns();
uint64_t time_since_start = time_now - ns_time_at_startup;
uint64_t time_since_last_debug_message = time_now - ns_time_at_last_debug_message;
ns_time_at_last_debug_message = time_now;
pthread_mutex_unlock(&debug_timing_lock);
s = generate_preliminary_string(b, sizeof(b), 1.0 * time_since_start / 1000000000,
1.0 * time_since_last_debug_message / 1000000000, filename,
linenumber, " ");
} else {
s = b;
}
va_list args;
va_start(args, format);
vsnprintf(s, sizeof(b) - (s - b), format, args);
va_end(args);
// syslog(LOG_INFO, "%s", b);
fprintf(stderr, "%s\n", b);
pthread_setcancelstate(oldState, NULL);
}