Skip to content

Commit

Permalink
Add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lujjjh committed Sep 5, 2021
1 parent d5fc99f commit 286088c
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 46 deletions.
91 changes: 91 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ tokio = { version = "1.10.0", features = ["full"] }
lrc = "0.1.6"
html-escape = "0.2.9"
once_cell = "1.8.0"
flexi_logger = "0.18"
log = "0.4"
anyhow = "1.0"

[build-dependencies]
embed-resource = "1.6"
1 change: 1 addition & 0 deletions bindings/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() {
Windows::Win32::System::LibraryLoader::*,
Windows::Win32::System::Threading::*,
Windows::Win32::UI::Animation::*,
Windows::Win32::UI::Shell::*,
Windows::Win32::UI::WindowsAndMessaging::*,
};
}
47 changes: 47 additions & 0 deletions src/initialize.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use std::path::Path;
use std::process::exit;
use std::ptr::null_mut;

use bindings::Windows::Win32::System::Com::*;
use bindings::Windows::Win32::System::Diagnostics::Debug::*;
use bindings::Windows::Win32::System::Threading::*;
use bindings::Windows::Win32::UI::Shell::*;
use bindings::Windows::Win32::UI::WindowsAndMessaging::*;
use flexi_logger::detailed_format;
use flexi_logger::Cleanup;
use flexi_logger::Criterion;
use flexi_logger::FileSpec;
use flexi_logger::Logger;
use flexi_logger::Naming;
use windows::*;

#[cfg(debug_assertions)]
Expand All @@ -29,6 +37,45 @@ pub fn initialize() -> Result<()> {
exit(1);
}

let roaming_app_data_path = known_folder_path(&FOLDERID_RoamingAppData)?;
let log_directory = Path::new(&roaming_app_data_path)
.join("iLyrics")
.join("logs");
Logger::try_with_str("info")
.unwrap()
.log_to_file(
FileSpec::default()
.directory(log_directory)
.basename("iLyrics"),
)
.format(detailed_format)
.rotate(
Criterion::Size(5 * 1_024 * 1_024),
Naming::Timestamps,
Cleanup::KeepLogFiles(0),
)
.start()
.unwrap();

Ok(())
}
}

fn known_folder_path(id: &windows::Guid) -> Result<String> {
unsafe {
let path = SHGetKnownFolderPath(id, 0, None)?;
if path.0.is_null() {
return Ok(String::new());
}
let mut end = path.0;
while *end != 0 {
end = end.add(1);
}
let result = String::from_utf16_lossy(std::slice::from_raw_parts(
path.0,
end.offset_from(path.0) as _,
));
CoTaskMemFree(path.0 as _);
Ok(result)
}
}
43 changes: 28 additions & 15 deletions src/lyrics/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::time::Duration;

use log::error;
use log::info;
use log::warn;
use lrc::Lyrics;
use reqwest::blocking::Client;

Expand Down Expand Up @@ -30,15 +33,36 @@ impl Query {
) -> Result<bool, Box<dyn std::error::Error>> {
let query = format!("{} {}", name, artist);
if self.last_query != query {
if name.is_empty() || artist.is_empty() {
*lyrics = None;
return Ok(true);
}
info!("{}", &query);
self.last_query = query;
*lyrics = None;
let body = self
let response = self
.client
.get("https://lyrics-api.lujjjh.com/")
.query(&[("name", name), ("artist", artist)])
.send()?
.text()?;
let downloaded_lyrics = Lyrics::from_str(body)?;
.send()
.map_err(|e| {
error!("Network error: {:?}", e);
e
})?
.error_for_status()
.map_err(|e| {
warn!("Bad status: {:?}", e);
e
})?;
let body = response.text().map_err(|e| {
error!("Failed to read response body: {:?}", e);
e
})?;
let downloaded_lyrics = Lyrics::from_str(body).map_err(|e| {
error!("Failed to parse lyrics: {:?}", e);
e
})?;
info!("OK");
let mut new_lyrics = Lyrics::new();
let timed_lines = downloaded_lyrics.get_timed_lines();
for (i, (time_tag, line)) in timed_lines.iter().enumerate() {
Expand All @@ -58,17 +82,6 @@ impl Query {
}
*lyrics = Some(new_lyrics);
Ok(true)

// self.lines = lyrics
// .get_timed_lines()
// .as_ref()
// .iter()
// .map(|(time_tag, s)| {
// let duration = Duration::from_millis(time_tag.get_timestamp() as u64);
// let s = html_escape::decode_html_entities(&s).trim().to_string();
// (duration, s)
// })
// .collect::<Vec<(Duration, String)>>();
} else {
Ok(false)
}
Expand Down
Loading

0 comments on commit 286088c

Please sign in to comment.