From e2eee577e0451f843aeffe8159aaa184a4d45c8f Mon Sep 17 00:00:00 2001 From: Petr Semiletov Date: Mon, 16 Jan 2023 09:30:30 +0300 Subject: [PATCH] gzip fix --- logrot.cpp | 71 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/logrot.cpp b/logrot.cpp index f00f76f..2a95988 100644 --- a/logrot.cpp +++ b/logrot.cpp @@ -11,13 +11,13 @@ 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); @@ -25,27 +25,10 @@ CLogRotator::CLogRotator (const string &fname, size_t maxfiles, size_t maxfilesi // 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() { @@ -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 + } + } + +} +