Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
psemiletov committed Jan 14, 2023
1 parent f2cfd40 commit da0c273
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 63 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cmake_minimum_required(VERSION 3.5)

set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD 17)

project (logfilegen VERSION 0.2.0 LANGUAGES CXX)
project (logfilegen VERSION 0.3.0 LANGUAGES CXX)
add_definitions(-DVERSION_NUMBER="\\"${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}\\"")


Expand Down
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
* free space evaluation fixes
* C++17 minimum dependency
+ Windows source compatibility
* max_log_files renamed to logcount
* max_log_file_size renamed to logsize
+ max_log_files, max_log_file_size config variables (see Readme)
+ $msec_random, $request_time, $connection_time variables with default values 0.1..60
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CXXFLAGS = -Wall -g -O3


all: main.cpp
$(CXX) $(CXXFLAGS) -std=c++11 -o logfilegen utl.cpp tpl.cpp pairfile.cpp cycle.cpp logrot.cpp main.cpp
$(CXX) $(CXXFLAGS) -std=c++17 -o logfilegen utl.cpp tpl.cpp pairfile.cpp cycle.cpp logrot.cpp main.cpp

clean:
$(RM) logfilegen
Expand Down
2 changes: 1 addition & 1 deletion Makefile.OpenBSD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CXX = clang++
CXXFLAGS = -Wall -g -O3

all: main.cpp
$(CXX) $(CXXFLAGS) -std=c++11 -o logfilegen utl.cpp tpl.cpp pairfile.cpp logrot.cpp cycle.cpp main.cpp
$(CXX) $(CXXFLAGS) -std=c++17 -o logfilegen utl.cpp tpl.cpp pairfile.cpp logrot.cpp cycle.cpp main.cpp

clean:
$(RM) logfilegen
Expand Down
2 changes: 1 addition & 1 deletion Makefile.clang
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ prefix=/usr
endif

CXX = clang++
CXXFLAGS = -Wall -g -O3 -std=c++11
CXXFLAGS = -Wall -g -O3 -std=c++17

all: main.cpp
$(CXX) $(CXXFLAGS) -std=c++11 -o logfilegen utl.cpp tpl.cpp pairfile.cpp logrot.cpp cycle.cpp main.cpp
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ You can use following configuration variables:

**logfile=string** - file name for the resulting logfile. If no absolute file provided, the program will search in the current directory. You can also use "stdout" (without the quotes) as the file name to output lines to the console.

**max_log_files=integer** - maximum log files used in log rotation.
**logcount=integer** - maximum log files used in log rotation.

**max_log_file_size=integer** - maximum log file size (in bytes by default). If exeeds, the log rotation will happen. The value can be with the suffix "k" to set the kilobytes and "m" for the megabytes, i.e. ```64k``` or ```16m```. The default value is ```16m```.
**logsize=integer** - maximum log file size (in bytes by default). If exeeds, the log rotation will happen. The value can be with the suffix "k" to set the kilobytes and "m" for the megabytes, "g" for Gb, i.e. ```64k``` or ```16m```. The default value is ```16m```.

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

Expand Down
52 changes: 7 additions & 45 deletions cycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void CParameters::print()
cout << "logfile: " << logfile << endl;

cout << "max_log_files: " << max_log_files << endl;
cout << "max_log_file_size: " << max_log_file_size << endl;
cout << "logsize: " << max_log_file_size << endl;


cout << "pure: " << pure << endl;
Expand All @@ -46,6 +46,7 @@ CGenCycleUnrated::CGenCycleUnrated (CParameters *prms, const string &fname)
params = prms;
fname_template = fname;
log_current_size = 0;
no_free_space = false;

logrotator = new CLogRotator (params->logfile, params->max_log_files, string_to_file_size (params->max_log_file_size));
tpl = new CTpl (fname_template, params->mode);
Expand Down Expand Up @@ -74,11 +75,11 @@ CGenCycleUnrated::CGenCycleUnrated (CParameters *prms, const string &fname)
//exit from program

cout << "Output files will not fit to the available disk space with current parameters, exiting!" << endl;
return false;
no_free_space = true;
//return;
}
}


}


Expand All @@ -94,7 +95,7 @@ bool CGenCycleUnrated::open_logfile()

file_out_error = false;

if (! params->bstdout)
if (! params->bstdout && ! no_free_space)
{
if (file_exists (params->logfile))
{
Expand All @@ -114,51 +115,12 @@ bool CGenCycleUnrated::open_logfile()
// throw std::ios_base::failure(std::strerror(errno));
file_out_error = true;
cout << "cannot create " << params->logfile << "\n";
return false;
}
else
file_out.exceptions (file_out.exceptions() | std::ios::failbit | std::ifstream::badbit);
}

/*
if (! params->bstdout)
{
// how many space we occupy with logstrings?
size_t free_space = get_free_space (get_file_path (params->logfile));
string test_string = tpl->prepare_log_string();
//test_string_size = test_string.size() + (test_string.size() / 2);
test_string_size = test_string.size();
// cout << "test_string_size, bytes: " << test_string_size << endl;
std::uintmax_t lines_total = static_cast<std::uintmax_t> (params->duration) * params->rate;
if (params->debug)
cout << "lines_total: " << lines_total << endl;
std::uintmax_t size_needed = lines_total * test_string_size;
size_needed += log_current_size;
if (params->debug)
cout << "size_needed, bytes: " << size_needed << endl;
if (size_needed >= free_space)
{
//exit from program
cout << "Output file will be too large with current parameters, exiting!" << endl;
return false;
}
}
*/





return true;
}

Expand Down Expand Up @@ -209,7 +171,7 @@ void CGenCycleUnrated::loop()
if (params->bstdout)
cout << log_string << "\n";

if (! file_out_error)
if (! file_out_error && ! no_free_space)
{
file_out << log_string << "\n";
log_current_size += log_string.size();
Expand Down
1 change: 1 addition & 0 deletions cycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class CGenCycleUnrated

ofstream file_out;
bool file_out_error;
bool no_free_space;
size_t test_string_size;

string fname_template;
Expand Down
16 changes: 8 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


#ifndef VERSION_NUMBER
#define VERSION_NUMBER "0.2.0"
#define VERSION_NUMBER "0.3.0"
#endif


Expand All @@ -40,7 +40,7 @@ int main (int argc, char *argv[])

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

CParameters params;

Expand Down Expand Up @@ -69,8 +69,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.max_log_files = opts_config.get_int ("max_log_files", 5);
params.max_log_file_size = opts_config.get_string ("max_log_file_size", "16m");
params.max_log_files = opts_config.get_int ("logcount", 5);
params.max_log_file_size = opts_config.get_string ("logsize", "16m");


if (params.debug)
Expand All @@ -91,8 +91,8 @@ int main (int argc, char *argv[])
params.mode = opts_cmdline.get_string ("mode", params.mode);
params.pure = opts_cmdline.get_bool ("pure", params.pure);

params.max_log_files = opts_cmdline.get_int ("max_log_files", params.max_log_files);
params.max_log_file_size = opts_cmdline.get_string ("max_log_file_size", params.max_log_file_size);
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 All @@ -116,8 +116,8 @@ int main (int argc, char *argv[])
params.pure = opts_envars.get_bool ("pure", params.pure);


params.max_log_files = opts_envars.get_int ("max_log_files", params.max_log_files);
params.max_log_file_size = opts_envars.get_string ("max_log_file_size", params.max_log_file_size);
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);

if (params.debug)
params.print();
Expand Down
23 changes: 20 additions & 3 deletions utl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

using namespace std;

#include <sys/statvfs.h>
//#include <sys/statvfs.h>
#include <sys/stat.h>

#include <algorithm>
#include <filesystem>


#include <cstdint>
#include <cstdlib>
Expand Down Expand Up @@ -122,9 +124,11 @@ string current_path()
return result;
}


/*
size_t get_free_space (const string &path)
{
struct statvfs buf;
int r = statvfs (path.c_str(), &buf);
Expand All @@ -134,6 +138,20 @@ size_t get_free_space (const string &path)
return buf.f_bavail * buf.f_bsize;
}
*/


size_t get_free_space (const string &path)
{
if (path.empty())
return 0;

// cout << "get_free_space:" << path << endl;

std::filesystem::path p (path);
const std::filesystem::space_info i = std::filesystem::space (p);
return i.available;
}


bool file_exists (const string &name)
Expand Down Expand Up @@ -200,7 +218,6 @@ vector <string> split_string_to_vector (const string& s, const string& delimeter

size_t string_to_file_size (const string &val)
{
cout << "string_to_file_size: " << val << endl;

size_t result = 0;
const char *st = val.c_str();
Expand Down

0 comments on commit da0c273

Please sign in to comment.