commit 6950a5725a31fc6bf5d2023c5182d57b51069731
parent de99c0c2a9cf165e7288f55506cf9c75c8e65475
Author: MTRNord <mtrnord1@gmail.com>
Date: Fri, 4 Dec 2020 23:59:36 +0100
refactor: use tracing for better logging
Diffstat:
4 files changed, 130 insertions(+), 6 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -38,6 +38,15 @@ dependencies = [
]
[[package]]
+name = "ansi_term"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
name = "async-channel"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -143,6 +152,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
[[package]]
+name = "byteorder"
+version = "1.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
+
+[[package]]
name = "bytes"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -173,6 +188,19 @@ 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 = "cloudabi"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -424,6 +452,8 @@ dependencies = [
"strum",
"swayipc-async",
"tokio",
+ "tracing",
+ "tracing-subscriber",
]
[[package]]
@@ -470,6 +500,15 @@ dependencies = [
]
[[package]]
+name = "matchers"
+version = "0.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1"
+dependencies = [
+ "regex-automata",
+]
+
+[[package]]
name = "memalloc"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -565,6 +604,25 @@ dependencies = [
]
[[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 = "num_cpus"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -718,6 +776,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
+name = "regex"
+version = "1.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
+dependencies = [
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4"
+dependencies = [
+ "byteorder",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
+
+[[package]]
name = "rustc-demangle"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1022,14 +1105,46 @@ dependencies = [
]
[[package]]
+name = "tracing-log"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5e0f8c7178e13481ff6765bd169b33e8d554c5d2bbede5e32c356194be02b9b9"
+dependencies = [
+ "lazy_static",
+ "log",
+ "tracing-core",
+]
+
+[[package]]
+name = "tracing-serde"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fb65ea441fbb84f9f6748fd496cf7f63ec9af5bca94dd86456978d055e8eb28b"
+dependencies = [
+ "serde",
+ "tracing-core",
+]
+
+[[package]]
name = "tracing-subscriber"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1fa8f0c8f4c594e4fc9debc1990deab13238077271ba84dd853d54902ee3401"
dependencies = [
+ "ansi_term",
+ "chrono",
+ "lazy_static",
+ "matchers",
+ "regex",
+ "serde",
+ "serde_json",
"sharded-slab",
+ "smallvec",
"thread_local",
+ "tracing",
"tracing-core",
+ "tracing-log",
+ "tracing-serde",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
@@ -14,6 +14,8 @@ futures-util = "0.3.7"
color-eyre = "0.5"
alsa = "0.4.3"
strum = { version = "0.20", features = ["derive"] }
+tracing = "0.1"
+tracing-subscriber = "0.2"
[profile.dev.package.backtrace]
opt-level = 3
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
@@ -5,11 +5,13 @@ use crate::sway::{
use color_eyre::Result;
use midir::{Ignore, MidiInput, MidiOutput, MidiOutputConnection};
use std::convert::{TryFrom, TryInto};
+use std::env;
use std::io::stdin;
use std::process::exit;
use strum::IntoEnumIterator;
use swayipc_async::Connection;
use tokio::runtime::Runtime;
+use tracing::*;
mod sway;
@@ -24,6 +26,10 @@ fn reset_colors(conn_out: &mut MidiOutputConnection) {
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
+ if let Err(_) = env::var("RUST_LOG") {
+ env::set_var("RUST_LOG", "INFO");
+ }
+ tracing_subscriber::fmt::init();
// establish a connection to sway over a unix socket
let mut connection = Connection::new().await?;
@@ -49,7 +55,7 @@ async fn main() -> Result<()> {
let midi_out_ports = midi_out.ports();
let out_port = midi_out_ports.get(1).unwrap();
- println!("Opening connections");
+ info!("Opening connections");
let mut conn_out = midi_out.connect(out_port, "midir-test").unwrap();
@@ -63,7 +69,7 @@ async fn main() -> Result<()> {
"midir-test",
move |_, message, _| {
if message[2] == 127 {
- println!("{:?}", message);
+ debug!("{:?}", message);
if let Ok(v) = LaunchpadWorkspaceMapping::try_from(message[1] as i32) {
rt.block_on(async {
let mut connection = Connection::new().await.unwrap();
@@ -81,16 +87,16 @@ async fn main() -> Result<()> {
)
.unwrap();
- println!("Connections open, enter `q` to exit ...");
+ info!("Connections open, enter `q` to exit ...");
loop {
input.clear();
stdin().read_line(&mut input)?;
if input.trim() == "q" {
- println!("Closing connections");
+ info!("Closing connections");
conn_in.close();
//conn_out.close();
- println!("Connections closed");
+ info!("Connections closed");
exit(0);
}
}
diff --git a/src/sway/mod.rs b/src/sway/mod.rs
@@ -6,10 +6,11 @@ use std::convert::{TryFrom, TryInto};
use std::fmt::{Display, Formatter};
use strum::EnumIter;
use swayipc_async::{Connection, Event, EventType};
+use tracing::*;
pub async fn get_workspaces(connection: &mut Connection) -> Result<()> {
let workspaces = connection.get_workspaces().await?;
- println!("{:#?}", workspaces);
+ debug!("{:#?}", workspaces);
for space in workspaces {
if space.focused {