changed: log is saved after some requests (config: log_request value) (performance)

git-svn-id: svn://ttmath.org/publicrep/winix/trunk@563 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2010-01-28 21:09:48 +00:00
parent 09d427b4ba
commit ed9feaf542
7 changed files with 75 additions and 23 deletions

View File

@@ -15,16 +15,28 @@ Log::Log()
{
log_level = 3;
current_level = 4; // nothing to log (call Init() first)
item = 0;
item_save = 1;
lines = 0;
}
void Log::Init(int log_l, const std::string & log_f, bool log_std)
void Log::Init(int log_l, const std::string & log_f, bool log_std, int log_request)
{
log_level = log_l;
log_file = log_f;
log_stdout = log_std;
item_save = log_request;
OpenFile();
}
void Log::OpenFile()
{
if( !log_file.empty() )
file.open( log_file.c_str(), std::ios_base::out | std::ios_base::app );
}
void Log::PutDate(Manipulators m)
@@ -45,6 +57,7 @@ void Log::PutDate(Manipulators m)
Log & Log::operator<<(const char * s)
{
if( !s )
@@ -56,6 +69,7 @@ return *this;
}
Log & Log::operator<<(const std::string & s)
{
buffer << s;
@@ -64,6 +78,7 @@ Log & Log::operator<<(const std::string & s)
}
Log & Log::operator<<(const std::string * s)
{
buffer << *s;
@@ -72,6 +87,7 @@ Log & Log::operator<<(const std::string * s)
}
Log & Log::operator<<(int s)
{
buffer << s;
@@ -80,6 +96,7 @@ Log & Log::operator<<(int s)
}
Log & Log::operator<<(long s)
{
buffer << s;
@@ -98,6 +115,7 @@ Log & Log::operator<<(char s)
}
Log & Log::operator<<(size_t s)
{
buffer << s;
@@ -106,6 +124,7 @@ Log & Log::operator<<(size_t s)
}
Log & Log::operator<<(double s)
{
buffer << s;
@@ -113,13 +132,34 @@ Log & Log::operator<<(double s)
return *this;
}
Log & Log::operator<<(Manipulators m)
{
switch(m)
{
case logend:
buffer << '\n';
lines += 1;
break;
case logsavenow:
SaveLog();
buffer.str( "" );
item = 0;
lines = 0;
break;
case logsave:
item += 1;
if( item >= item_save || lines > 3000 )
{
SaveLog();
buffer.str( "" );
item = 0;
lines = 0;
}
break;
case log1:
@@ -142,32 +182,32 @@ return *this;
void Log::SaveLog()
{
int attempt = 2;
if( current_level > log_level )
return;
const std::string & source = buffer.str();
if( source.empty() )
return;
if( log_stdout )
std::cout << buffer.str() << std::endl;
std::cout << source;
if( log_file.empty() )
return;
std::ofstream file;
do
{
file.open( log_file.c_str(), std::ios_base::out | std::ios_base::app );
// if( !file )
// sleep(1);
}
while( --attempt > 0 && !file );
if( !file )
return;
{
file.close();
file.clear();
file << buffer.str() << std::endl;
OpenFile();
if( !file )
return;
}
file << source << std::endl;
}