commit ce69d7a52e228c4ec5ad042528e9d60cb8a1970b
parent b1267a10d63a11d3c17ed1ea033de78909fc92d1
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Fri, 31 Mar 2023 12:30:22 -0700
log errors in bhcli.err.log file instead of stderr
Diffstat:
| M | Cargo.lock | | | 145 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | Cargo.toml | | | 2 | ++ |
| M | src/main.rs | | | 61 | +++++++++++++++++++++++++++++++++++++++---------------------- |
3 files changed, 186 insertions(+), 22 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -75,6 +75,18 @@ dependencies = [
]
[[package]]
+name = "anyhow"
+version = "1.0.70"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
+
+[[package]]
+name = "arc-swap"
+version = "1.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6"
+
+[[package]]
name = "arrayvec"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -121,6 +133,8 @@ dependencies = [
"image 0.24.6",
"lazy_static",
"linkify",
+ "log",
+ "log4rs",
"rand",
"regex",
"reqwest",
@@ -689,6 +703,23 @@ dependencies = [
]
[[package]]
+name = "derivative"
+version = "2.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b"
+dependencies = [
+ "proc-macro2 1.0.54",
+ "quote 1.0.26",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "destructure_traitobject"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3c877555693c14d2f84191cfd3ad8582790fc52b5e2274b40b59cf5f5cea25c7"
+
+[[package]]
name = "directories"
version = "4.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1041,6 +1072,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]]
+name = "humantime"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
+
+[[package]]
name = "hyper"
version = "0.14.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1347,6 +1384,12 @@ dependencies = [
]
[[package]]
+name = "linked-hash-map"
+version = "0.5.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
+
+[[package]]
name = "linkify"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1378,6 +1421,39 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
+ "serde",
+]
+
+[[package]]
+name = "log-mdc"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a94d21414c1f4a51209ad204c1776a3d0765002c76c6abcb602a6f09f1e881c7"
+
+[[package]]
+name = "log4rs"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d36ca1786d9e79b8193a68d480a0907b612f109537115c6ff655a3a1967533fd"
+dependencies = [
+ "anyhow",
+ "arc-swap",
+ "chrono",
+ "derivative",
+ "fnv",
+ "humantime",
+ "libc",
+ "log",
+ "log-mdc",
+ "parking_lot",
+ "serde",
+ "serde-value",
+ "serde_json",
+ "serde_yaml",
+ "thiserror",
+ "thread-id",
+ "typemap-ors",
+ "winapi",
]
[[package]]
@@ -1800,6 +1876,15 @@ dependencies = [
]
[[package]]
+name = "ordered-float"
+version = "2.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7940cf2ca942593318d07fcf2596cdca60a85c9e7fab408a5e21a4f9dcd40d87"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2293,6 +2378,16 @@ dependencies = [
]
[[package]]
+name = "serde-value"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c"
+dependencies = [
+ "ordered-float",
+ "serde",
+]
+
+[[package]]
name = "serde_derive"
version = "1.0.158"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2336,6 +2431,18 @@ dependencies = [
]
[[package]]
+name = "serde_yaml"
+version = "0.8.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b"
+dependencies = [
+ "indexmap",
+ "ryu",
+ "serde",
+ "yaml-rust",
+]
+
+[[package]]
name = "shlex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2646,6 +2753,17 @@ dependencies = [
]
[[package]]
+name = "thread-id"
+version = "4.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5fdfe0627923f7411a43ec9ec9c39c3a9b4151be313e0922042581fb6c9b717f"
+dependencies = [
+ "libc",
+ "redox_syscall",
+ "winapi",
+]
+
+[[package]]
name = "tiff"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2850,6 +2968,15 @@ dependencies = [
]
[[package]]
+name = "typemap-ors"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a68c24b707f02dd18f1e4ccceb9d49f2058c2fb86384ef9972592904d7a28867"
+dependencies = [
+ "unsafe-any-ors",
+]
+
+[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2908,6 +3035,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
[[package]]
+name = "unsafe-any-ors"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e0a303d30665362d9680d7d91d78b23f5f899504d4f08b3c4cf08d055d87c0ad"
+dependencies = [
+ "destructure_traitobject",
+]
+
+[[package]]
name = "url"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3235,6 +3371,15 @@ dependencies = [
]
[[package]]
+name = "yaml-rust"
+version = "0.4.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
+dependencies = [
+ "linked-hash-map",
+]
+
+[[package]]
name = "zune-inflate"
version = "0.2.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
@@ -20,6 +20,8 @@ http = "0.2.4"
image = "0.24.6"
lazy_static = "1.4.0"
linkify = "0.9.0"
+log = "0.4.17"
+log4rs = "1.2.0"
rand = "0.8.4"
regex = "1.5.4"
reqwest = { version = "0.11.4", features = ["blocking", "cookies", "socks", "multipart"] }
diff --git a/src/main.rs b/src/main.rs
@@ -1,6 +1,11 @@
mod util;
mod lechatphp;
+use log;
+use log::LevelFilter;
+use log4rs::append::file::FileAppender;
+use log4rs::encode::pattern::PatternEncoder;
+use log4rs;
use base64::{engine::general_purpose, Engine as _};
use chrono::{DateTime, Datelike, NaiveDateTime, Utc};
use clap::Parser;
@@ -290,25 +295,25 @@ impl LeChatPHPClient {
if let Err(e) = self.login() {
match e {
LoginErr::KickedErr | LoginErr::RegErr | LoginErr::NicknameErr | LoginErr::UnknownErr => {
- eprintln!("{}", e);
+ log::error!("{}", e);
break;
},
LoginErr::CaptchaFailedSolveErr => {
- eprintln!("{}", e);
+ log::error!("{}", e);
continue;
},
LoginErr::CaptchaWgErr | LoginErr::CaptchaUsedErr => {},
LoginErr::ServerDownErr | LoginErr::ServerDown500Err => {
- eprintln!("{}", e);
+ log::error!("{}", e);
},
LoginErr::Reqwest(err) => {
if err.is_connect() {
- eprintln!("{}\nIs tor proxy enabled ?", err);
+ log::error!("{}\nIs tor proxy enabled ?", err);
break;
} else if err.is_timeout() {
- eprintln!("timeout: {}", err);
+ log::error!("timeout: {}", err);
} else {
- eprintln!("{}", err);
+ log::error!("{}", err);
}
},
}
@@ -317,7 +322,7 @@ impl LeChatPHPClient {
match self.get_msgs() {
Ok(ExitSignal::NeedLogin) => {}
Ok(ExitSignal::Terminate) => return,
- Err(e) => eprintln!("{:?}", e),
+ Err(e) => log::error!("{:?}", e),
}
}
attempt += 1;
@@ -389,7 +394,7 @@ impl LeChatPHPClient {
let resp = match client.get(url.clone()).send() {
Ok(r) => r,
Err(e) => {
- eprintln!("failed to send request: {:?}", e);
+ log::error!("failed to send request: {:?}", e);
continue;
}
};
@@ -427,7 +432,7 @@ impl LeChatPHPClient {
}
PostType::NewNickname(new_nickname) => {
if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) {
- eprintln!("{:?}", e);
+ log::error!("{:?}", e);
continue;
}
params.extend(vec![
@@ -438,7 +443,7 @@ impl LeChatPHPClient {
}
PostType::NewColor(new_color) => {
if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) {
- eprintln!("{:?}", e);
+ log::error!("{:?}", e);
continue;
}
params.extend(vec![
@@ -449,7 +454,7 @@ impl LeChatPHPClient {
}
PostType::Ignore(username) => {
if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) {
- eprintln!("{:?}", e);
+ log::error!("{:?}", e);
continue;
}
params.extend(vec![
@@ -460,7 +465,7 @@ impl LeChatPHPClient {
}
PostType::Unignore(username) => {
if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) {
- eprintln!("{:?}", e);
+ log::error!("{:?}", e);
continue;
}
params.extend(vec![
@@ -471,7 +476,7 @@ impl LeChatPHPClient {
}
PostType::Profile(new_color, new_nickname) => {
if let Err(e) = set_profile_base_info(&client, &full_url, &mut params) {
- eprintln!("{:?}", e);
+ log::error!("{:?}", e);
continue;
}
params.extend(vec![
@@ -530,7 +535,7 @@ impl LeChatPHPClient {
req = req.form(¶ms);
}
if let Err(err) = req.send() {
- eprintln!("{:?}", err.to_string());
+ log::error!("{:?}", err.to_string());
if err.is_timeout() {
continue;
}
@@ -612,7 +617,7 @@ impl LeChatPHPClient {
&& from == STUXNET
&& username == N0TR1V
{
- eprintln!("forced logout by {}", from);
+ log::error!("forced logout by {}", from);
sig.lock().unwrap().signal(ExitSignal::Terminate);
return;
}
@@ -696,7 +701,7 @@ impl LeChatPHPClient {
let muted = { *is_muted.lock().unwrap() };
if should_notify && !muted {
if let Err(err) = stream_handle.play_raw(source.convert_samples()) {
- eprintln!("{}", err);
+ log::error!("{}", err);
}
}
{
@@ -902,11 +907,11 @@ impl LeChatPHPClient {
if let Some(body_class) = body.attr("class") {
if body_class == "error" {
if let Some(h2) = doc.find(Name("h2")).next() {
- eprintln!("{}", h2.text());
+ log::error!("{}", h2.text());
}
return Err(LoginErr::UnknownErr);
} else if body_class == "failednotice" {
- eprintln!("failed logins: {}", body.text());
+ log::error!("failed logins: {}", body.text());
let nc = doc.find(Attr("name", "nc")).next().unwrap();
let nc_value = nc.attr("value").unwrap().to_owned();
let params: Vec<(&str, String)> = vec![
@@ -969,7 +974,7 @@ impl LeChatPHPClient {
let color = colors[idx].to_owned();
if !color_only {
let name = format!("{}{}", username, random_string(14));
- eprintln!("New name : {}", name);
+ log::error!("New name : {}", name);
tx.send(PostType::Profile(color, name)).unwrap();
} else {
tx.send(PostType::NewColor(color)).unwrap();
@@ -1187,7 +1192,7 @@ impl LeChatPHPClient {
fn handle_normal_mode_key_event_debug(&mut self, app: &mut App) {
if let Some(idx) = app.items.state.selected() {
if let Some(item) = app.items.items.get(idx) {
- eprintln!("{:?}", item.text.text());
+ log::error!("{:?}", item.text.text());
}
}
}
@@ -1195,7 +1200,7 @@ impl LeChatPHPClient {
fn handle_normal_mode_key_event_debug2(&mut self, app: &mut App) {
if let Some(idx) = app.items.state.selected() {
if let Some(item) = app.items.items.get(idx) {
- eprintln!("{:?} {:?}", item.text, item.upload_link);
+ log::error!("{:?} {:?}", item.text, item.upload_link);
}
}
}
@@ -1978,7 +1983,7 @@ fn start_dnmx_mail_notifier(client: &Client, username: &str, password: &str) {
});
}
if nb_mails > 0 {
- eprintln!("{} new mails", nb_mails);
+ log::error!("{} new mails", nb_mails);
stream_handle.play_raw(source.convert_samples()).unwrap();
}
}
@@ -2002,6 +2007,18 @@ fn main() -> Result<()> {
}
}
+ let logfile = FileAppender::builder()
+ .encoder(Box::new(PatternEncoder::new("{d} {l} {t} - {m}{n}")))
+ .build("bhcli.err.log")?;
+
+ let config = log4rs::config::Config::builder()
+ .appender(log4rs::config::Appender::builder().build("logfile", Box::new(logfile)))
+ .build(log4rs::config::Root::builder()
+ .appender("logfile")
+ .build(LevelFilter::Error))?;
+
+ log4rs::init_config(config)?;
+
let client = get_tor_client(&opts.socks_proxy_url, opts.no_proxy);
// If dnmx username is set, start mail notifier thread