forked from Silicondust/libhdhomerun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hdhomerun_debug.c
460 lines (374 loc) · 9.72 KB
/
hdhomerun_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* hdhomerun_debug.c
*
* Copyright © 2007-2016 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* The debug logging includes optional support for connecting to the
* Silicondust support server. This option should not be used without
* being explicitly enabled by the user. Debug information should be
* limited to information useful to diagnosing a problem.
* - Silicondust.
*/
#include "hdhomerun.h"
#if !defined(HDHOMERUN_DEBUG_HOST)
#define HDHOMERUN_DEBUG_HOST "debug.silicondust.com"
#endif
#if !defined(HDHOMERUN_DEBUG_PORT)
#define HDHOMERUN_DEBUG_PORT 8002
#endif
#define HDHOMERUN_DEBUG_CONNECT_RETRY_TIME 30000
#define HDHOMERUN_DEBUG_CONNECT_TIMEOUT 10000
#define HDHOMERUN_DEBUG_SEND_TIMEOUT 10000
struct hdhomerun_debug_message_t
{
struct hdhomerun_debug_message_t *next;
char buffer[2048];
};
struct hdhomerun_debug_t
{
thread_task_t thread;
volatile bool enabled;
volatile bool terminate;
char *prefix;
thread_mutex_t print_lock;
thread_mutex_t queue_lock;
thread_mutex_t send_lock;
thread_cond_t queue_cond;
struct hdhomerun_debug_message_t *queue_head;
struct hdhomerun_debug_message_t *queue_tail;
uint32_t queue_depth;
uint64_t connect_delay;
char *file_name;
FILE *file_fp;
struct hdhomerun_sock_t *sock;
};
static void hdhomerun_debug_thread_execute(void *arg);
struct hdhomerun_debug_t *hdhomerun_debug_create(void)
{
struct hdhomerun_debug_t *dbg = (struct hdhomerun_debug_t *)calloc(1, sizeof(struct hdhomerun_debug_t));
if (!dbg) {
return NULL;
}
thread_mutex_init(&dbg->print_lock);
thread_mutex_init(&dbg->queue_lock);
thread_mutex_init(&dbg->send_lock);
thread_cond_init(&dbg->queue_cond);
if (!thread_task_create(&dbg->thread, &hdhomerun_debug_thread_execute, dbg)) {
free(dbg);
return NULL;
}
return dbg;
}
void hdhomerun_debug_destroy(struct hdhomerun_debug_t *dbg)
{
if (!dbg) {
return;
}
dbg->terminate = true;
thread_cond_signal(&dbg->queue_cond);
thread_task_join(dbg->thread);
if (dbg->prefix) {
free(dbg->prefix);
}
if (dbg->file_name) {
free(dbg->file_name);
}
if (dbg->file_fp) {
fclose(dbg->file_fp);
}
if (dbg->sock) {
hdhomerun_sock_destroy(dbg->sock);
}
thread_cond_dispose(&dbg->queue_cond);
thread_mutex_dispose(&dbg->print_lock);
thread_mutex_dispose(&dbg->queue_lock);
thread_mutex_dispose(&dbg->send_lock);
free(dbg);
}
/* Send lock held by caller */
static void hdhomerun_debug_close_internal(struct hdhomerun_debug_t *dbg)
{
if (dbg->file_fp) {
fclose(dbg->file_fp);
dbg->file_fp = NULL;
}
if (dbg->sock) {
hdhomerun_sock_destroy(dbg->sock);
dbg->sock = NULL;
}
}
void hdhomerun_debug_close(struct hdhomerun_debug_t *dbg, uint64_t timeout)
{
if (!dbg) {
return;
}
if (timeout > 0) {
hdhomerun_debug_flush(dbg, timeout);
}
thread_mutex_lock(&dbg->send_lock);
hdhomerun_debug_close_internal(dbg);
dbg->connect_delay = 0;
thread_mutex_unlock(&dbg->send_lock);
}
void hdhomerun_debug_set_filename(struct hdhomerun_debug_t *dbg, const char *filename)
{
if (!dbg) {
return;
}
thread_mutex_lock(&dbg->send_lock);
if (!filename && !dbg->file_name) {
thread_mutex_unlock(&dbg->send_lock);
return;
}
if (filename && dbg->file_name) {
if (strcmp(filename, dbg->file_name) == 0) {
thread_mutex_unlock(&dbg->send_lock);
return;
}
}
hdhomerun_debug_close_internal(dbg);
dbg->connect_delay = 0;
if (dbg->file_name) {
free(dbg->file_name);
dbg->file_name = NULL;
}
if (filename) {
dbg->file_name = strdup(filename);
}
thread_mutex_unlock(&dbg->send_lock);
}
void hdhomerun_debug_set_prefix(struct hdhomerun_debug_t *dbg, const char *prefix)
{
if (!dbg) {
return;
}
thread_mutex_lock(&dbg->print_lock);
if (dbg->prefix) {
free(dbg->prefix);
dbg->prefix = NULL;
}
if (prefix) {
dbg->prefix = strdup(prefix);
}
thread_mutex_unlock(&dbg->print_lock);
}
void hdhomerun_debug_enable(struct hdhomerun_debug_t *dbg)
{
if (!dbg) {
return;
}
if (dbg->enabled) {
return;
}
dbg->enabled = true;
thread_cond_signal(&dbg->queue_cond);
}
void hdhomerun_debug_disable(struct hdhomerun_debug_t *dbg)
{
if (!dbg) {
return;
}
dbg->enabled = false;
}
bool hdhomerun_debug_enabled(struct hdhomerun_debug_t *dbg)
{
if (!dbg) {
return false;
}
return dbg->enabled;
}
void hdhomerun_debug_flush(struct hdhomerun_debug_t *dbg, uint64_t timeout)
{
if (!dbg) {
return;
}
timeout = getcurrenttime() + timeout;
while (getcurrenttime() < timeout) {
thread_mutex_lock(&dbg->queue_lock);
struct hdhomerun_debug_message_t *message = dbg->queue_head;
thread_mutex_unlock(&dbg->queue_lock);
if (!message) {
return;
}
msleep_approx(16);
}
}
void hdhomerun_debug_printf(struct hdhomerun_debug_t *dbg, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
hdhomerun_debug_vprintf(dbg, fmt, args);
va_end(args);
}
void hdhomerun_debug_vprintf(struct hdhomerun_debug_t *dbg, const char *fmt, va_list args)
{
if (!dbg) {
return;
}
struct hdhomerun_debug_message_t *message = (struct hdhomerun_debug_message_t *)malloc(sizeof(struct hdhomerun_debug_message_t));
if (!message) {
return;
}
message->next = NULL;
char *ptr = message->buffer;
char *end = message->buffer + sizeof(message->buffer) - 2;
*end = 0;
/*
* Timestamp.
*/
time_t current_time = time(NULL);
ptr += strftime(ptr, end - ptr, "%Y%m%d-%H:%M:%S ", localtime(¤t_time));
if (ptr > end) {
ptr = end;
}
/*
* Debug prefix.
*/
thread_mutex_lock(&dbg->print_lock);
if (dbg->prefix) {
hdhomerun_sprintf(ptr, end, "%s ", dbg->prefix);
ptr = strchr(ptr, 0);
}
thread_mutex_unlock(&dbg->print_lock);
/*
* Message text.
*/
hdhomerun_vsprintf(ptr, end, fmt, args);
ptr = strchr(ptr, 0);
/*
* Force newline.
*/
if (ptr[-1] != '\n') {
hdhomerun_sprintf(ptr, end, "\n");
}
/*
* Enqueue.
*/
thread_mutex_lock(&dbg->queue_lock);
if (dbg->queue_tail) {
dbg->queue_tail->next = message;
} else {
dbg->queue_head = message;
}
dbg->queue_tail = message;
dbg->queue_depth++;
bool signal_thread = dbg->enabled || (dbg->queue_depth > 1024 + 100);
thread_mutex_unlock(&dbg->queue_lock);
if (signal_thread) {
thread_cond_signal(&dbg->queue_cond);
}
}
/* Send lock held by caller */
static bool hdhomerun_debug_output_message_file(struct hdhomerun_debug_t *dbg, struct hdhomerun_debug_message_t *message)
{
if (!dbg->file_fp) {
uint64_t current_time = getcurrenttime();
if (current_time < dbg->connect_delay) {
return false;
}
dbg->connect_delay = current_time + 30*1000;
dbg->file_fp = fopen(dbg->file_name, "a");
if (!dbg->file_fp) {
return false;
}
}
fprintf(dbg->file_fp, "%s", message->buffer);
fflush(dbg->file_fp);
return true;
}
/* Send lock held by caller */
static bool hdhomerun_debug_output_message_sock(struct hdhomerun_debug_t *dbg, struct hdhomerun_debug_message_t *message)
{
if (!dbg->sock) {
uint64_t current_time = getcurrenttime();
if (current_time < dbg->connect_delay) {
return false;
}
dbg->connect_delay = current_time + HDHOMERUN_DEBUG_CONNECT_RETRY_TIME;
dbg->sock = hdhomerun_sock_create_tcp();
if (!dbg->sock) {
return false;
}
uint32_t remote_addr = hdhomerun_sock_getaddrinfo_addr(dbg->sock, HDHOMERUN_DEBUG_HOST);
if (remote_addr == 0) {
hdhomerun_debug_close_internal(dbg);
return false;
}
if (!hdhomerun_sock_connect(dbg->sock, remote_addr, HDHOMERUN_DEBUG_PORT, HDHOMERUN_DEBUG_CONNECT_TIMEOUT)) {
hdhomerun_debug_close_internal(dbg);
return false;
}
}
size_t length = strlen(message->buffer);
if (!hdhomerun_sock_send(dbg->sock, message->buffer, length, HDHOMERUN_DEBUG_SEND_TIMEOUT)) {
hdhomerun_debug_close_internal(dbg);
return false;
}
return true;
}
static bool hdhomerun_debug_output_message(struct hdhomerun_debug_t *dbg, struct hdhomerun_debug_message_t *message)
{
thread_mutex_lock(&dbg->send_lock);
bool ret;
if (dbg->file_name) {
ret = hdhomerun_debug_output_message_file(dbg, message);
} else {
ret = hdhomerun_debug_output_message_sock(dbg, message);
}
thread_mutex_unlock(&dbg->send_lock);
return ret;
}
static void hdhomerun_debug_pop_and_free_message(struct hdhomerun_debug_t *dbg)
{
thread_mutex_lock(&dbg->queue_lock);
struct hdhomerun_debug_message_t *message = dbg->queue_head;
dbg->queue_head = message->next;
if (!dbg->queue_head) {
dbg->queue_tail = NULL;
}
dbg->queue_depth--;
thread_mutex_unlock(&dbg->queue_lock);
free(message);
}
static void hdhomerun_debug_thread_execute(void *arg)
{
struct hdhomerun_debug_t *dbg = (struct hdhomerun_debug_t *)arg;
while (!dbg->terminate) {
thread_mutex_lock(&dbg->queue_lock);
struct hdhomerun_debug_message_t *message = dbg->queue_head;
uint32_t queue_depth = dbg->queue_depth;
thread_mutex_unlock(&dbg->queue_lock);
if (!message) {
thread_cond_wait(&dbg->queue_cond);
continue;
}
if (queue_depth > 1024) {
hdhomerun_debug_pop_and_free_message(dbg);
continue;
}
if (!dbg->enabled) {
thread_cond_wait(&dbg->queue_cond);
continue;
}
if (!hdhomerun_debug_output_message(dbg, message)) {
msleep_approx(1000);
continue;
}
hdhomerun_debug_pop_and_free_message(dbg);
}
}