Browse Source

Add logging library and initialize by default

no-contract-setup-message
Philipp Hoenisch 3 years ago
parent
commit
382e7095e8
No known key found for this signature in database GPG Key ID: E5F8E74C672BC666
  1. 124
      Cargo.lock
  2. 3
      daemon/Cargo.toml
  3. 13
      daemon/README.md
  4. 33
      daemon/src/logger.rs
  5. 10
      daemon/src/maker.rs
  6. 10
      daemon/src/taker.rs

124
Cargo.lock

@ -22,6 +22,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "anyhow"
version = "1.0.44"
@ -269,6 +278,18 @@ 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",
"winapi 0.3.9",
]
[[package]]
name = "clap"
version = "3.0.0-beta.4"
@ -411,6 +432,7 @@ name = "daemon"
version = "0.1.0"
dependencies = [
"anyhow",
"atty",
"bdk",
"cfd_protocol",
"clap",
@ -430,6 +452,8 @@ dependencies = [
"thiserror",
"tokio",
"tokio-util",
"tracing",
"tracing-subscriber",
"uuid",
]
@ -1029,6 +1053,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]]
name = "matchers"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1"
dependencies = [
"regex-automata",
]
[[package]]
name = "matches"
version = "0.1.9"
@ -1133,6 +1166,16 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg 1.0.1",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
@ -1551,6 +1594,15 @@ dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
@ -1870,6 +1922,15 @@ dependencies = [
"opaque-debug",
]
[[package]]
name = "sharded-slab"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "740223c51853f3145fe7c90360d2d4232f2b62e3449489c207eccde818979982"
dependencies = [
"lazy_static",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.0"
@ -2146,6 +2207,15 @@ dependencies = [
"syn",
]
[[package]]
name = "thread_local"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
dependencies = [
"once_cell",
]
[[package]]
name = "time"
version = "0.3.2"
@ -2267,9 +2337,21 @@ checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98863d0dd09fa59a1b79c6750ad80dbda6b75f4e71c437a6a1a8cb91a8bcbd77"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.19"
@ -2279,6 +2361,48 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "tracing-log"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6923477a48e41c1951f1999ef8bb5a3023eb723ceadafe78ffb65dc366761e3"
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.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9cbe87a2fa7e35900ce5de20220a582a9483a7063811defce79d7cbd59d4cfe"
dependencies = [
"ansi_term",
"chrono",
"lazy_static",
"matchers",
"regex",
"serde",
"serde_json",
"sharded-slab",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
"tracing-serde",
]
[[package]]
name = "try-lock"
version = "0.2.3"

3
daemon/Cargo.toml

@ -5,6 +5,7 @@ edition = "2018"
[dependencies]
anyhow = "1"
atty = "0.2"
bdk = { git = "https://github.com/bitcoindevkit/bdk/" }
cfd_protocol = { path = "../cfd_protocol" }
clap = "3.0.0-beta.4"
@ -23,6 +24,8 @@ sqlx = { version = "0.5", features = ["offline"] }
thiserror = "1"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "net"] }
tokio-util = { version = "0.6", features = ["codec"] }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.2", default-features = false, features = ["fmt", "ansi", "env-filter", "chrono", "tracing-log", "json"] }
uuid = { version = "0.8", features = ["serde", "v4"] }
[[bin]]

13
daemon/README.md

@ -25,3 +25,16 @@ Currently the database for taker and maker is the same.
The `taker` binary is used as an example to run the `prepare` command above, but it is irrelevant if you run it for taker or maker.
The `tempdb` created can be deleted, it should not be checked into the repo.
You can keep it around and just run the `prepare` statement multiple times when working on the database.
## Logging
`maker` and `taker` both make use of a log library which can be controlled using the environment variable `RUST_LOG`.
By default, logging is set to `debug` for application specific logs only.
If you want to see logs by third party libraries, you need to set `RUST_LOG=`.
For example, to enable logs for rocket, use:
```bash
RUST_LOG=rocket=debug cargo run --bin taker
```
Additionally, logs can be formatted in json format by passing `--json` or `-j`.

33
daemon/src/logger.rs

@ -0,0 +1,33 @@
use anyhow::Result;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::fmt::time::ChronoLocal;
use tracing_subscriber::{EnvFilter, FmtSubscriber};
pub fn init(level: LevelFilter, json_format: bool) -> Result<()> {
if level == LevelFilter::OFF {
return Ok(());
}
let is_terminal = atty::is(atty::Stream::Stderr);
let filter = EnvFilter::from_default_env()
.add_directive(format!("taker={}", level).parse()?)
.add_directive(format!("maker={}", level).parse()?);
let builder = FmtSubscriber::builder()
.with_env_filter(filter)
.with_writer(std::io::stderr)
.with_ansi(is_terminal)
.with_timer(ChronoLocal::with_format("%F %T".to_owned()))
.with_target(false);
if json_format {
builder.json().init();
} else {
builder.init();
}
tracing::info!("Initialized logger");
Ok(())
}

10
daemon/src/maker.rs

@ -1,7 +1,7 @@
use crate::maker_cfd_actor::Command;
use crate::seed::Seed;
use crate::wallet::Wallet;
use anyhow::Result;
use anyhow::{Context, Result};
use bdk::bitcoin::secp256k1::{schnorrsig, SECP256K1};
use bdk::bitcoin::Network;
use clap::Clap;
@ -12,9 +12,11 @@ use rocket_db_pools::Database;
use std::path::PathBuf;
use std::time::Duration;
use tokio::sync::{mpsc, watch};
use tracing_subscriber::filter::LevelFilter;
mod db;
mod keypair;
mod logger;
mod maker_cfd_actor;
mod maker_inc_connections_actor;
mod model;
@ -51,12 +53,18 @@ struct Opts {
/// Generate a seed file within the data directory.
#[clap(long)]
generate_seed: bool,
/// If enabled logs will be in json format
#[clap(short, long)]
json: bool,
}
#[rocket::main]
async fn main() -> Result<()> {
let opts = Opts::parse();
logger::init(LevelFilter::DEBUG, opts.json).context("initialize logger")?;
let data_dir = opts
.data_dir
.unwrap_or_else(|| std::env::current_dir().expect("unable to get cwd"));

10
daemon/src/taker.rs

@ -1,7 +1,7 @@
use crate::model::WalletInfo;
use crate::taker_cfd_actor::Command;
use crate::wallet::Wallet;
use anyhow::Result;
use anyhow::{Context, Result};
use bdk::bitcoin::secp256k1::{schnorrsig, SECP256K1};
use bdk::bitcoin::Network;
use clap::Clap;
@ -14,9 +14,11 @@ use std::path::PathBuf;
use std::thread::sleep;
use std::time::Duration;
use tokio::sync::watch;
use tracing_subscriber::filter::LevelFilter;
mod db;
mod keypair;
mod logger;
mod model;
mod routes_taker;
mod seed;
@ -55,12 +57,18 @@ struct Opts {
/// Generate a seed file within the data directory.
#[clap(long)]
generate_seed: bool,
/// If enabled logs will be in json format
#[clap(short, long)]
json: bool,
}
#[rocket::main]
async fn main() -> Result<()> {
let opts = Opts::parse();
logger::init(LevelFilter::DEBUG, opts.json).context("initialize logger")?;
let data_dir = opts
.data_dir
.unwrap_or_else(|| std::env::current_dir().expect("unable to get cwd"));

Loading…
Cancel
Save