1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#ifndef LOGD_CPP
#define LOGD_CPP
#include "logd.h"
#ifdef LOGGING
#include <fstream>
logd::logd( string s_filename, string 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 );
}
logd::~logd()
{
flush();
pthread_mutex_destroy( &mut_s_logging );
}
void
logd::initialize( string s_filename, int i_log_lines )
{
pthread_mutex_init( &mut_s_logging, NULL );
if( s_filename.empty() )
{
wrap::system_message( LOGERR2 );
exit(1);
}
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);
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);
}
void
logd::flush()
{
ofstream of_output;
of_output.open(s_logfile.c_str(), ios::app);
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() );
}
of_output.close();
}
void
logd::log_access( map<string,string> request )
{
string s_time = get_time_string();
string s_logstr = request["REMOTE_ADDR"] + " - - "+s_time+" \"" + request["QUERY_STRING"]+"\" 200 0 \""+request["request"]+"\" \""+request["User-Agent"]+"\"\n";
pthread_mutex_lock ( &mut_s_logging );
s_queue.push(s_logstr);
if ( s_queue.size() > i_lines )
flush();
pthread_mutex_unlock( &mut_s_logging );
}
void
logd::log_simple_line( string s_line )
{
// Dont log empty lines!
if (s_line.empty())
return;
string s_time = get_time_string();
string s_logstr = s_time + " " + s_line;
pthread_mutex_lock ( &mut_s_logging );
s_queue.push(s_logstr);
if ( s_queue.size() > i_lines )
flush();
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" );
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 );
}
string
logd::remove_html_tags( string s_logs )
{
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
break;
}
if ( s_logs == "\n" )
return "";
return s_logs;
}
void
logd::set_lines( const int i_lines )
{
this->i_lines = i_lines;
}
#endif
#endif
|