bktrans.cc

00001 //
00002 // ktrans.cc - print human readable version of usbfs_snoop kernel log
00003 //
00004 
00005 /*
00006     Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017     See the GNU General Public License in the COPYING file at the
00018     root directory of this project for more details.
00019 */
00020 
00021 #include <iostream>
00022 #include <iomanip>
00023 #include <sstream>
00024 #include <string>
00025 #include <string.h>
00026 #include <barry/data.h>
00027 
00028 using namespace std;
00029 
00030 void Usage()
00031 {
00032         cout << "ktrans - usbfs_snoop kernel log translator\n"
00033                 "\n"
00034                 "\tUsage:   ktrans logmarker < log\n"
00035                 "\n"
00036                 "\tExample:  ktrans \"kernel: \" < /var/log/kern.log\n"
00037                 "\n";
00038 }
00039 
00040 bool IsHexData(const char *logmarker, const char *str)
00041 {
00042         str = strstr(str, logmarker);
00043         if( !str )
00044                 return false;
00045         str += strlen(logmarker);
00046 
00047         if( strstr(str, "data ") || strstr(str, "data: ") ) {
00048                 // looks like a data line, make sure data from there on out
00049                 // is hex data
00050 
00051                 // skip "data"
00052                 str = strstr(str, "data") + 4;
00053                 for( ; *str == ':' || *str == ' '; str++ )
00054                         ;
00055 
00056                 while( *str ) {
00057                         if( !( isdigit(*str) || *str == ' ' ||
00058                             (*str >= 'a' && *str <= 'f')) )
00059                                 return false;
00060                         str++;
00061                 }
00062 
00063                 return true;
00064         }
00065         else {
00066                 while( *str ) {
00067                         if( !( isdigit(*str) || *str == ' ' ||
00068                             (*str >= 'a' && *str <= 'f')) )
00069                                 return false;
00070                         str++;
00071                 }
00072                 return true;    // all data is numeric, so this is a continuation line
00073         }
00074 }
00075 
00076 void SplitHex(const char *logmarker, const char *str, Barry::Data &data)
00077 {
00078         const char *hexptr = strstr(str, logmarker);
00079         if( !hexptr ) {
00080                 cout << str << endl;
00081                 return;
00082         }
00083         hexptr += strlen(logmarker);
00084         std::string readable(str, hexptr - str);
00085 
00086         str = hexptr;
00087 
00088         hexptr = strstr(str, "data ");
00089         if( hexptr ) {
00090                 hexptr += 5;
00091         }
00092         else {
00093                 hexptr = strstr(str, "data: ");
00094                 if( hexptr )
00095                         hexptr += 6;
00096         }
00097 
00098         if( !hexptr )
00099                 hexptr = str;
00100 
00101         for( ; *hexptr == ':' || *hexptr == ' '; hexptr++ )
00102                 ;
00103 
00104         readable.append(str, hexptr - str);
00105         cout << readable << endl;
00106 
00107         data.AppendHexString(hexptr);
00108 }
00109 
00110 int main(int argc, char *argv[])
00111 {
00112         cout.sync_with_stdio(false);
00113 
00114         if( argc < 2 ) {
00115                 Usage();
00116                 return 0;
00117         }
00118 
00119         Barry::Data data;
00120         while( cin ) {
00121                 std::string buff;
00122                 getline(cin, buff);
00123                 if( IsHexData(argv[1], buff.c_str()) ) {
00124                         SplitHex(argv[1], buff.c_str(), data);
00125                 }
00126                 else {
00127                         if( data.GetSize() ) {
00128                                 cout << data;
00129                                 data.Zap();
00130                         }
00131                         cout << buff << "\n";
00132                 }
00133         }
00134 
00135         if( data.GetSize() ) {
00136                 cout << data;
00137         }
00138 }
00139 

Generated on Tue Jun 30 16:08:13 2009 for Barry by  doxygen 1.5.8