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