Switch from GPLv2 to GPLv2+
[fw/altos] / ao-tools / ao-sky-flash / sky_debug.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdint.h>
20 #include <unistd.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <sys/time.h>
24 #include "sky_flash.h"
25
26 static int      dbg_input;
27 static int      dbg_newline = 1;
28
29 int
30 skytraq_millis(void)
31 {
32         struct timeval  tv;
33         gettimeofday(&tv, NULL);
34         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
35 }
36
37 static void
38 skytraq_dbg_time(void)
39 {
40         int     delta = skytraq_millis() - skytraq_open_time;
41
42         if (!skytraq_verbose)
43                 return;
44         printf ("%4d.%03d ", delta / 1000, delta % 1000);
45 }
46
47 void
48 skytraq_dbg_newline(void)
49 {
50         if (!skytraq_verbose)
51                 return;
52         if (!dbg_newline) {
53                 putchar('\n');
54                 dbg_newline = 1;
55         }
56 }
57
58 static void
59 skytraq_dbg_set(int input)
60 {
61         if (!skytraq_verbose)
62                 return;
63         if (input != dbg_input) {
64                 skytraq_dbg_newline();
65                 if (input)
66                         putchar('\t');
67                 dbg_input = input;
68         }
69 }
70
71 void
72 skytraq_dbg_char(int input, char c)
73 {
74         if (!skytraq_verbose)
75                 return;
76         skytraq_dbg_set(input);
77         if (dbg_newline)
78                 skytraq_dbg_time();
79         if (c < ' '  || c > '~')
80                 printf ("\\%02x", (unsigned char) c);
81         else
82                 putchar(c);
83         dbg_newline = 0;
84         if (c == '\n')
85                 dbg_input = 2;
86         fflush(stdout);
87 }
88
89 void
90 skytraq_dbg_buf(int input, const char *buf, int len)
91 {
92         if (!skytraq_verbose)
93                 return;
94         while (len--)
95                 skytraq_dbg_char(input, *buf++);
96 }
97
98 void
99 skytraq_dbg_printf(int input, const char *fmt, ...)
100 {
101         va_list ap;
102
103         if (!skytraq_verbose)
104                 return;
105         skytraq_dbg_set(input);
106         if (dbg_newline)
107                 skytraq_dbg_time();
108         va_start (ap, fmt);
109         vprintf(fmt, ap);
110         va_end(ap);
111         dbg_newline = 0;
112 }