Barry debian version 0.18.5-1
[barry.git] / tools / bktrans.cc
blobcb5f52329e6bc1f9eeb26e99547529d92a173536
1 //
2 // ktrans.cc - print human readable version of usbfs_snoop kernel log
3 //
5 /*
6 Copyright (C) 2005-2013, 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>
27 #include "i18n.h"
29 using namespace std;
31 void Usage()
33 cout << "ktrans - usbfs_snoop kernel log translator\n"
34 "\n"
35 "\tUsage: ktrans logmarker < log\n"
36 "\n"
37 "\tExample: ktrans \"kernel: \" < /var/log/kern.log\n"
38 "\n";
41 bool IsHexData(const char *logmarker, const char *str)
43 str = strstr(str, logmarker);
44 if( !str )
45 return false;
46 str += strlen(logmarker);
48 if( strstr(str, "data ") || strstr(str, "data: ") ) {
49 // looks like a data line, make sure data from there on out
50 // is hex data
52 // skip "data"
53 str = strstr(str, "data") + 4;
54 for( ; *str == ':' || *str == ' '; str++ )
57 while( *str ) {
58 if( !( isdigit(*str) || *str == ' ' ||
59 (*str >= 'a' && *str <= 'f')) )
60 return false;
61 str++;
64 return true;
66 else {
67 while( *str ) {
68 if( !( isdigit(*str) || *str == ' ' ||
69 (*str >= 'a' && *str <= 'f')) )
70 return false;
71 str++;
73 return true; // all data is numeric, so this is a continuation line
77 void SplitHex(const char *logmarker, const char *str, Barry::Data &data)
79 const char *hexptr = strstr(str, logmarker);
80 if( !hexptr ) {
81 cout << str << endl;
82 return;
84 hexptr += strlen(logmarker);
85 std::string readable(str, hexptr - str);
87 str = hexptr;
89 hexptr = strstr(str, "data ");
90 if( hexptr ) {
91 hexptr += 5;
93 else {
94 hexptr = strstr(str, "data: ");
95 if( hexptr )
96 hexptr += 6;
99 if( !hexptr )
100 hexptr = str;
102 for( ; *hexptr == ':' || *hexptr == ' '; hexptr++ )
105 readable.append(str, hexptr - str);
106 cout << readable << endl;
108 data.AppendHexString(hexptr);
111 int main(int argc, char *argv[])
113 INIT_I18N(PACKAGE);
115 cout.sync_with_stdio(false);
117 if( argc < 2 ) {
118 Usage();
119 return 0;
122 Barry::Data data;
123 while( cin ) {
124 std::string buff;
125 getline(cin, buff);
126 if( IsHexData(argv[1], buff.c_str()) ) {
127 SplitHex(argv[1], buff.c_str(), data);
129 else {
130 if( data.GetSize() ) {
131 cout << data;
132 data.Zap();
134 cout << buff << "\n";
138 if( data.GetSize() ) {
139 cout << data;