Skip to content

Commit

Permalink
Add logging verbosity options
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Sep 20, 2023
1 parent b7de7ff commit bf4ae72
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
1 change: 1 addition & 0 deletions base_layer/chat_ffi/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct ApplicationConfig *create_chat_config(const char *network_str,
const char *identity_file_path,
struct TransportConfig *tor_transport_config,
const char *log_path,
int log_verbosity,
int *error_out);

/**
Expand Down
15 changes: 11 additions & 4 deletions base_layer/chat_ffi/src/application_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{ffi::CStr, path::PathBuf, ptr, str::FromStr};
use std::{convert::TryFrom, ffi::CStr, path::PathBuf, ptr, str::FromStr};

use libc::{c_char, c_int};
use tari_chat_client::{
config::{ApplicationConfig, ChatClientConfig},
networking::Multiaddr,
};
use tari_common::configuration::{MultiaddrList, Network};
use tari_p2p::{PeerSeedsConfig, TransportConfig};
use tari_common::configuration::{MultiaddrList, Network, StringList};
use tari_p2p::{PeerSeedsConfig, TransportConfig, DEFAULT_DNS_NAME_SERVER};

use crate::error::{InterfaceError, LibChatError};

Expand All @@ -53,6 +53,7 @@ pub unsafe extern "C" fn create_chat_config(
identity_file_path: *const c_char,
tor_transport_config: *mut TransportConfig,
log_path: *const c_char,
log_verbosity: c_int,
error_out: *mut c_int,
) -> *mut ApplicationConfig {
let mut error = 0;
Expand Down Expand Up @@ -152,6 +153,8 @@ pub unsafe extern "C" fn create_chat_config(
}
let log_path = PathBuf::from(log_path_string);

let log_verbosity = u8::try_from(log_verbosity).unwrap_or(2); // 2 == WARN

let mut bad_identity = |e| {
error = LibChatError::from(InterfaceError::InvalidArgument(e)).code;
ptr::swap(error_out, &mut error as *mut c_int);
Expand All @@ -170,13 +173,16 @@ pub unsafe extern "C" fn create_chat_config(
chat_client_config.p2p.transport = (*tor_transport_config).clone();
chat_client_config.p2p.public_addresses = MultiaddrList::from(vec![address]);
chat_client_config.log_path = Some(log_path);
chat_client_config.log_verbosity = Some(log_verbosity);
chat_client_config.identity_file = identity_path;
chat_client_config.set_base_path(datastore_path);

let config = ApplicationConfig {
chat_client: chat_client_config,
peer_seeds: PeerSeedsConfig {
dns_seeds_use_dnssec: true,
dns_seeds_use_dnssec: false,
dns_seeds_name_server: DEFAULT_DNS_NAME_SERVER.parse().unwrap(),
dns_seeds: StringList::from(vec![format!("seeds.{}.tari.com", network.as_key_str())]),
..PeerSeedsConfig::default()
},
..ApplicationConfig::default()
Expand Down Expand Up @@ -246,6 +252,7 @@ mod test {
identity_path.as_ptr(),
transport_config,
log_path.as_ptr(),
5,
error_ptr,
);

Expand Down
2 changes: 1 addition & 1 deletion base_layer/chat_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub unsafe extern "C" fn create_chat_client(
}

if let Some(log_path) = (*config).clone().chat_client.log_path {
init_logging(log_path, error_out);
init_logging(log_path, (*config).clone().chat_client.log_verbosity, error_out);

if error > 0 {
return ptr::null_mut();
Expand Down
36 changes: 23 additions & 13 deletions base_layer/chat_ffi/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,23 @@ const LOG_TARGET: &str = "chat_ffi::logging";
/// # Safety
/// None
#[allow(clippy::too_many_lines)]
pub unsafe fn init_logging(log_path: PathBuf, error_out: *mut c_int) {
pub unsafe fn init_logging(log_path: PathBuf, verbosity: Option<u8>, error_out: *mut c_int) {
let mut error = 0;
ptr::swap(error_out, &mut error as *mut c_int);

let num_rolling_log_files = 2;
let size_per_log_file_bytes: u64 = 10 * 1024 * 1024;

let log_level = match verbosity {
Some(0) => LevelFilter::Off,
Some(1) => LevelFilter::Error,
Some(2) => LevelFilter::Warn,
Some(3) => LevelFilter::Info,
Some(4) => LevelFilter::Debug,
Some(5) | Some(11) => LevelFilter::Trace, // Cranked up to 11
_ => LevelFilter::Warn,
};

let path = log_path.to_str().expect("Convert path to string");
let encoder = PatternEncoder::new("{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] {l:5} {m}{n}");

Expand Down Expand Up @@ -95,69 +105,69 @@ pub unsafe fn init_logging(log_path: PathBuf, error_out: *mut c_int) {
Logger::builder()
.appender("logfile")
.additive(false)
.build("comms", LevelFilter::Warn),
.build("comms", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("comms::noise", LevelFilter::Warn),
.build("comms::noise", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("tokio_util", LevelFilter::Warn),
.build("tokio_util", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("tracing", LevelFilter::Warn),
.build("tracing", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("chat_ffi::callback_handler", LevelFilter::Warn),
.build("chat_ffi::callback_handler", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("chat_ffi", LevelFilter::Warn),
.build("chat_ffi", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("contacts", LevelFilter::Warn),
.build("contacts", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("p2p", LevelFilter::Warn),
.build("p2p", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("yamux", LevelFilter::Warn),
.build("yamux", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("dht", LevelFilter::Warn),
.build("dht", log_level),
)
.logger(
Logger::builder()
.appender("logfile")
.additive(false)
.build("mio", LevelFilter::Warn),
.build("mio", log_level),
)
.build(Root::builder().appender("logfile").build(LevelFilter::Warn))
.build(Root::builder().appender("logfile").build(log_level))
.expect("Should be able to create a Config");

match log4rs::init_config(lconfig) {
Expand Down
4 changes: 4 additions & 0 deletions base_layer/contacts/examples/chat_client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub struct ChatClientConfig {
pub metadata_auto_ping_interval: Duration,
/// The location of the log path
pub log_path: Option<PathBuf>,
/// The log verbosity
pub log_verbosity: Option<u8>,
}

impl Default for ChatClientConfig {
Expand Down Expand Up @@ -120,6 +122,7 @@ impl Default for ChatClientConfig {
force_sync_peers: StringList::default(),
metadata_auto_ping_interval: Duration::from_secs(30),
log_path: None,
log_verbosity: Some(2), // Warn
}
}
}
Expand Down Expand Up @@ -160,6 +163,7 @@ impl ChatClientConfig {
pub fn default_local_test() -> Self {
Self {
network: Network::LocalNet,
log_verbosity: Some(5), // Trace
p2p: P2pConfig {
datastore_path: PathBuf::from("peer_db/chat_client"),
user_agent: format!("tari/chat_client/{}", consts::APP_VERSION_NUMBER),
Expand Down

0 comments on commit bf4ae72

Please sign in to comment.