diff options
| author | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:44 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:44 +0200 |
| commit | 5b7605790328c6c0f473296df444d0f4a79ac779 (patch) | |
| tree | 48b545ac0295c69fb02247d4ad780be311642462 /src/logd.cpp | |
| parent | d3982ec72b255876db00825605d1d5aae0bc313e (diff) | |
tagging ychat-0.7.6ychat-0.7.6
Diffstat (limited to 'src/logd.cpp')
| -rwxr-xr-x | src/logd.cpp | 169 |
1 files changed, 85 insertions, 84 deletions
diff --git a/src/logd.cpp b/src/logd.cpp index b572a42..01a8824 100755 --- a/src/logd.cpp +++ b/src/logd.cpp @@ -10,162 +10,163 @@ logd::logd( string s_filename, string s_log_lines ) { - initialize( s_filename, tool::string2int(s_log_lines) ); + initialize( s_filename, tool::string2int(s_log_lines) ); } logd::logd( string s_filename, int i_log_lines ) { - initialize( s_filename, i_log_lines ); + initialize( s_filename, i_log_lines ); } logd::~logd() { - flush_logs(); - pthread_mutex_destroy( &mut_s_logging ); + flush(); + pthread_mutex_destroy( &mut_s_logging ); } void logd::initialize( string s_filename, int i_log_lines ) { - pthread_mutex_init( &mut_s_logging, NULL ); + pthread_mutex_init( &mut_s_logging, NULL ); - if( s_filename.empty() ) - { - wrap::system_message( LOGERR2 ); - exit(1); - } + if( s_filename.empty() ) + { + wrap::system_message( LOGERR2 ); + exit(1); + } - s_logfile = s_filename; - i_lines = i_log_lines; + s_logfile = s_filename; + i_lines = i_log_lines; } string logd::get_time_string() { - struct tm *t_m; - time_t t_cur = time(NULL); - t_m = gmtime(&t_cur); + struct tm *t_m; + time_t t_cur = time(NULL); + t_m = gmtime(&t_cur); - char c_buf[100]; - c_buf[99] = '\0'; - strftime(c_buf, 100, "[%d/%b/%Y:%H:%M:%S %z]", t_m); + char c_buf[100]; + c_buf[99] = '\0'; + strftime(c_buf, 100, "[%d/%b/%Y:%H:%M:%S %z]", t_m); - return string(c_buf); + return string(c_buf); } -void +void logd::flush() { - ofstream of_output; + ofstream of_output; - of_output.open(s_logfile.c_str(), ios::app); + of_output.open(s_logfile.c_str(), ios::app); - if( of_output == NULL ) - { - wrap::system_message( LOGERR1 + s_logfile ); - exit(1); - } + if( of_output == NULL ) + { + wrap::system_message( LOGERR1 + s_logfile ); + exit(1); + } - while( ! s_queue.empty() ) - { - string s_l=s_queue.front(); - s_queue.pop(); - of_output.write( s_l.c_str(), s_l.size() ); - } + while( ! s_queue.empty() ) + { + string s_l=s_queue.front(); + s_queue.pop(); + of_output.write( s_l.c_str(), s_l.size() ); + } - of_output.close(); + of_output.close(); } void -logd::log_access( hashmap<string> &map_request ) +logd::log_access( hashmap<string> map_request ) { - string s_time = get_time_string(); - string s_logstr = map_request["REMOTE_ADDR"] + " - - "+s_time+" \"" + map_request["QUERY_STRING"]+"\" 200 0 \""+map_request["request"]+"\" \""+map_request["User-Agent"]+"\"\n"; + string s_time = get_time_string(); + string s_logstr = map_request["REMOTE_ADDR"] + " - - "+s_time+" \"" + map_request["QUERY_STRING"]+"\" 200 0 \""+map_request["request"]+"\" \""+map_request["User-Agent"]+"\"\n"; - pthread_mutex_lock ( &mut_s_logging ); - s_queue.push(s_logstr); + pthread_mutex_lock ( &mut_s_logging ); + s_queue.push(s_logstr); - if ( s_queue.size() > i_lines ) - flush(); + if ( s_queue.size() > i_lines ) + flush(); - pthread_mutex_unlock( &mut_s_logging ); + pthread_mutex_unlock( &mut_s_logging ); } void logd::log_simple_line( string s_line ) { - // Dont log empty lines! - if (s_line.empty()) - return; + // Dont log empty lines! + if (s_line.empty()) + return; - string s_time = get_time_string(); - string s_logstr = s_time + " " + s_line; + string s_time = get_time_string(); + string s_logstr = s_time + " " + s_line; - pthread_mutex_lock ( &mut_s_logging ); - s_queue.push(s_logstr); + pthread_mutex_lock ( &mut_s_logging ); + s_queue.push(s_logstr); - if ( s_queue.size() > i_lines ) - flush(); + if ( s_queue.size() > i_lines ) + flush(); - pthread_mutex_unlock( &mut_s_logging ); + pthread_mutex_unlock( &mut_s_logging ); } void logd::set_logfile( string s_path, string s_filename ) { - // Remove "/" from filename! - unsigned i_pos = s_filename.find( "/" ); - while ( i_pos != string::npos ) - { - s_filename.replace( i_pos, 1, "SLASH" ); - i_pos = s_filename.find( "/" ); - } - - // Remove "\" from filename (for non unix systems)! - i_pos = s_filename.find( "\\" ); - while ( i_pos != string::npos ) - { - s_filename.replace( i_pos, 1, "BACKSLASH" ); + // Remove "/" from filename! + unsigned i_pos = s_filename.find( "/" ); + while ( i_pos != string::npos ) + { + s_filename.replace( i_pos, 1, "SLASH" ); + i_pos = s_filename.find( "/" ); + } + + // Remove "\" from filename (for non unix systems)! i_pos = s_filename.find( "\\" ); - } - - pthread_mutex_lock ( &mut_s_logging ); - this->s_logfile = s_path + s_filename; - pthread_mutex_unlock( &mut_s_logging ); + while ( i_pos != string::npos ) + { + s_filename.replace( i_pos, 1, "BACKSLASH" ); + i_pos = s_filename.find( "\\" ); + } + + pthread_mutex_lock ( &mut_s_logging ); + this->s_logfile = s_path + s_filename; + pthread_mutex_unlock( &mut_s_logging ); } void logd::flush_logs() { - pthread_mutex_lock ( &mut_s_logging ); - flush(); - pthread_mutex_unlock( &mut_s_logging ); + pthread_mutex_lock ( &mut_s_logging ); + flush(); + pthread_mutex_unlock( &mut_s_logging ); } string logd::remove_html_tags( string s_logs ) { - unsigned pos[2]; + unsigned pos[2]; - while ( (pos[0] = s_logs.find("<")) != string::npos ) - { - if ( (pos[1] = s_logs.find(">", pos[0])) != string::npos ) - s_logs.replace( pos[0], pos[1]-pos[0]+1, ""); - else + while ( (pos[0] = s_logs.find("<")) != string::npos ) + { + if ( (pos[1] = s_logs.find(">", pos[0])) != string::npos ) + s_logs.replace( pos[0], pos[1]-pos[0]+1, ""); + else break; - } + } - if ( s_logs == "\n" ) - return ""; + if ( s_logs == "\n" ) + return ""; - return s_logs; + return s_logs; } void logd::set_lines( const int i_lines ) { - this->i_lines = i_lines; + this->i_lines = i_lines; } #endif + #endif |
