time::{self, Duration},
};
+use anyhow::Result;
+use log::{debug, error, info, trace, warn};
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 service_main(arguments: Vec<OsString>) {
if let Err(error) = run_service(arguments) {
- println!("Error: {error}");
+ error!("{error}");
}
}
-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);
+
+ info!("Setuping the event handler...");
+
let event_handler = move |control_event| -> ServiceControlHandlerResult {
match control_event {
ServiceControl::Stop => {
// 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
// Register system service event handler
let status_handle = service_control_handler::register(consts::SERVICE_NAME, event_handler)?;
- let running_status = ServiceStatus {
- // Should match the one from system service registry
+ status_handle.set_service_status(ServiceStatus {
service_type: ServiceType::OWN_PROCESS,
-
- // The new state
current_state: ServiceState::Running,
-
- // Accept stop events when running
- controls_accepted: ServiceControlAccept::STOP, // | ServiceControlAccept::SHUTDOWN,
-
- // Used to report an error when starting or stopping only, otherwise must be zero
+ controls_accepted: ServiceControlAccept::STOP | ServiceControlAccept::SHUTDOWN,
exit_code: ServiceExitCode::Win32(0),
-
- // Only used for pending states, otherwise must be zero
checkpoint: 0,
-
- // Only used for pending states, otherwise must be zero
wait_hint: Duration::default(),
-
- process_id: None,
- };
-
- status_handle.set_service_status(running_status)?;
+ process_id: None, //Some(std::process::id()),
+ })?;
main_loop::main_loop(completed.clone());
exit_code: ServiceExitCode::Win32(0),
checkpoint: 0,
wait_hint: Duration::default(),
- process_id: None,
+ process_id: None, //Some(std::process::id()),
})?;
+ info!("Main loop stopped: Temperature to RGB will now shut down");
+
Ok(())
}