diff --git a/.env b/.env deleted file mode 100644 index d751096..0000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -BROKER_URL=tcp://127.0.0.1:1883 diff --git a/Cargo.lock b/Cargo.lock index 423bbf0..6b6861a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,38 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + [[package]] name = "cc" version = "1.0.69" @@ -20,6 +46,34 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cmake" version = "0.1.45" @@ -29,6 +83,17 @@ dependencies = [ "cc", ] +[[package]] +name = "colored" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + [[package]] name = "futures" version = "0.3.16" @@ -129,6 +194,21 @@ dependencies = [ "slab", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[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.98" @@ -148,9 +228,11 @@ dependencies = [ name = "logs-publisher" version = "0.1.0" dependencies = [ + "clap", "log", "logwatcher", "paho-mqtt", + "simple_logger", ] [[package]] @@ -165,6 +247,25 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + [[package]] name = "openssl-sys" version = "0.9.65" @@ -250,12 +351,31 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "simple_logger" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd57f17c093ead1d4a1499dc9acaafdd71240908d64775465543b8d9a9f1d198" +dependencies = [ + "atty", + "chrono", + "colored", + "log", + "winapi", +] + [[package]] name = "slab" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f173ac3d1a7e3b28003f40de0b5ce7fe2710f9b9dc3fc38664cebee46b3b6527" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "syn" version = "1.0.74" @@ -267,6 +387,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.26" @@ -287,6 +416,23 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.2.2" @@ -298,3 +444,37 @@ name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 04c4e75..6baaae9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,20 +3,16 @@ name = "logs-publisher" version = "0.1.0" edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] # Mqtt paho-mqtt = "0.9" # Watching file -#hotwatch = "0.4.5" -#crossbeam-channel = "0.4.0" -#notify = "4.0.17" -#linemux = "0.2" logwatcher = "0.1.1" #log log = "0.4.14" +simple_logger = "1.11.0" + +clap = "2.33.3" -#tokio = { version = "1", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 04a1080..791be00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,30 +1,48 @@ -use std::{process, thread}; -use paho_mqtt::{QOS_0}; +use std::{process}; +use paho_mqtt::QOS_0; use std::path::Path; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::fs::File; -use std::io::BufReader; -use std::error::Error; -use std::time::Duration; + +use log::{error, LevelFilter}; use logwatcher::{LogWatcher, LogWatcherAction}; +use simple_logger::SimpleLogger; + +use clap::{App, Arg}; #[macro_use] extern crate log; -static URL: &str = "tcp://127.0.0.1:1883"; +fn main() -> std::io::Result<()> { + // create cli app and get Arg + let matches = App::new("logs-publisher") + .version(env!("CARGO_PKG_VERSION")) + .author("Urvoy L") + .about("Read the log file and publish log to MQTT topic") + .arg( + Arg::with_name("file").short("f").long("file").help("Set the file to read").required(true).takes_value(true), + ) + .arg( + Arg::with_name("broker_url").short("u").long("broker_url").help("Set the URL of MQTT broker").required(true).takes_value(true) + ) + .arg( + Arg::with_name("topic").short("t").long("topic").help("MQTT Topic target for publish").required(true).takes_value(true) + ) + .get_matches(); + let broker_url = matches.value_of("broker_url").unwrap(); + let file_path = matches.value_of("file").unwrap(); + let topic = matches.value_of("topic").unwrap(); + + SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); -fn main() -> std::io::Result<()> { info!("Starting program ..."); - let cli = paho_mqtt::Client::new(URL).unwrap_or_else(|err| { + let cli = paho_mqtt::Client::new(broker_url).unwrap_or_else(|err| { error!("Error creating the client: {:?}", err); process::exit(1); }); let conn_opts = paho_mqtt::ConnectOptionsBuilder::new().clean_session(true) - // .client_id(std::env::var("MQTT_CLIENT_ID").unwrap_or_else(|_| "logs-publisher".to_string())) .finalize(); // Connect and wait for it to complete or fail @@ -33,18 +51,28 @@ fn main() -> std::io::Result<()> { process::exit(1); } - // let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/data.json"); - let path = Path::new("/Users/urvoy/Developpement/MOB_MDGate/logs/2021-07-27 13-49-47/").join("MDGate.log"); + info!("MQTT Connected to {}", broker_url); + + let path = Path::new(file_path); let mut log_watcher = LogWatcher::register(path).unwrap(); log_watcher.watch(&mut move |line: String| { - let msg = paho_mqtt::Message::new("toto/tata", line, QOS_0); - cli.publish(msg).unwrap(); + let msg = paho_mqtt::Message::new(topic, line.clone(), QOS_0); + if cli.is_connected() { + cli.publish(msg).unwrap(); + info!("{}", line); + } else { + info!("MQTT cli not connected when publish, try to reconnect to {}", broker_url); + if cli.reconnect().is_ok() { + cli.publish(msg).unwrap(); + info!("{}", line); + } else { + warn!("Fail to MQTT reconnect"); + } + } LogWatcherAction::None }); Ok(()) } - -