time::{self, Duration},
};
+use anyhow::Result;
+use log::{error, info, trace, warn, debug};
use windows::Win32::Foundation::{ERROR_SERVICE_DOES_NOT_EXIST, WIN32_ERROR};
use windows_service::{
service::{
mod settings;
mod timer;
-fn main() -> Result<(), windows_service::Error> {
+fn main() -> Result<()> {
+ let is_debug = cfg!(debug_assertions);
+
+ flexi_logger::Logger::try_with_str(if is_debug { "debug" } else { "info" })?
+ .log_to_file(
+ flexi_logger::FileSpec::default()
+ .directory(dirs::config_dir().unwrap().join(consts::SERVICE_NAME))
+ .basename(consts::SERVICE_NAME),
+ )
+ .duplicate_to_stdout(flexi_logger::Duplicate::All)
+ .format(if is_debug { flexi_logger::default_format } else { flexi_logger::detailed_format })
+ .rotate(
+ flexi_logger::Criterion::Size(1024 * 1024),
+ flexi_logger::Naming::Timestamps,
+ flexi_logger::Cleanup::KeepLogFiles(10),
+ )
+ .print_message()
+ .start()?;
+
let args: Vec<String> = env::args().collect();
- println!("Temperature to RGB");
+ info!("Temperature to RGB");
if args.contains(&"--no-service".to_string()) {
let completed: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
};
let service = service_manager.create_service(&service_info, ServiceAccess::CHANGE_CONFIG)?;
service.set_description(
- "A service to set the color of hardware according to the temperatur of GPU and CPU",
+ "A service to set the color of hardware according to the temperature of GPU and CPU",
)?;
Ok(())
}
}
}
-fn run_service(arguments: Vec<OsString>) -> Result<(), windows_service::Error> {
+fn run_service(_arguments: Vec<OsString>) -> Result<(), windows_service::Error> {
let completed: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
let completed_event_handler = Arc::clone(&completed);
// Handle stop event and return control back to the system.
ServiceControlHandlerResult::NoError
}
- // ServiceControl::Shutdown => {
- // completed_event_handler.store(true, Ordering::Relaxed);
- // // Handle stop event and return control back to the system.
- // ServiceControlHandlerResult::NoError
- // }
+ ServiceControl::Shutdown => {
+ completed_event_handler.store(true, Ordering::Relaxed);
+ // Handle stop event and return control back to the system.
+ ServiceControlHandlerResult::NoError
+ }
// ServiceControl::Preshutdown => {
// completed_event_handler.store(true, Ordering::Relaxed);
// ServiceControlHandlerResult::NoError
current_state: ServiceState::Running,
// Accept stop events when running
- controls_accepted: ServiceControlAccept::STOP, // | ServiceControlAccept::SHUTDOWN,
+ controls_accepted: ServiceControlAccept::STOP | ServiceControlAccept::SHUTDOWN,
// Used to report an error when starting or stopping only, otherwise must be zero
exit_code: ServiceExitCode::Win32(0),
process_id: None,
})?;
+ info!("Main loop stopped: Temperature to RGB will now shut down");
+
Ok(())
}