More logging

This commit is contained in:
Luke
2020-04-19 10:25:50 +12:00
parent cfddb6077a
commit 221b230acb
6 changed files with 55 additions and 16 deletions

1
Cargo.lock generated
View File

@@ -282,6 +282,7 @@ version = "0.4.0"
dependencies = [
"aho-corasick",
"gumdrop",
"log",
"rusb",
"serde",
"serde_derive",

View File

@@ -2,7 +2,7 @@
Description=ROG Core Daemon
[Service]
Environment="RUST_LOG=info"
Environment="ROGCORE_LOG=info"
ExecStart=/usr/bin/rog-core -d
Restart=on-failure

View File

@@ -42,8 +42,9 @@ struct LedModeCommand {
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut builder = Builder::from_default_env();
let mut builder = Builder::from_env("ROGCORE_LOG");
builder.target(Target::Stdout);
builder.format_timestamp(None);
builder.filter(None, LevelFilter::Info).init();
let parsed = CLIStart::parse_args_default_or_exit();

View File

@@ -13,3 +13,4 @@ toml = "0.5"
sysfs-class = "0.1.2"
aho-corasick = "0.7"
thiserror = "1.0.15"
log = "0.4"

View File

@@ -1,6 +1,9 @@
// Return show-stopping errors, otherwise map error to a log level
use crate::{aura::BuiltInModeByte, config::Config, error::AuraError, laptops::*};
use aho_corasick::AhoCorasick;
use gumdrop::Options;
use log::{debug, warn};
use rusb::DeviceHandle;
use std::cell::{Ref, RefCell};
use std::process::Command;
@@ -143,8 +146,10 @@ impl RogCore {
self.aura_write_messages(&messages)?;
self.config.set_field_from(bytes);
self.config.write();
debug!("Wrote: {:X?}", bytes);
return Ok(());
}
warn!("{:?} not supported", BuiltInModeByte::from(mode));
Err(AuraError::NotSupported)
}
@@ -173,25 +178,41 @@ impl RogCore {
std::process::Command::new("systemctl")
.arg("suspend")
.spawn()
.expect("failed to suspend");
.map_or_else(|err| warn!("Failed to suspend: {}", err), |_| {});
}
pub fn toggle_airplane_mode(&self) -> Result<(), AuraError> {
pub fn toggle_airplane_mode(&self) {
match Command::new("rfkill").arg("list").output() {
Ok(output) => {
if output.status.success() {
let patterns = &["yes"];
let ac = AhoCorasick::new(patterns);
if ac.earliest_find(output.stdout).is_some() {
Command::new("rfkill").arg("unblock").arg("all").spawn()?;
Command::new("rfkill")
.arg("unblock")
.arg("all")
.spawn()
.map_or_else(
|err| warn!("Could not unblock rf devices: {}", err),
|_| {},
);
} else {
Command::new("rfkill").arg("block").arg("all").spawn()?;
let _ = Command::new("rfkill")
.arg("block")
.arg("all")
.spawn()
.map_or_else(
|err| warn!("Could not block rf devices: {}", err),
|_| {},
);
}
return Ok(());
} else {
warn!("Could not list rf devices");
}
return Err(AuraError::CommandFailed);
}
Err(err) => Err(AuraError::from(err)),
Err(err) => {
warn!("Could not list rf devices: {}", err);
}
}
}
}
@@ -219,19 +240,33 @@ impl Backlight {
panic!("Backlight not found")
}
pub fn step_up(&self) {
let brightness = self.backlight.brightness().unwrap();
let brightness = self
.backlight
.brightness()
.map_err(|err| warn!("Failed to fetch backlight level: {}", err))
.unwrap();
if brightness + self.step <= self.max {
self.backlight
.set_brightness(brightness + self.step)
.unwrap();
.map_or_else(
|err| warn!("Failed to increment backlight level: {}", err),
|_| {},
);
}
}
pub fn step_down(&self) {
let brightness = self.backlight.brightness().unwrap();
let brightness = self
.backlight
.brightness()
.map_err(|err| warn!("Failed to fetch backlight level: {}", err))
.unwrap();
if brightness > self.step {
self.backlight
.set_brightness(brightness - self.step)
.unwrap();
.map_or_else(
|err| warn!("Failed to increment backlight level: {}", err),
|_| {},
);
}
}
}

View File

@@ -1,6 +1,7 @@
use crate::aura::BuiltInModeByte;
use crate::core::{Backlight, RogCore};
use crate::error::AuraError;
use log::info;
pub fn match_laptop() -> Result<Box<dyn Laptop>, AuraError> {
let dmi = sysfs_class::DmiId::default();
@@ -136,11 +137,11 @@ impl Laptop for LaptopGX502GW {
rogcore.suspend();
}
GX502GWKeys::AirplaneMode => {
rogcore.toggle_airplane_mode()?;
rogcore.toggle_airplane_mode();
}
_ => {
if key_byte != 0 {
dbg!(&key_byte);
info!("Unmapped key: {}", &key_byte);
}
}
}