slog-rs logging (#171)
* added global slog instance, changed all logging macro formats to include logger instance * adding configuration to logging, allowing for multiple log outputs * updates to test, changes to build docs * rustfmt * moving logging functions into util crate
This commit is contained in:
committed by
Ignotus Peverell
parent
b85006ebe5
commit
8e382a7593
+1
-1
@@ -12,8 +12,8 @@ grin_store = { path = "../store" }
|
||||
grin_util = { path = "../util" }
|
||||
secp256k1zkp = { git = "https://github.com/mimblewimble/rust-secp256k1-zkp" }
|
||||
hyper = "~0.10.6"
|
||||
slog = "^2.0.12"
|
||||
iron = "~0.5.1"
|
||||
log = "~0.3"
|
||||
router = "~0.5.1"
|
||||
serde = "~1.0.8"
|
||||
serde_derive = "~1.0.8"
|
||||
|
||||
+9
-14
@@ -26,14 +26,12 @@ use rest::Error;
|
||||
/// returns a JSON object. Handles request building, JSON deserialization and
|
||||
/// response code checking.
|
||||
pub fn get<'a, T>(url: &'a str) -> Result<T, Error>
|
||||
where
|
||||
for<'de> T: Deserialize<'de>,
|
||||
where for<'de> T: Deserialize<'de>
|
||||
{
|
||||
let client = hyper::Client::new();
|
||||
let res = check_error(client.get(url).send())?;
|
||||
serde_json::from_reader(res).map_err(|e| {
|
||||
Error::Internal(format!("Server returned invalid JSON: {}", e))
|
||||
})
|
||||
serde_json::from_reader(res)
|
||||
.map_err(|e| Error::Internal(format!("Server returned invalid JSON: {}", e)))
|
||||
}
|
||||
|
||||
/// Helper function to easily issue a HTTP POST request with the provided JSON
|
||||
@@ -41,18 +39,15 @@ where
|
||||
/// building, JSON serialization and deserialization, and response code
|
||||
/// checking.
|
||||
pub fn post<'a, IN, OUT>(url: &'a str, input: &IN) -> Result<OUT, Error>
|
||||
where
|
||||
IN: Serialize,
|
||||
for<'de> OUT: Deserialize<'de>,
|
||||
where IN: Serialize,
|
||||
for<'de> OUT: Deserialize<'de>
|
||||
{
|
||||
let in_json = serde_json::to_string(input).map_err(|e| {
|
||||
Error::Internal(format!("Could not serialize data to JSON: {}", e))
|
||||
})?;
|
||||
let in_json = serde_json::to_string(input)
|
||||
.map_err(|e| Error::Internal(format!("Could not serialize data to JSON: {}", e)))?;
|
||||
let client = hyper::Client::new();
|
||||
let res = check_error(client.post(url).body(&mut in_json.as_bytes()).send())?;
|
||||
serde_json::from_reader(res).map_err(|e| {
|
||||
Error::Internal(format!("Server returned invalid JSON: {}", e))
|
||||
})
|
||||
serde_json::from_reader(res)
|
||||
.map_err(|e| Error::Internal(format!("Server returned invalid JSON: {}", e)))
|
||||
}
|
||||
|
||||
// convert hyper error and check for non success response codes
|
||||
|
||||
@@ -24,6 +24,7 @@ use rest::*;
|
||||
use types::*;
|
||||
use secp::pedersen::Commitment;
|
||||
use util;
|
||||
use util::LOGGER;
|
||||
|
||||
/// ApiEndpoint implementation for the blockchain. Exposes the current chain
|
||||
/// state as a simple JSON object.
|
||||
@@ -69,7 +70,7 @@ impl ApiEndpoint for OutputApi {
|
||||
}
|
||||
|
||||
fn get(&self, id: String) -> ApiResult<Output> {
|
||||
debug!("GET output {}", id);
|
||||
debug!(LOGGER, "GET output {}", id);
|
||||
let c = util::from_hex(id.clone()).map_err(|_| {
|
||||
Error::Argument(format!("Not a valid commitment: {}", id))
|
||||
})?;
|
||||
@@ -130,6 +131,7 @@ where
|
||||
identifier: "?.?.?.?".to_string(),
|
||||
};
|
||||
info!(
|
||||
LOGGER,
|
||||
"Pushing transaction with {} inputs and {} outputs to pool.",
|
||||
tx.inputs.len(),
|
||||
tx.outputs.len()
|
||||
@@ -172,7 +174,7 @@ pub fn start_rest_apis<T>(
|
||||
apis.register_endpoint("/pool".to_string(), PoolApi { tx_pool: tx_pool });
|
||||
|
||||
apis.start(&addr[..]).unwrap_or_else(|e| {
|
||||
error!("Failed to start API HTTP server: {}.", e);
|
||||
error!(LOGGER, "Failed to start API HTTP server: {}.", e);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ extern crate secp256k1zkp as secp;
|
||||
|
||||
extern crate hyper;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate slog;
|
||||
extern crate iron;
|
||||
extern crate router;
|
||||
extern crate serde;
|
||||
|
||||
+25
-40
@@ -36,6 +36,7 @@ use serde::de::DeserializeOwned;
|
||||
use serde_json;
|
||||
|
||||
use store;
|
||||
use util::LOGGER;
|
||||
|
||||
/// Errors that can be returned by an ApiEndpoint implementation.
|
||||
#[derive(Debug)]
|
||||
@@ -171,13 +172,13 @@ impl<E> Handler for ApiWrapper<E>
|
||||
Method::Get => {
|
||||
let res = self.0.get(extract_param(req, "id")?)?;
|
||||
let res_json = serde_json::to_string(&res)
|
||||
.map_err(|e| IronError::new(e, status::InternalServerError))?;
|
||||
.map_err(|e| IronError::new(e, status::InternalServerError))?;
|
||||
Ok(Response::with((status::Ok, res_json)))
|
||||
}
|
||||
Method::Put => {
|
||||
let id = extract_param(req, "id")?;
|
||||
let t: E::T = serde_json::from_reader(req.body.by_ref())
|
||||
.map_err(|e| IronError::new(e, status::BadRequest))?;
|
||||
.map_err(|e| IronError::new(e, status::BadRequest))?;
|
||||
self.0.update(id, t)?;
|
||||
Ok(Response::with(status::NoContent))
|
||||
}
|
||||
@@ -188,7 +189,7 @@ impl<E> Handler for ApiWrapper<E>
|
||||
}
|
||||
Method::Post => {
|
||||
let t: E::T = serde_json::from_reader(req.body.by_ref())
|
||||
.map_err(|e| IronError::new(e, status::BadRequest))?;
|
||||
.map_err(|e| IronError::new(e, status::BadRequest))?;
|
||||
let id = self.0.create(t)?;
|
||||
Ok(Response::with((status::Created, id.to_string())))
|
||||
}
|
||||
@@ -203,8 +204,7 @@ struct OpWrapper<E> {
|
||||
}
|
||||
|
||||
impl<E> Handler for OpWrapper<E>
|
||||
where
|
||||
E: ApiEndpoint,
|
||||
where E: ApiEndpoint
|
||||
{
|
||||
fn handle(&self, req: &mut Request) -> IronResult<Response> {
|
||||
let t: E::OP_IN = serde_json::from_reader(req.body.by_ref()).map_err(|e| {
|
||||
@@ -219,7 +219,7 @@ where
|
||||
Ok(Response::with((status::Ok, res_json)))
|
||||
}
|
||||
Err(e) => {
|
||||
error!("API operation: {:?}", e);
|
||||
error!(LOGGER, "API operation: {:?}", e);
|
||||
Err(IronError::from(e))
|
||||
}
|
||||
}
|
||||
@@ -227,9 +227,8 @@ where
|
||||
}
|
||||
|
||||
fn extract_param<ID>(req: &mut Request, param: &'static str) -> IronResult<ID>
|
||||
where
|
||||
ID: ToString + FromStr,
|
||||
<ID as FromStr>::Err: Debug + Send + error::Error + 'static,
|
||||
where ID: ToString + FromStr,
|
||||
<ID as FromStr>::Err: Debug + Send + error::Error + 'static
|
||||
{
|
||||
|
||||
let id = req.extensions
|
||||
@@ -237,9 +236,8 @@ where
|
||||
.unwrap()
|
||||
.find(param)
|
||||
.unwrap_or("");
|
||||
id.parse::<ID>().map_err(
|
||||
|e| IronError::new(e, status::BadRequest),
|
||||
)
|
||||
id.parse::<ID>()
|
||||
.map_err(|e| IronError::new(e, status::BadRequest))
|
||||
}
|
||||
|
||||
/// HTTP server allowing the registration of ApiEndpoint implementations.
|
||||
@@ -279,9 +277,8 @@ impl ApiServer {
|
||||
/// Register a new API endpoint, providing a relative URL for the new
|
||||
/// endpoint.
|
||||
pub fn register_endpoint<E>(&mut self, subpath: String, endpoint: E)
|
||||
where
|
||||
E: ApiEndpoint,
|
||||
<<E as ApiEndpoint>::ID as FromStr>::Err: Debug + Send + error::Error,
|
||||
where E: ApiEndpoint,
|
||||
<<E as ApiEndpoint>::ID as FromStr>::Err: Debug + Send + error::Error
|
||||
{
|
||||
|
||||
assert_eq!(subpath.chars().nth(0).unwrap(), '/');
|
||||
@@ -299,13 +296,9 @@ impl ApiServer {
|
||||
endpoint: endpoint.clone(),
|
||||
};
|
||||
let full_path = format!("{}/{}", root.clone(), op_s.clone());
|
||||
self.router.route(
|
||||
op.to_method(),
|
||||
full_path.clone(),
|
||||
wrapper,
|
||||
route_name,
|
||||
);
|
||||
info!("route: POST {}", full_path);
|
||||
self.router
|
||||
.route(op.to_method(), full_path.clone(), wrapper, route_name);
|
||||
info!(LOGGER, "route: POST {}", full_path);
|
||||
} else {
|
||||
|
||||
// regular REST operations
|
||||
@@ -317,21 +310,18 @@ impl ApiServer {
|
||||
_ => panic!("unreachable"),
|
||||
};
|
||||
let wrapper = ApiWrapper(endpoint.clone());
|
||||
self.router.route(
|
||||
op.to_method(),
|
||||
full_path.clone(),
|
||||
wrapper,
|
||||
route_name,
|
||||
);
|
||||
info!("route: {} {}", op.to_method(), full_path);
|
||||
self.router
|
||||
.route(op.to_method(), full_path.clone(), wrapper, route_name);
|
||||
info!(LOGGER, "route: {} {}", op.to_method(), full_path);
|
||||
}
|
||||
}
|
||||
|
||||
// support for the HTTP Options method by differentiating what's on the
|
||||
// root resource vs the id resource
|
||||
let (root_opts, sub_opts) = endpoint.operations().iter().fold(
|
||||
(vec![], vec![]),
|
||||
|mut acc, op| {
|
||||
let (root_opts, sub_opts) = endpoint
|
||||
.operations()
|
||||
.iter()
|
||||
.fold((vec![], vec![]), |mut acc, op| {
|
||||
let m = op.to_method();
|
||||
if m == Method::Post {
|
||||
acc.0.push(m);
|
||||
@@ -339,23 +329,18 @@ impl ApiServer {
|
||||
acc.1.push(m);
|
||||
}
|
||||
acc
|
||||
},
|
||||
);
|
||||
});
|
||||
self.router.options(
|
||||
root.clone(),
|
||||
move |_: &mut Request| {
|
||||
Ok(Response::with(
|
||||
(status::Ok, Header(headers::Allow(root_opts.clone()))),
|
||||
))
|
||||
Ok(Response::with((status::Ok, Header(headers::Allow(root_opts.clone())))))
|
||||
},
|
||||
"option_".to_string() + route_postfix,
|
||||
);
|
||||
self.router.options(
|
||||
root.clone() + "/:id",
|
||||
move |_: &mut Request| {
|
||||
Ok(Response::with(
|
||||
(status::Ok, Header(headers::Allow(sub_opts.clone()))),
|
||||
))
|
||||
Ok(Response::with((status::Ok, Header(headers::Allow(sub_opts.clone())))))
|
||||
},
|
||||
"option_id_".to_string() + route_postfix,
|
||||
);
|
||||
|
||||
+1
-4
@@ -58,10 +58,7 @@ impl Output {
|
||||
pub fn from_output(output: &core::Output, block_header: &core::BlockHeader) -> Output {
|
||||
let (output_type, lock_height) = match output.features {
|
||||
x if x.contains(core::transaction::COINBASE_OUTPUT) => {
|
||||
(
|
||||
OutputType::Coinbase,
|
||||
block_header.height + global::coinbase_maturity(),
|
||||
)
|
||||
(OutputType::Coinbase, block_header.height + global::coinbase_maturity())
|
||||
}
|
||||
_ => (OutputType::Transaction, 0),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user