-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
155 lines (146 loc) · 4 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <exception>
#include <iostream>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/trivial.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/utility/empty_deleter.hpp>
#include "FreeMemoryUbuntu.hpp"
#include "FreeException.hpp"
#define DEFAULT_SLEEP (30)
#define DEFAULT_MEM_FREE (25)
static SixFree::AFreeMemory *six;
void init_boost_logs()
{
typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> text_sink;
auto sink = boost::make_shared<text_sink>();
boost::shared_ptr<std::ostream> stream(&std::clog, boost::empty_deleter());
sink->locked_backend()->add_stream (stream);
sink->set_formatter(
boost::log::expressions::stream
<< "[" << boost::log::expressions::attr<boost::log::trivial::severity_level>("Severity")
<< "] " << boost::log::expressions::smessage
);
boost::log::core::get()->add_sink(sink);
}
void set_silent_logs()
{
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= boost::log::trivial::fatal
);
}
void delSix()
{
delete (six);
}
int main(int ac, char **av)
{
bool bg = false;
bool silent = false;
bool loop = false;
bool swap = true;
size_t time = DEFAULT_SLEEP;
size_t mem_perc = DEFAULT_MEM_FREE;
try
{
init_boost_logs();
if (atexit(delSix))
{
BOOST_LOG_TRIVIAL(error) << "Cannot set exit function";
return (EXIT_FAILURE);
}
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help,h", "print this menu")
("bg,b", "run in background")
("watchdog,w", "run as watchdog")
("silent,s", "do not print output")
("noswap,S", "do not swapoff/swapon")
("time,t", boost::program_options::value<size_t>(), "wait N minutes between each run")
("memory,m", boost::program_options::value<size_t>(&mem_perc), "free if % memory available is below N")
;
boost::program_options::variables_map vm;
try
{
boost::program_options::store(boost::program_options::parse_command_line(ac, av, desc), vm);
if (vm.count("help"))
{
std::cout << desc << std::endl;
return (EXIT_SUCCESS);
}
boost::program_options::notify(vm);
if (vm.count("silent"))
{
silent = true;
set_silent_logs();
}
if (vm.count("watchdog"))
{
loop = true;
bg = true;
BOOST_LOG_TRIVIAL(trace) << "Watchdog Mode every " << time << " minutes";
}
else if (vm.count("bg"))
{
bg = true;
BOOST_LOG_TRIVIAL(trace) << "Running in Background";
}
if (vm.count("time"))
{
time = vm["time"].as<size_t>();
if (!vm.count("watchdog"))
BOOST_LOG_TRIVIAL(warning) << "Option --time only used with --watchdog";
}
if (vm.count("noswap"))
{
swap = false;
BOOST_LOG_TRIVIAL(trace) << "NoSwap Management";
}
BOOST_LOG_TRIVIAL(trace) << "Free Memory below " << mem_perc << "%";
}
catch (const boost::program_options::error& err)
{
BOOST_LOG_TRIVIAL(error) << err.what();
std::cout << desc << std::endl;
return (EXIT_FAILURE);
}
}
catch (const std::exception& err)
{
BOOST_LOG_TRIVIAL(error) << err.what();
return (EXIT_FAILURE);
}
try
{
if (bg && daemon(1, !silent))
{
throw(SixFree::FreeException("Cannot launch itself as daemon"));
}
six = new SixFree::FreeMemoryUbuntu(swap);
loop:
if (six->run(mem_perc))
return (EXIT_FAILURE);
if (loop)
{
sleep(time * 60);
goto loop;
}
}
catch (const std::exception& err)
{
BOOST_LOG_TRIVIAL(error) << err.what();
return (EXIT_FAILURE);
}
catch (...)
{
BOOST_LOG_TRIVIAL(error) << "Unknown Exception";
BOOST_LOG_TRIVIAL(error) << "Aborting";
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}