Reduce file writes, add logging capability

This commit is contained in:
2022-10-03 14:02:44 +02:00
parent bf68e53a20
commit 95b6f248c8
6 changed files with 80 additions and 23 deletions

View File

@@ -5,7 +5,7 @@ project(fantasize VERSION 0.1.0)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(nlohmann_json 3.11.2 REQUIRED)
find_package(Boost 1.80 COMPONENTS program_options REQUIRED)
find_package(Boost 1.80 COMPONENTS program_options log log_setup date_time REQUIRED)
add_executable(${PROJECT_NAME}
src/main.cxx

View File

@@ -16,8 +16,8 @@ public:
PWMControl(std::string controlPath);
~PWMControl();
void pwm(int percent);
int pwm();
void Power(int percent);
int Power();
void EnableManualControl();
void Reset();
@@ -27,7 +27,7 @@ public:
json toJson() const override;
private:
int readValue(std::string path);
int mCurrentValue;
std::string mControlPath;
std::string mEnablePath;

View File

@@ -22,7 +22,7 @@ FanGenerator::FindFans(vector<shared_ptr<Sensor>> rpmSensors,
cout << "Setting all fans to maximum speed" << endl;
for (auto c : pwmControls) {
c->EnableManualControl();
c->pwm(100);
c->Power(100);
}
// Wait for fans to settle
@@ -41,7 +41,7 @@ FanGenerator::FindFans(vector<shared_ptr<Sensor>> rpmSensors,
for (auto c : pwmControls) {
cout << "Setting " << c->toString()
<< " to 50% and wait for it to settle..." << endl;
c->pwm(50);
c->Power(50);
this_thread::sleep_for(chrono::seconds(SETTLE_TIMEOUT));
@@ -53,7 +53,7 @@ FanGenerator::FindFans(vector<shared_ptr<Sensor>> rpmSensors,
}
cout << "Setting fan back to 100%" << endl;
c->pwm(100);
c->Power(100);
}
return mapping;

View File

@@ -19,9 +19,9 @@ HwmonFan::HwmonFan(std::shared_ptr<PWMControl> pwmControl,
void HwmonFan::PWM(int percent) {
if (percent < mMinPWM) {
mPWMControl->pwm(mMinPWM);
mPWMControl->Power(mMinPWM);
} else {
mPWMControl->pwm(percent);
mPWMControl->Power(percent);
}
}

View File

@@ -1,15 +1,27 @@
#include <boost/log/expressions/formatters/named_scope.hpp>
#include <csignal>
#include <iostream>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/attributes/named_scope.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/expressions/message.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/program_options.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/value_semantic.hpp>
#include <boost/program_options/variables_map.hpp>
#include <csignal>
#include <iostream>
#include <boost/program_options.hpp>
#include <App.h>
namespace po = boost::program_options;
namespace logging = boost::log;
App app;
@@ -17,18 +29,50 @@ static int doInitialSetup = 0;
void signal_handler(int s) { app.Shutdown(); }
void InitLogging(bool verbose) {
logging::add_console_log(
std::clog,
logging::keywords::format =
(logging::expressions::stream
<< "["
<< logging::expressions::format_date_time<boost::posix_time::ptime>(
"TimeStamp", "%Y-%m-%d %H:%M:%S")
<< "]["
<< logging::expressions::format_named_scope(
"Scope", logging::keywords::format = "%c")
<< "]"
<< "[" << logging::trivial::severity << "] "
<< logging::expressions::smessage));
logging::add_common_attributes();
logging::core::get()->add_global_attribute(
"Scope", logging::attributes::named_scope());
BOOST_LOG_FUNCTION();
if (!verbose) {
logging::core::get()->set_filter(logging::trivial::severity >=
logging::trivial::info);
} else {
BOOST_LOG_TRIVIAL(info) << "Verbose logging enabled";
}
}
int main(int argc, char **argv) {
signal(SIGINT, signal_handler);
po::options_description desc("Allowed options");
desc.add_options()("help", "produce help message")("setup", po::bool_switch(),
"run initial setup");
desc.add_options()("help,h", "produce help message")(
"setup,s", po::bool_switch(),
"run initial setup")("verbose,v", po::bool_switch(), "print debug info");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
try {
InitLogging(vm["verbose"].as<bool>());
if (vm.count("help")) {
std::cout << desc << "\n";
return 0;

View File

@@ -1,8 +1,10 @@
#include <boost/json/object.hpp>
#include <boost/log/attributes/named_scope.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <boost/log/trivial.hpp>
#include <pwm/PWMControl.h>
#define PWM_POSTFIX_ENABLE "_enable"
@@ -22,6 +24,8 @@ PWMControl::PWMControl(string controlPath) : mControlPath(controlPath) {
ifstream istrm;
mCurrentValue = Power();
istrm.open(mEnablePath);
istrm >> mInitialEnable;
istrm.close();
@@ -36,22 +40,31 @@ PWMControl::~PWMControl() {
Reset();
}
void PWMControl::pwm(int percent) {
int pwmValue = PWM_MAX_VALUE * percent / 100;
void PWMControl::Power(int percent) {
BOOST_LOG_FUNCTION();
ofstream ostrm(mControlPath, ios::trunc);
ostrm << pwmValue;
ostrm.close();
int pwmValue = PWM_MAX_VALUE * (percent / 100);
if (percent != mCurrentValue) {
BOOST_LOG_TRIVIAL(trace) << "Updating control value to " << percent;
ofstream ostrm(mControlPath, ios::trunc);
ostrm << pwmValue;
ostrm.close();
mCurrentValue = percent;
} else {
BOOST_LOG_TRIVIAL(trace) << "Value unchanged, do nothing";
}
}
int PWMControl::pwm() {
int PWMControl::Power() {
int value;
ifstream istrm;
istrm.open(mControlPath);
istrm >> value;
return value;
return (value / PWM_MAX_VALUE) * 100;
}
void PWMControl::EnableManualControl() {