Commit 7eba963477a789aeb5f44e50c30040d261622b86

Kano 2013-06-15T22:03:56

remove varargs from logging/quit/in general as much as possible

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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
diff --git a/cgminer.c b/cgminer.c
index a9cb249..d374462 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -385,13 +385,16 @@ void get_timestamp(char *f, struct timeval *tv)
 		tm->tm_sec);
 }
 
+static char exit_buf[512];
+
 static void applog_and_exit(const char *fmt, ...)
 {
 	va_list ap;
 
 	va_start(ap, fmt);
-	vapplog(LOG_ERR, fmt, ap);
+	vsnprintf(exit_buf, sizeof(exit_buf), fmt, ap);
 	va_end(ap);
+	_applog(LOG_ERR, exit_buf);
 	exit(1);
 }
 
@@ -2233,24 +2236,16 @@ static void switch_logsize(void)
 }
 
 /* For mandatory printing when mutex is already locked */
-void wlog(const char *f, ...)
+void _wlog(const char *str)
 {
-	va_list ap;
-
-	va_start(ap, f);
-	vw_printw(logwin, f, ap);
-	va_end(ap);
+	wprintw(logwin, "%s", str);
 }
 
 /* Mandatory printing */
-void wlogprint(const char *f, ...)
+void _wlogprint(const char *str)
 {
-	va_list ap;
-
 	if (curses_active_locked()) {
-		va_start(ap, f);
-		vw_printw(logwin, f, ap);
-		va_end(ap);
+		wprintw(logwin, "%s", str);
 		unlock_curses();
 	}
 }
@@ -2261,7 +2256,7 @@ static void switch_logsize(void)
 #endif
 
 #ifdef HAVE_CURSES
-bool log_curses_only(int prio, const char *f, va_list ap)
+bool log_curses_only(int prio, const char *datetime, const char *str)
 {
 	bool high_prio;
 
@@ -2269,7 +2264,7 @@ bool log_curses_only(int prio, const char *f, va_list ap)
 
 	if (curses_active_locked()) {
 		if (!opt_loginput || high_prio) {
-			vw_printw(logwin, f, ap);
+			wprintw(logwin, "%s%s\n", datetime, str);
 			if (high_prio) {
 				touchwin(logwin);
 				wrefresh(logwin);
@@ -4242,7 +4237,7 @@ updated:
 	}
 retry:
 	wlogprint("\nCurrent pool management strategy: %s\n",
-		strategies[pool_strategy]);
+		strategies[pool_strategy].s);
 	if (pool_strategy == POOL_ROTATE)
 		wlogprint("Set to rotate every %d minutes\n", opt_rotate_period);
 	wlogprint("[F]ailover only %s\n", opt_fail_only ? "enabled" : "disabled");
@@ -4313,7 +4308,7 @@ retry:
 		goto updated;
 	} else if (!strncasecmp(&input, "c", 1)) {
 		for (i = 0; i <= TOP_STRATEGY; i++)
-			wlogprint("%d: %s\n", i, strategies[i]);
+			wlogprint("%d: %s\n", i, strategies[i].s);
 		selected = curses_int("Select strategy number type");
 		if (selected < 0 || selected > TOP_STRATEGY) {
 			wlogprint("Invalid selection\n");
@@ -6736,15 +6731,8 @@ static void clean_up(void)
 	curl_global_cleanup();
 }
 
-void quit(int status, const char *format, ...)
+void _quit(int status)
 {
-	if (format) {
-		va_list ap;
-		va_start(ap, format);
-		vapplog(LOG_ERR, format, ap);
-		va_end(ap);
-	}
-
 	clean_up();
 
 #if defined(unix) || defined(__APPLE__)
@@ -7180,12 +7168,10 @@ bool add_cgpu(struct cgpu_info *cgpu)
 struct device_drv *copy_drv(struct device_drv *drv)
 {
 	struct device_drv *copy;
-	char buf[100];
 
 	if (unlikely(!(copy = malloc(sizeof(*copy))))) {
-		sprintf(buf, "Failed to allocate device_drv copy of %s (%s)",
+		quit(1, "Failed to allocate device_drv copy of %s (%s)",
 				drv->name, drv->copy ? "copy" : "original");
-		quit(1, buf);
 	}
 	memcpy(copy, drv, sizeof(*copy));
 	copy->copy = true;
diff --git a/driver-avalon.c b/driver-avalon.c
index 367f5a5..23df70f 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -337,7 +337,6 @@ static int avalon_reset(struct cgpu_info *avalon, bool initial)
 static bool get_options(int this_option_offset, int *baud, int *miner_count,
 			int *asic_count, int *timeout, int *frequency)
 {
-	char err_buf[BUFSIZ+1];
 	char buf[BUFSIZ+1];
 	char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
 	size_t max;
@@ -388,10 +387,8 @@ static bool get_options(int this_option_offset, int *baud, int *miner_count,
 		*baud = 19200;
 		break;
 	default:
-		sprintf(err_buf,
-			"Invalid avalon-options for baud (%s) "
+		quit(1, "Invalid avalon-options for baud (%s) "
 			"must be 115200, 57600, 38400 or 19200", buf);
-		quit(1, err_buf);
 	}
 
 	if (colon && *colon) {
@@ -404,11 +401,9 @@ static bool get_options(int this_option_offset, int *baud, int *miner_count,
 			if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
 				*miner_count = tmp;
 			} else {
-				sprintf(err_buf,
-					"Invalid avalon-options for "
+				quit(1, "Invalid avalon-options for "
 					"miner_count (%s) must be 1 ~ %d",
 					colon, AVALON_DEFAULT_MINER_NUM);
-				quit(1, err_buf);
 			}
 		}
 
@@ -421,11 +416,9 @@ static bool get_options(int this_option_offset, int *baud, int *miner_count,
 			if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
 				*asic_count = tmp;
 			else {
-				sprintf(err_buf,
-					"Invalid avalon-options for "
+				quit(1, "Invalid avalon-options for "
 					"asic_count (%s) must be 1 ~ %d",
 					colon2, AVALON_DEFAULT_ASIC_NUM);
-				quit(1, err_buf);
 			}
 
 			if (colon3 && *colon3) {
@@ -437,11 +430,9 @@ static bool get_options(int this_option_offset, int *baud, int *miner_count,
 				if (tmp > 0 && tmp <= 0xff)
 					*timeout = tmp;
 				else {
-					sprintf(err_buf,
-						"Invalid avalon-options for "
+					quit(1, "Invalid avalon-options for "
 						"timeout (%s) must be 1 ~ %d",
 						colon3, 0xff);
-					quit(1, err_buf);
 				}
 				if (colon4 && *colon4) {
 					tmp = atoi(colon4);
@@ -453,10 +444,8 @@ static bool get_options(int this_option_offset, int *baud, int *miner_count,
 						*frequency = tmp;
 						break;
 					default:
-						sprintf(err_buf,
-							"Invalid avalon-options for "
+						quit(1, "Invalid avalon-options for "
 							"frequency must be 256/270/282/300");
-							quit(1, err_buf);
 					}
 				}
 			}
diff --git a/driver-icarus.c b/driver-icarus.c
index 8983e28..b763cc7 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -595,7 +595,6 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
 
 static uint32_t mask(int work_division)
 {
-	char err_buf[BUFSIZ+1];
 	uint32_t nonce_mask = 0x7fffffff;
 
 	// yes we can calculate these, but this way it's easy to see what they are
@@ -613,8 +612,7 @@ static uint32_t mask(int work_division)
 		nonce_mask = 0x1fffffff;
 		break;
 	default:
-		sprintf(err_buf, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
-		quit(1, err_buf);
+		quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
 	}
 
 	return nonce_mask;
@@ -622,7 +620,6 @@ static uint32_t mask(int work_division)
 
 static void get_options(int this_option_offset, struct cgpu_info *icarus, int *baud, int *work_division, int *fpga_count)
 {
-	char err_buf[BUFSIZ+1];
 	char buf[BUFSIZ+1];
 	char *ptr, *comma, *colon, *colon2;
 	size_t max;
@@ -691,8 +688,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b
 				*baud = 57600;
 				break;
 			default:
-				sprintf(err_buf, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
-				quit(1, err_buf);
+				quit(1, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
 			}
 		}
 
@@ -707,8 +703,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b
 					*work_division = tmp;
 					*fpga_count = tmp;	// default to the same
 				} else {
-					sprintf(err_buf, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
-					quit(1, err_buf);
+					quit(1, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
 				}
 			}
 
@@ -717,8 +712,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b
 				if (tmp > 0 && tmp <= *work_division)
 					*fpga_count = tmp;
 				else {
-					sprintf(err_buf, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
-					quit(1, err_buf);
+					quit(1, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
 				}
 			}
 		}
diff --git a/driver-opencl.c b/driver-opencl.c
index 87d4291..b406a75 100644
--- a/driver-opencl.c
+++ b/driver-opencl.c
@@ -53,7 +53,6 @@ extern bool have_opencl;
 extern void *miner_thread(void *userdata);
 extern int dev_from_id(int thr_id);
 extern void tailsprintf(char *f, const char *fmt, ...);
-extern void wlog(const char *f, ...);
 extern void decay_time(double *f, double fadd);
 
 /**********************************************/
diff --git a/logging.c b/logging.c
index 45d80bf..4361401 100644
--- a/logging.c
+++ b/logging.c
@@ -1,5 +1,6 @@
 /*
  * Copyright 2011-2012 Con Kolivas
+ * Copyright 2013 Andrew Smith
  *
  * 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
@@ -13,7 +14,6 @@
 
 #include "logging.h"
 #include "miner.h"
-// #include "util.h"
 
 bool opt_debug = false;
 bool opt_log_output = false;
@@ -21,66 +21,40 @@ bool opt_log_output = false;
 /* per default priorities higher than LOG_NOTICE are logged */
 int opt_log_level = LOG_NOTICE;
 
-static void my_log_curses(__maybe_unused int prio, char *f, va_list ap)
+static void my_log_curses(int prio, const char *datetime, const char *str)
 {
 	if (opt_quiet && prio != LOG_ERR)
 		return;
 
 #ifdef HAVE_CURSES
 	extern bool use_curses;
-	if (use_curses && log_curses_only(prio, f, ap))
+	if (use_curses && log_curses_only(prio, datetime, str))
 		;
 	else
 #endif
 	{
-		int len = strlen(f);
-
-		strcpy(f + len - 1, "                    \n");
-
 		mutex_lock(&console_lock);
-		vprintf(f, ap);
+		printf("%s%s%s\n", datetime, str, "                    \n");
 		mutex_unlock(&console_lock);
 	}
 }
 
-static void log_generic(int prio, const char *fmt, va_list ap);
-
-void vapplog(int prio, const char *fmt, va_list ap)
-{
-	if (!opt_debug && prio == LOG_DEBUG)
-		return;
-	if (use_syslog || opt_log_output || prio <= LOG_NOTICE)
-		log_generic(prio, fmt, ap);
-}
-
-void _applog(int prio, const char *fmt, ...)
-{
-	va_list ap;
-
-	va_start(ap, fmt);
-	vapplog(prio, fmt, ap);
-	va_end(ap);
-}
-
-
-/* high-level logging functions, based on global opt_log_level */
+/* high-level logging function, based on global opt_log_level */
 
 /*
- * generic log function used by priority specific ones
- * equals vapplog() without additional priority checks
+ * log function
  */
-static void log_generic(int prio, const char *fmt, va_list ap)
+void _applog(int prio, const char *str)
 {
 #ifdef HAVE_SYSLOG_H
 	if (use_syslog) {
-		vsyslog(prio, fmt, ap);
+		syslog(prio, "%s", str);
 	}
 #else
 	if (0) {}
 #endif
 	else {
-		char *f;
-		int len;
+		char datetime[64];
 		struct timeval tv = {0, 0};
 		struct tm *tm;
 
@@ -89,59 +63,20 @@ static void log_generic(int prio, const char *fmt, va_list ap)
 		const time_t tmp_time = tv.tv_sec;
 		tm = localtime(&tmp_time);
 
-		len = 40 + strlen(fmt) + 22;
-		f = alloca(len);
-		sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
+		sprintf(datetime, " [%d-%02d-%02d %02d:%02d:%02d] ",
 			tm->tm_year + 1900,
 			tm->tm_mon + 1,
 			tm->tm_mday,
 			tm->tm_hour,
 			tm->tm_min,
-			tm->tm_sec,
-			fmt);
+			tm->tm_sec);
+
 		/* Only output to stderr if it's not going to the screen as well */
 		if (!isatty(fileno((FILE *)stderr))) {
-			va_list apc;
-
-			va_copy(apc, ap);
-			vfprintf(stderr, f, apc);	/* atomic write to stderr */
-			va_end(apc);
+			fprintf(stderr, "%s%s\n", datetime, str);	/* atomic write to stderr */
 			fflush(stderr);
 		}
 
-		my_log_curses(prio, f, ap);
-	}
-}
-/* we can not generalize variable argument list */
-#define LOG_TEMPLATE(PRIO)		\
-	if (PRIO <= opt_log_level) {	\
-		va_list ap;		\
-		va_start(ap, fmt);	\
-		vapplog(PRIO, fmt, ap);	\
-		va_end(ap);		\
+		my_log_curses(prio, datetime, str);
 	}
-
-void log_error(const char *fmt, ...)
-{
-	LOG_TEMPLATE(LOG_ERR);
-}
-
-void log_warning(const char *fmt, ...)
-{
-	LOG_TEMPLATE(LOG_WARNING);
-}
-
-void log_notice(const char *fmt, ...)
-{
-	LOG_TEMPLATE(LOG_NOTICE);
-}
-
-void log_info(const char *fmt, ...)
-{
-	LOG_TEMPLATE(LOG_INFO);
-}
-
-void log_debug(const char *fmt, ...)
-{
-	LOG_TEMPLATE(LOG_DEBUG);
 }
diff --git a/logging.h b/logging.h
index be3923f..0a9b464 100644
--- a/logging.h
+++ b/logging.h
@@ -17,7 +17,7 @@ enum {
 };
 #endif
 
-/* original / legacy debug flags */
+/* debug flags */
 extern bool opt_debug;
 extern bool opt_log_output;
 extern bool opt_realquiet;
@@ -26,22 +26,43 @@ extern bool want_per_device_stats;
 /* global log_level, messages with lower or equal prio are logged */
 extern int opt_log_level;
 
-/* low-level logging functions with priority parameter */
-extern void vapplog(int prio, const char *fmt, va_list ap);
-extern void _applog(int prio, const char *fmt, ...);
+#define LOGBUFSIZ 256
+
+extern void _applog(int prio, const char *str);
+
 #define applog(prio, fmt, ...) do { \
- char *tmp42; \
- if (0) \
-	sprintf(tmp42, fmt, ##__VA_ARGS__); \
- else \
-	_applog(prio, fmt, ##__VA_ARGS__); \
+	if (opt_debug || prio != LOG_DEBUG) { \
+		if (use_syslog || opt_log_output || prio <= opt_log_level) { \
+			char tmp42[LOGBUFSIZ]; \
+			snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
+			_applog(prio, tmp42); \
+		} \
+	} \
+} while (0)
+
+#define quit(status, fmt, ...) do { \
+	if (fmt) { \
+		char tmp42[LOGBUFSIZ]; \
+		snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
+		_applog(LOG_ERR, tmp42); \
+	} \
+	_quit(status); \
+} while (0)
+
+#ifdef HAVE_CURSES
+
+#define wlog(fmt, ...) do { \
+	char tmp42[LOGBUFSIZ]; \
+	snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
+	_wlog(tmp42); \
 } while (0)
 
-/* high-level logging functions with implicit priority */
-extern void log_error(const char *fmt, ...);
-extern void log_warning(const char *fmt, ...);
-extern void log_notice(const char *fmt, ...);
-extern void log_info(const char *fmt, ...);
-extern void log_debug(const char *fmt, ...);
+#define wlogprint(fmt, ...) do { \
+	char tmp42[LOGBUFSIZ]; \
+	snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
+	_wlogprint(tmp42); \
+} while (0)
+
+#endif
 
 #endif /* __LOGGING_H__ */
diff --git a/miner.h b/miner.h
index 942e2a0..61861a5 100644
--- a/miner.h
+++ b/miner.h
@@ -713,7 +713,7 @@ endian_flip128(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
 }
 #endif
 
-extern void quit(int status, const char *format, ...);
+extern void _quit(int status);
 
 static inline void mutex_lock(pthread_mutex_t *lock)
 {
@@ -1281,7 +1281,8 @@ extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *mi
 extern void work_completed(struct cgpu_info *cgpu, struct work *work);
 extern void hash_queued_work(struct thr_info *mythr);
 extern void tailsprintf(char *f, const char *fmt, ...);
-extern void wlogprint(const char *f, ...);
+extern void _wlog(const char *str);
+extern void _wlogprint(const char *str);
 extern int curses_int(const char *query);
 extern char *curses_input(const char *query);
 extern void kill_work(void);
@@ -1292,7 +1293,7 @@ extern void write_config(FILE *fcfg);
 extern void zero_bestshare(void);
 extern void zero_stats(void);
 extern void default_save_file(char *filename);
-extern bool log_curses_only(int prio, const char *f, va_list ap);
+extern bool log_curses_only(int prio, const char *datetime, const char *str);
 extern void clear_logwin(void);
 extern void logwin_update(void);
 extern bool pool_tclear(struct pool *pool, bool *var);
diff --git a/usbutils.c b/usbutils.c
index 9fdb1ea..337518f 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -2247,7 +2247,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 	USBDEBUG("USB debug: _usb_read(%s (nodev=%s),ep=%d,buf=%p,bufsiz=%zu,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s,readonce=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, buf, bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi), bool_str(readonce));
 
 	if (bufsiz > USB_MAX_READ)
-		quit(1, "%s USB read request %d too large (max=%d)", cgpu->drv->name, bufsiz, USB_MAX_READ);
+		quit(1, "%s USB read request %zu too large (max=%d)", cgpu->drv->name, bufsiz, USB_MAX_READ);
 
 	if (timeout == DEVTIMEOUT)
 		timeout = usbdev->found->timeout;