- refactored record parser classes into separate files for each
[barry.git] / tools / ktrans.cc
blob40ef0ff992f315a4a680ce48c9b7f4330494f34a
1 //
2 // ktrans.cc - print human readable version of usbfs_snoop kernel log
3 //
5 /*
6 Copyright (C) 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU General Public License in the COPYING file at the
18 root directory of this project for more details.
21 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 #include <string>
25 #include <string.h>
26 #include <barry/data.h>
28 using namespace std;
30 void Usage()
32 cout << "ktrans - usbfs_snoop kernel log translator\n"
33 "\n"
34 "\tUsage: ktrans logmarker < log\n"
35 "\n"
36 "\tExample: ktrans \"kernel: \" < /var/log/kern.log\n"
37 "\n";
40 bool IsHexData(const char *logmarker, const char *str)
42 str = strstr(str, logmarker);
43 if( !str )
44 return false;
45 str += strlen(logmarker);
47 if( strstr(str, "data ") || strstr(str, "data: ") ) {
48 // looks like a data line, make sure data from there on out
49 // is hex data
51 // skip "data"
52 str = strstr(str, "data") + 4;
53 for( ; *str == ':' || *str == ' '; str++ )
56 while( *str ) {
57 if( !( isdigit(*str) || *str == ' ' ||
58 (*str >= 'a' && *str <= 'f')) )
59 return false;
60 str++;
63 return true;
65 else {
66 while( *str ) {
67 if( !( isdigit(*str) || *str == ' ' ||
68 (*str >= 'a' && *str <= 'f')) )
69 return false;
70 str++;
72 return true; // all data is numeric, so this is a continuation line
76 void SplitHex(const char *logmarker, const char *str, Barry::Data &data)
78 const char *hexptr = strstr(str, logmarker);
79 if( !hexptr ) {
80 cout << str << endl;
81 return;
83 hexptr += strlen(logmarker);
84 std::string readable(str, hexptr - str);
86 str = hexptr;
88 hexptr = strstr(str, "data ");
89 if( hexptr ) {
90 hexptr += 5;
92 else {
93 hexptr = strstr(str, "data: ");
94 if( hexptr )
95 hexptr += 6;
98 if( !hexptr )
99 hexptr = str;
101 for( ; *hexptr == ':' || *hexptr == ' '; hexptr++ )
104 readable.append(str, hexptr - str);
105 cout << readable << endl;
107 data.AppendHexString(hexptr);
110 int main(int argc, char *argv[])
112 cout.sync_with_stdio(false);
114 if( argc < 2 ) {
115 Usage();
116 return 0;
119 Barry::Data data;
120 while( cin ) {
121 std::string buff;
122 getline(cin, buff);
123 if( IsHexData(argv[1], buff.c_str()) ) {
124 SplitHex(argv[1], buff.c_str(), data);
126 else {
127 if( data.GetSize() ) {
128 cout << data;
129 data.Zap();
131 cout << buff << "\n";
135 if( data.GetSize() ) {
136 cout << data;