diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index cb7e3a3..81b966c 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -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 diff --git a/app/include/pwm/PWMControl.h b/app/include/pwm/PWMControl.h index 60e951f..40ae6af 100644 --- a/app/include/pwm/PWMControl.h +++ b/app/include/pwm/PWMControl.h @@ -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; diff --git a/app/src/FanGenerator.cxx b/app/src/FanGenerator.cxx index 8f64d73..1fbbe80 100644 --- a/app/src/FanGenerator.cxx +++ b/app/src/FanGenerator.cxx @@ -22,7 +22,7 @@ FanGenerator::FindFans(vector> 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> 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> rpmSensors, } cout << "Setting fan back to 100%" << endl; - c->pwm(100); + c->Power(100); } return mapping; diff --git a/app/src/fan/HwmonFan.cxx b/app/src/fan/HwmonFan.cxx index 0145753..22a56fc 100644 --- a/app/src/fan/HwmonFan.cxx +++ b/app/src/fan/HwmonFan.cxx @@ -19,9 +19,9 @@ HwmonFan::HwmonFan(std::shared_ptr pwmControl, void HwmonFan::PWM(int percent) { if (percent < mMinPWM) { - mPWMControl->pwm(mMinPWM); + mPWMControl->Power(mMinPWM); } else { - mPWMControl->pwm(percent); + mPWMControl->Power(percent); } } diff --git a/app/src/main.cxx b/app/src/main.cxx index 17c4456..a4c7c8a 100644 --- a/app/src/main.cxx +++ b/app/src/main.cxx @@ -1,15 +1,27 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include #include -#include -#include - -#include #include 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( + "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()); + if (vm.count("help")) { std::cout << desc << "\n"; return 0; diff --git a/app/src/pwm/PWMControl.cxx b/app/src/pwm/PWMControl.cxx index bc01937..54ffe1b 100644 --- a/app/src/pwm/PWMControl.cxx +++ b/app/src/pwm/PWMControl.cxx @@ -1,8 +1,10 @@ -#include +#include #include #include #include +#include + #include #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() {