Debian Codegraph @

src/log_hex.cc

raw
1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id: data_log.c 1720 2006-01-20 22:34:58Z roms $ */
3
4 /* libticables2 - link cable library, a part of the TiLP project
5 * Copyright (C) 1999-2005 Romain Lievin
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /*
23 Hexadecimal logging.
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <glib.h>
32
33 #include "logging.h"
34 #include "data_log.h"
35 #include "log_hex.h"
36
37 static char *ofn = NULL;
38 static FILE *logfile = NULL;
39
40 int log_hex_start(void)
41 {
42 int ret;
43
44 ofn = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, LOG_DIR, G_DIR_SEPARATOR_S, HEX_FILE, NULL);
45
46 logfile = fopen(ofn, "wt");
47 if (logfile != NULL)
48 {
49 fprintf(logfile, "TiCables-2 data logger\n"); // needed by log_dbus.c
50 fprintf(logfile, "Version %s\n", ticables_version_get());
51 fprintf(logfile, "\n");
52 ret = 0;
53 }
54 else
55 {
56 ticables_critical("Unable to open %s for logging.\n", ofn);
57 ret = 1;
58 }
59
60 return ret;
61 }
62
63 int log_hex_1(int dir, uint8_t data)
64 {
65 static int array[20];
66 static int i = 0;
67
68 if (logfile == NULL)
69 {
70 return 1;
71 }
72
73 array[i++] = data;
74 fprintf(logfile, "%02X ", data);
75
76 if ((i != 0) && !(i % 16))
77 {
78 int j;
79 int c;
80 fprintf(logfile, "| ");
81 for (j = 0; j < 16; j++)
82 {
83 c = array[j];
84 if ((c < 32) || (c > 127))
85 {
86 fprintf(logfile, " ");
87 }
88 else
89 {
90 fprintf(logfile, "%c", c);
91 }
92 }
93 fprintf(logfile, "\n");
94 i = 0;
95 }
96
97 return 0;
98 }
99
100 int log_hex_N(int dir, const uint8_t * data, uint32_t len)
101 {
102 uint32_t i = 0;
103
104 for (i = 0; i < len; i++)
105 {
106 log_hex_1(dir, data[i]);
107 }
108
109 return 0;
110 }
111
112 int log_hex_stop(void)
113 {
114 if (logfile != NULL)
115 {
116 fclose(logfile);
117 logfile = NULL;
118 }
119
120 g_free(ofn);
121 ofn = NULL;
122
123 return 0;
124 }