diff --git a/src/driver.cpp b/src/driver.cpp index 0cd17aa..802c3f9 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -35,18 +35,17 @@ Driver::Driver(bool optional, unsigned int max_errors) void Driver::try_init() { robust_op( - [&] () { + [&]/* op_fn */() { if (!available()) path_.emplace(lookup()); init(); initialized_ = true; }, - [&] (const ExpectedError &e) { /* skip_fn */ - if (!optional()) - log(TF_WRN) - << "Error " << errors() << "/" << max_errors() - << " while initializing driver: " << e.what() << flush - ; + [&]/* skip_fn */(const ExpectedError &e) { + log(optional() ? TF_DBG : TF_INF) << "Ignoring error "; + if (max_errors() && !optional()) + log() << errors() << "/" << max_errors() << " "; + log() << "while initializing " << type_name() << ": " << e.what() << flush; } ); } @@ -58,6 +57,9 @@ void Driver::robust_op(FN op_fn, FN skip_ errors_++; op_fn(); errors_ = 0; + } catch (DriverInitError &e) { + e.set_context(type_name()); + handle_io_error_(e, skip_fn); } catch (SystemError &e) { handle_io_error_(e, skip_fn); } catch (IOerror &e) { diff --git a/src/driver.h b/src/driver.h index 0c5cc08..48fbeea 100644 --- a/src/driver.h +++ b/src/driver.h @@ -72,6 +72,9 @@ class Driver { * The string returned by this will be accessible via the @a path() method. */ virtual string lookup() = 0; + /// @return A user-friendly name for the type of driver represented by the implementor + virtual string type_name() const = 0; + virtual void skip_io_error(const ExpectedError &); opt path_; diff --git a/src/error.cpp b/src/error.cpp index bf87697..da37306 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -222,5 +222,8 @@ MissingEntry::MissingEntry(const string &entry) InvocationError::InvocationError(const string &message) : ExpectedError("Invalid command line: " + message) {} +void DriverInitError::set_context(const string &context) +{ msg_ = context + ": " + msg_; } + } diff --git a/src/error.h b/src/error.h index 1acae98..8876016 100644 --- a/src/error.h +++ b/src/error.h @@ -138,12 +138,8 @@ class SystemError : public ExpectedError { class DriverInitError : public SystemError { public: using SystemError::SystemError; -}; - -class DriverIOError : public SystemError { -public: - using SystemError::SystemError; + void set_context(const string &context); }; diff --git a/src/fans.cpp b/src/fans.cpp index 06e2cf7..567c0c7 100644 --- a/src/fans.cpp +++ b/src/fans.cpp @@ -191,6 +191,9 @@ string TpFanDriver::lookup() return path_; } +string TpFanDriver::type_name() const +{ return "tpacpi fan driver"; } + /*---------------------------------------------------------------------------- | HwmonFanDriver: Driver for PWM fans, typically somewhere in sysfs. | @@ -255,6 +258,9 @@ void HwmonFanDriver::init() string HwmonFanDriver::lookup() { return hwmon_interface_->lookup(); } +string HwmonFanDriver::type_name() const +{ return "hwmon fan driver"; } + void HwmonFanDriver::set_speed(const Level &level) { diff --git a/src/fans.h b/src/fans.h index 05fefe6..5ab0ea7 100644 --- a/src/fans.h +++ b/src/fans.h @@ -71,6 +71,7 @@ class TpFanDriver : public FanDriver { protected: virtual void init() override; virtual string lookup() override; + virtual string type_name() const override; private: const string path_; @@ -93,6 +94,7 @@ class HwmonFanDriver : public FanDriver { protected: virtual void init() override; virtual string lookup() override; + virtual string type_name() const override; private: shared_ptr> hwmon_interface_; diff --git a/src/message.cpp b/src/message.cpp index 6a1745a..b95bc17 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -60,6 +60,23 @@ LogLevel &operator++(LogLevel &l) } +Logger &log(LogLevel lvl) +{ + Logger::instance().level(lvl); + return Logger::instance(); +} + + +Logger &flush(Logger &l) +{ return l.flush(); } + +Logger &log() +{ return Logger::instance(); } + + + + + Logger::Logger() : syslog_(false), log_lvl_(DEFAULT_LOG_LVL), @@ -78,16 +95,6 @@ LogLevel &Logger::log_lvl() { return log_lvl_; } -Logger &flush(Logger &l) { return l.flush(); } - - -Logger &log(LogLevel lvl) -{ - Logger::instance().level(lvl); - return Logger::instance(); -} - - Logger::~Logger() { flush(); @@ -106,19 +113,13 @@ void Logger::enable_syslog() Logger &Logger::flush() { - if (msg_pfx_.length() == 0) return *this; + if (msg_pfx_.length() == 0) + return *this; if (msg_lvl_ <= log_lvl_) { - if (syslog_) { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-security" - // I think we can safely do this because thinkfan doesn't receive - // any data from unprivileged processes. + if (syslog_) syslog(msg_lvl_, "%s", msg_pfx_.c_str()); -#pragma GCC diagnostic pop - } - else { + else std::cerr << msg_pfx_ << std::endl; - } } msg_pfx_ = ""; @@ -196,4 +197,5 @@ Logger &Logger::operator<< (const vector> &fan_configs) + } diff --git a/src/message.h b/src/message.h index 3514915..d419497 100644 --- a/src/message.h +++ b/src/message.h @@ -94,7 +94,9 @@ class Logger { std::exception_ptr exception_; }; + Logger &flush(Logger &l); +Logger &log(); Logger &log(LogLevel lvl); template void error(const ArgTs &... args) { @@ -104,7 +106,9 @@ template void error(const ArgTs &... args) { log(TF_ERR) << ErrT(args...).what() << flush; } -} + +} // namespace thinkfan + #ifdef USE_ATASMART #define DND_DISK_HELP \ diff --git a/src/sensors.cpp b/src/sensors.cpp index 917c3f5..5a4081c 100644 --- a/src/sensors.cpp +++ b/src/sensors.cpp @@ -168,6 +168,9 @@ void HwmonSensorDriver::read_temps_() string HwmonSensorDriver::lookup() { return hwmon_interface_->lookup(); } +string HwmonSensorDriver::type_name() const +{ return "hwmon sensor driver"; } + /*---------------------------------------------------------------------------- | TpSensorDriver: A driver for sensors provided by thinkpad_acpi, typically | @@ -272,6 +275,9 @@ string TpSensorDriver::lookup() throw IOerror(MSG_SENSOR_INIT(path()), errno); } +string TpSensorDriver::type_name() const +{ return "tpacpi sensor driver"; } + #ifdef USE_ATASMART /*---------------------------------------------------------------------------- @@ -343,6 +349,9 @@ void AtasmartSensorDriver::read_temps_() string AtasmartSensorDriver::lookup() { return device_path_; } +string AtasmartSensorDriver::type_name() const +{ return "atasmart sensor driver"; } + #endif /* USE_ATASMART */ @@ -421,6 +430,9 @@ void NvmlSensorDriver::read_temps_() string NvmlSensorDriver::lookup() { return bus_id_; } +string NvmlSensorDriver::type_name() const +{ return "NVML sensor driver"; } + #endif /* USE_NVML */ @@ -469,7 +481,6 @@ void LMSensorsDriver::set_unavailable() { path_.reset(); } - string LMSensorsDriver::lookup() { if (!libsensors_iface_) @@ -481,8 +492,8 @@ string LMSensorsDriver::lookup() } - - +string LMSensorsDriver::type_name() const +{ return "libsensors sensor driver"; } void LMSensorsDriver::read_temps_() diff --git a/src/sensors.h b/src/sensors.h index c1deb5d..6b26e77 100644 --- a/src/sensors.h +++ b/src/sensors.h @@ -93,6 +93,7 @@ class HwmonSensorDriver : public SensorDriver { protected: virtual void read_temps_() override; virtual string lookup() override; + virtual string type_name() const override; private: shared_ptr> hwmon_interface_; @@ -113,6 +114,7 @@ class TpSensorDriver : public SensorDriver { virtual void init() override; virtual void read_temps_() override; virtual string lookup() override; + virtual string type_name() const override; private: std::char_traits::off_type skip_bytes_; @@ -128,10 +130,13 @@ class AtasmartSensorDriver : public SensorDriver { public: AtasmartSensorDriver(string device_path, bool optional, opt> correction = nullopt, opt max_errors = nullopt); virtual ~AtasmartSensorDriver(); + protected: virtual void init() override; virtual void read_temps_() override; virtual string lookup() override; + virtual string type_name() const override; + private: SkDisk *disk_; const string device_path_; @@ -144,10 +149,13 @@ class NvmlSensorDriver : public SensorDriver { public: NvmlSensorDriver(string bus_id, bool optional, opt> correction = nullopt, opt max_errors = nullopt); virtual ~NvmlSensorDriver() noexcept(false) override; + protected: virtual void init() override; virtual void read_temps_() override; virtual string lookup() override; + virtual string type_name() const override; + private: const string bus_id_; nvmlDevice_t device_; @@ -185,6 +193,7 @@ class LMSensorsDriver : public SensorDriver { virtual void init() override; virtual void read_temps_() override; virtual string lookup() override; + virtual string type_name() const override; private: const string chip_name_; diff --git a/src/thinkfan.cpp b/src/thinkfan.cpp index 0c624cc..bae2fb8 100644 --- a/src/thinkfan.cpp +++ b/src/thinkfan.cpp @@ -360,11 +360,11 @@ int main(int argc, char **argv) { #endif if (daemonize) { + LogLevel old_lvl = Logger::instance().log_lvl(); { // Test the config before forking unique_ptr test_cfg(Config::read_config(config_files)); - LogLevel old_lvl = Logger::instance().log_lvl(); Logger::instance().log_lvl() = TF_ERR; temp_state = TemperatureState(test_cfg->num_temps()); @@ -373,9 +373,10 @@ int main(int argc, char **argv) { for (auto &sensor : test_cfg->sensors()) sensor->read_temps(); - Logger::instance().log_lvl() = old_lvl; // Own scope so the config gets destroyed before forking } + Logger::instance().log_lvl() = old_lvl; + pid_t child_pid = ::fork(); if (child_pid < 0) {