-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogrot.cpp
65 lines (53 loc) · 1.61 KB
/
logrot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include "logrot.h"
#include "utl.h"
using namespace std;
CLogRotator::CLogRotator (const std::string &fname, size_t maxfiles, size_t maxfilesize)
{
//if (! file_exists(source_filename))
// return;
source_filename = fname;
max_log_files = maxfiles;
max_log_file_size = maxfilesize;
has_gzip = is_program_exists ("gzip");
for (size_t i = 0; i < max_log_files; i++)
{
std::string f = source_filename + "." + std::to_string(i);
filenames.push_back (f);
}
}
void CLogRotator::rotate()
{
if (! use_gzip)
{
for (size_t i = filenames.size() - 1; i > 0; i--)
{
std::string oldname = filenames[i-1];
std::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
}
}
}