Skip to content

Commit

Permalink
+ initial gzip support
Browse files Browse the repository at this point in the history
  • Loading branch information
psemiletov committed Jan 15, 2023
1 parent 39095f3 commit 0a6af0f
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
+ gzip config variable (true, false, false by default)
* $msec_random -> $seconds_random (renamed)
* free space evaluation fixes
* C++17 minimum dependency
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ You can use following configuration variables:

**debug=boolean** - if true, show debug messages. Be verbose, dude! False by default.

**gzip=boolean** - if true, use external gzip to compress "rotated" log files


### logfilegen.conf

The configuration file is called ```logfilegen.conf``` and must be placed to ```/etc/logfilegen/``` or ```$HOME/.config/logfilegen/``` or to current the directory (where logfilegen binary has been runned).
Expand Down Expand Up @@ -125,6 +128,9 @@ All variables above can be also set via enviromnent variables using ```export KE

**LFG_LOGCOUNT** - set **logcount**

**LFG_GZIP** - set **gzip**


Example:

```export LFG_DURATION=20```
Expand Down
6 changes: 6 additions & 0 deletions cycle.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <thread>


#include "cycle.h"


Expand Down Expand Up @@ -49,6 +52,9 @@ CGenCycleUnrated::CGenCycleUnrated (CParameters *prms, const string &fname)
no_free_space = false;

logrotator = new CLogRotator (params->logfile, params->max_log_files, string_to_file_size (params->max_log_file_size));
logrotator->use_gzip = params->use_gzip;


tpl = new CTpl (fname_template, params->mode);

std::signal (SIGINT, f_signal_handler);
Expand Down
3 changes: 2 additions & 1 deletion cycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <vector>
#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>
#include <unistd.h>
#include <csignal>
Expand Down Expand Up @@ -35,6 +34,8 @@ class CParameters
int duration; //duration of log generation, in seconds
int rate; //during the log generation, how many lines per second will be written

bool use_gzip;

size_t max_log_files;
string max_log_file_size;

Expand Down
49 changes: 47 additions & 2 deletions logrot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ CLogRotator::CLogRotator (const string &fname, size_t maxfiles, size_t maxfilesi
max_log_files = maxfiles;
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;
Expand All @@ -23,7 +27,7 @@ CLogRotator::CLogRotator (const string &fname, size_t maxfiles, size_t maxfilesi

}


/*
void CLogRotator::rotate()
{
// cout << "CLogRotator::rotate()" << endl;
Expand All @@ -33,12 +37,53 @@ void CLogRotator::rotate()
string oldname = filenames[i-1];
string newname = filenames[i];
// cout << "rename: " << oldname << " to: " << newname << endl;
rename (oldname.c_str(), newname.c_str());
rename (oldname.c_str(), newname.c_str());
}
string fname = source_filename + ".0";
rename (source_filename.c_str(), fname.c_str());
}
*/


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());
}

if (has_gzip && use_gzip)
{
// cout << "ZIP" << endl;
string cm = "gzip " + source_filename;
if (system (cm.c_str()) == 0)
{
string fnamezip = source_filename + ".gz";
string fname = source_filename + ".0";

// cout << "fnamezip" << fnamezip << endl;
// cout << "fname" << fname << endl;

rename (fnamezip.c_str(), fname.c_str());
}
else
{//fallback if not zipped
string fname = source_filename + ".0";
rename (source_filename.c_str(), fname.c_str());
}
}
else
{
string fname = source_filename + ".0";
rename (source_filename.c_str(), fname.c_str());
}


}
3 changes: 3 additions & 0 deletions logrot.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class CLogRotator
{
public:

bool has_gzip;
bool use_gzip;

size_t max_log_files;
size_t max_log_file_size;

Expand Down
10 changes: 9 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main (int argc, char *argv[])

vector <string> envars = {"LFG_DURATION", "LFG_RATE", "LFG_LOGFILE",
"LFG_TEMPLATEFILE", "LFG_DEBUG", "LFG_PURE",
"LFG_LOGSIZE", "LFG_LOGCOUNT"};
"LFG_LOGSIZE", "LFG_LOGCOUNT", "LFG_GZIP"};

CParameters params;

Expand Down Expand Up @@ -76,6 +76,8 @@ int main (int argc, char *argv[])
params.mode = opts_config.get_string ("mode", "nginx");
params.pure = opts_config.get_bool ("pure", false);
params.debug = opts_config.get_bool ("debug", false);
params.use_gzip = opts_config.get_bool ("gzip", false);

params.max_log_files = opts_config.get_int ("logcount", 5);
params.max_log_file_size = opts_config.get_string ("logsize", "16m");

Expand All @@ -97,6 +99,9 @@ int main (int argc, char *argv[])
params.templatefile = opts_cmdline.get_string ("templatefile", params.templatefile);
params.mode = opts_cmdline.get_string ("mode", params.mode);
params.pure = opts_cmdline.get_bool ("pure", params.pure);
params.use_gzip = opts_cmdline.get_bool ("gzip", params.use_gzip);

params.debug = opts_cmdline.get_bool ("debug", params.debug);

params.max_log_files = opts_cmdline.get_int ("logcount", params.max_log_files);
params.max_log_file_size = opts_cmdline.get_string ("logsize", params.max_log_file_size);
Expand Down Expand Up @@ -127,6 +132,9 @@ int main (int argc, char *argv[])
params.mode = opts_envars.get_string ("mode", params.mode);
params.pure = opts_envars.get_bool ("pure", params.pure);

params.debug = opts_envars.get_bool ("debug", params.debug);
params.use_gzip = opts_envars.get_bool ("gzip", params.use_gzip);


params.max_log_files = opts_envars.get_int ("logcount", params.max_log_files);
params.max_log_file_size = opts_envars.get_string ("logsize", params.max_log_file_size);
Expand Down
8 changes: 8 additions & 0 deletions utl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,13 @@ size_t string_to_file_size (const string &val)
}


bool is_program_exists (const string &appname)
{
string cm = "which " + appname + " > /dev/null 2>&1";
if (system (cm.c_str()))
return false;
else
return true;
}

#endif
2 changes: 2 additions & 0 deletions utl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ string get_home_dir();
string current_path();
bool file_exists (const string &name);
size_t get_free_space (const string &path);
bool is_program_exists (const string &appname);


//String utis

Expand Down

0 comments on commit 0a6af0f

Please sign in to comment.