Skip to content

Commit

Permalink
gzip fix
Browse files Browse the repository at this point in the history
  • Loading branch information
psemiletov committed Jan 16, 2023
1 parent 0a6af0f commit e2eee57
Showing 1 changed file with 52 additions and 19 deletions.
71 changes: 52 additions & 19 deletions logrot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,24 @@ CLogRotator::CLogRotator (const string &fname, size_t maxfiles, size_t maxfilesi
max_log_file_size = maxfilesize;

has_gzip = is_program_exists ("gzip");
use_gzip = true;


//cout << "source_filename: " << source_filename << endl;
// cout << "max_log_files: " << max_log_files << endl;
//cout << "max_log_file_size: " << max_log_file_size << endl;


for (size_t i = 0; i < max_log_files; i++)
{
string f = source_filename + "." + std::to_string(i);
filenames.push_back (f);
// cout << "f: " << f << endl;
}

}

/*
void CLogRotator::rotate()
{
// cout << "CLogRotator::rotate()" << endl;
for (size_t i = filenames.size() - 1; i > 0; i--)
{
string oldname = filenames[i-1];
string newname = filenames[i];
// cout << "rename: " << oldname << " to: " << newname << endl;
rename (oldname.c_str(), newname.c_str());
}
string fname = source_filename + ".0";
rename (source_filename.c_str(), fname.c_str());

}
*/

/*
void CLogRotator::rotate()
{
Expand Down Expand Up @@ -87,3 +70,53 @@ void CLogRotator::rotate()
}
*/



void CLogRotator::rotate()
{
// cout << "CLogRotator::rotate()" << endl;

if (! use_gzip)
{
for (size_t i = filenames.size() - 1; i > 0; i--)
{
string oldname = filenames[i-1];
string newname = filenames[i];
// cout << "rename: " << oldname << " to: " << newname << endl;
rename (oldname.c_str(), newname.c_str());
}

string fname = source_filename + ".0";
rename (source_filename.c_str(), fname.c_str());
return;
}

if (has_gzip && use_gzip)
{

for (size_t i = filenames.size() - 1; i > 0; i--)
{
string oldname = filenames[i-1] + ".gz";
string newname = filenames[i] + ".gz";
// cout << "rename: " << oldname << " to: " << newname << endl;
rename (oldname.c_str(), newname.c_str());
}


string fname = source_filename + ".0";
rename (source_filename.c_str(), fname.c_str());

// cout << "ZIP" << endl;
string cm = "gzip " + fname;
if (system (cm.c_str()) == 0)
{
// cout << "ok " << fname << endl;
; //ok
}
}

}

0 comments on commit e2eee57

Please sign in to comment.