@ -3,12 +3,14 @@
# include <string>
# include <stdlib.h>
# include <stdint.h>
# include <string.h>
using namespace std ;
# define CPY1(x) *((uint8_t *)x)
# define CPY2(x) *((uint16_t *)x)
# define CPY8(x) *((uint64_t *)x)
enum log_event_type {
PROXYSQL_QUERY
} ;
typedef union _4bytes_t {
unsigned char data [ 4 ] ;
@ -22,18 +24,33 @@ unsigned int CPY3(unsigned char *ptr) {
return buf . i ;
}
uint8_t mysql_decode_length ( unsigned char * ptr , uint64_t * len ) {
if ( * ptr < = 0xfb ) { if ( len ) { * len = CPY1 ( ptr ) ; } ; return 1 ; }
if ( * ptr = = 0xfc ) { if ( len ) { * len = CPY2 ( ptr + 1 ) ; } ; return 3 ; }
//if (*ptr == 0xfc) { if (len) { *len = CPY2(ptr+1); }; return 3; }
if ( * ptr = = 0xfc ) {
if ( len ) {
memcpy ( ( void * ) len , ( void * ) ( ptr + 1 ) , 2 ) ;
} ;
return 3 ;
}
if ( * ptr = = 0xfd ) { if ( len ) { * len = CPY3 ( ptr + 1 ) ; } ; return 4 ; }
if ( * ptr = = 0xfe ) { if ( len ) { * len = CPY8 ( ptr + 1 ) ; } ; return 9 ; }
//if (*ptr == 0xfe) { if (len) { *len = CPY8(ptr+1); }; return 9; }
if ( * ptr = = 0xfe ) {
if ( len ) {
memcpy ( ( void * ) len , ( void * ) ( ptr + 1 ) , 8 ) ;
} ;
return 9 ;
}
return 0 ; // never reaches here
}
void read_encoded_length ( uint64_t * ptr , std : : fstream * f ) {
unsigned char buf [ 10 ] ;
unsigned char buf [ 9 ] ;
memset ( buf , 0 , sizeof ( uint64_t ) ) ;
uint8_t len ;
* ptr = 0 ;
f - > read ( ( char * ) buf , 1 ) ;
len = mysql_decode_length ( buf , NULL ) ;
if ( len ) {
@ -68,24 +85,49 @@ class MySQL_Event {
size_t server_len ;
size_t client_len ;
uint64_t total_length ;
log_event_type et ;
public :
MySQL_Event ( ) {
query_len = 0 ;
}
void read ( std : : fstream * f ) {
f - > read ( ( char * ) & et , 1 ) ;
switch ( et ) {
case PROXYSQL_QUERY :
read_query ( f ) ;
break ;
default :
break ;
}
}
void read_query ( std : : fstream * f ) {
read_encoded_length ( ( uint64_t * ) & thread_id , f ) ;
read_encoded_length ( ( uint64_t * ) & username_len , f ) ;
username = read_string ( f , username_len ) ;
read_encoded_length ( ( uint64_t * ) & schemaname_len , f ) ;
schemaname = read_string ( f , schemaname_len ) ;
cout < < " username= " < < username < < " schemaname= " < < schemaname ;
cout < < " thread_id=\" " < < thread_id < < " \" username=\" " < < username < < " \" schemaname= " < < schemaname < < " \" " ;
read_encoded_length ( ( uint64_t * ) & start_time , f ) ;
read_encoded_length ( ( uint64_t * ) & end_time , f ) ;
read_encoded_length ( ( uint64_t * ) & query_digest , f ) ;
read_encoded_length ( ( uint64_t * ) & query_len , f ) ;
query_ptr = read_string ( f , query_len ) ;
cout < < " starttime= " < < start_time < < " endtime= " < < end_time ;
cout < < " query= " < < query_ptr < < endl ;
char buffer [ 26 ] ;
char buffer2 [ 10 ] ;
struct tm * tm_info ;
time_t timer ;
timer = start_time / 1000 / 1000 ;
tm_info = localtime ( & timer ) ;
strftime ( buffer , 26 , " %Y-%m-%d %H:%M:%S " , tm_info ) ;
sprintf ( buffer2 , " %6u " , ( unsigned ) ( start_time % 1000000 ) ) ;
cout < < " starttime= \" " < < buffer < < " . " < < buffer2 < < " \" " ;
timer = end_time / 1000 / 1000 ;
tm_info = localtime ( & timer ) ;
strftime ( buffer , 26 , " %Y-%m-%d %H:%M:%S " , tm_info ) ;
sprintf ( buffer2 , " %6u " , ( unsigned ) ( end_time % 1000000 ) ) ;
cout < < " endtime= \" " < < buffer < < " . " < < buffer2 < < " \" " ;
cout < < " query= \" " < < query_ptr < < " \" " < < endl ;
}
~ MySQL_Event ( ) {
free ( username ) ;
@ -105,7 +147,7 @@ int main(int argc, char **argv) {
while ( more_data ) {
try {
input . read ( ( char * ) & msg_len , sizeof ( uint64_t ) ) ;
cout < < msg_len < < endl ;
//cout << msg_len << endl;
MySQL_Event me ;
me . read ( & input ) ;