Skip to content

Commit fca0cc1

Browse files
authored
add some logging (#11)
1 parent f20d7d6 commit fca0cc1

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

Cargo.lock

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ serde_json = { version = "1.0.127", features = [ "alloc" ], default-features = f
3131
scale-bits = { version = "0.4.0", default-features = false }
3232
scale-value = { version = "0.16.2", default-features = false }
3333
pythonize = "0.23.0"
34+
log = { version = "0.4.25", default-features = false }
35+
pyo3-log = { version = "0.12.1", default-features = false }

src/lib.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use codec::{Decode, Encode};
22
use custom_derive::pydecode;
33
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
4+
use log;
45

56
use pyo3::prelude::*;
67

@@ -599,6 +600,8 @@ mod bt_decode {
599600
type_id: u32,
600601
portable_registry: &PyPortableRegistry,
601602
) -> PyResult<Value<u32>> {
603+
log::debug!(target: "btdecode", "encoding a list-like type {:?}", py_list);
604+
log::debug!(target: "btdecode", "type_id: {:?}", type_id);
602605
match &ty.type_def {
603606
scale_info::TypeDef::Array(inner) => {
604607
let ty_param = inner.type_param;
@@ -607,6 +610,9 @@ mod bt_decode {
607610
.registry
608611
.resolve(ty_param_id)
609612
.expect(&format!("Failed to resolve type (1): {:?}", ty_param));
613+
log::debug!(target: "btdecode", "ty_param: {:?}", ty_param);
614+
log::debug!(target: "btdecode", "ty_param_id: {:?}", ty_param_id);
615+
log::debug!(target: "btdecode", "ty_: {:?}", ty_);
610616

611617
let items = py_list
612618
.iter()
@@ -723,6 +729,9 @@ mod bt_decode {
723729
type_id: u32,
724730
portable_registry: &PyPortableRegistry,
725731
) -> PyResult<Value<u32>> {
732+
log::debug!(target: "btdecode", "encoding a non-option type {:?} {:?}", ty, to_encode);
733+
log::debug!(target: "btdecode", "type_id: {:?}", type_id);
734+
726735
if to_encode.is_none(py) {
727736
// If none and NOT option,
728737
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
@@ -732,6 +741,7 @@ mod bt_decode {
732741
}
733742

734743
if py_isinstance(py, to_encode, "bool")? {
744+
log::debug!(target: "btdecode", "encoding to bool");
735745
let value = to_encode.extract::<bool>(py)?;
736746

737747
match ty.type_def {
@@ -748,6 +758,7 @@ mod bt_decode {
748758
}
749759
}
750760
} else if py_isinstance(py, to_encode, "str")? {
761+
log::debug!(target: "btdecode", "encoding to str");
751762
if to_encode.extract::<char>(py).is_ok()
752763
&& matches!(
753764
ty.type_def,
@@ -784,12 +795,14 @@ mod bt_decode {
784795
} else if py_isinstance(py, to_encode, "int")?
785796
&& matches!(&ty.type_def, scale_info::TypeDef::Primitive(_))
786797
{
798+
log::debug!(target: "btdecode", "encoding as primitive int");
787799
let as_py_int = to_encode.downcast_bound::<PyInt>(py)?.as_unbound();
788800

789801
return int_type_def_to_value(py, as_py_int, ty, type_id);
790802
} else if py_isinstance(py, to_encode, "int")?
791803
&& matches!(&ty.type_def, scale_info::TypeDef::Compact(_))
792804
{
805+
log::debug!(target: "btdecode", "encoding as compact int");
793806
// Must be a Compact int
794807
let as_py_int = to_encode.downcast_bound::<PyInt>(py)?.as_unbound();
795808

@@ -812,6 +825,7 @@ mod bt_decode {
812825
to_encode
813826
)));
814827
} else if py_isinstance(py, to_encode, "tuple")? {
828+
log::debug!(target: "btdecode", "encoding as tuple");
815829
let tuple_value = to_encode.downcast_bound::<PyTuple>(py)?;
816830
let as_list = tuple_value.to_list();
817831

@@ -822,6 +836,7 @@ mod bt_decode {
822836
))
823837
})
824838
} else if py_isinstance(py, to_encode, "list")? {
839+
log::debug!(target: "btdecode", "encoding as list");
825840
let as_list = to_encode.downcast_bound::<PyList>(py)?;
826841

827842
pylist_to_value(py, &as_list, ty, type_id, portable_registry).map_err(|_e| {
@@ -831,6 +846,7 @@ mod bt_decode {
831846
))
832847
})
833848
} else if py_isinstance(py, to_encode, "dict")? {
849+
log::debug!(target: "btdecode", "encoding as dict");
834850
let py_dict = to_encode.downcast_bound::<PyDict>(py)?;
835851

836852
match &ty.type_def {
@@ -894,6 +910,7 @@ mod bt_decode {
894910

895911
//} else if let Ok(value) = to_encode.downcast_bound::<PyBytes>(py) {
896912
} else if py_has_dict_method(py, to_encode)? {
913+
log::debug!(target: "btdecode", "encoding object as dict");
897914
// Convert object to dict
898915
let py_dict = py_to_dict(py, to_encode)?;
899916

@@ -956,9 +973,10 @@ mod bt_decode {
956973
}
957974
} else {
958975
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
959-
"Invalid type for data: {} of type {}",
976+
"Invalid type for data: {} of type {}, type_def: {:?}",
960977
to_encode,
961-
to_encode.getattr(py, "__class__").unwrap_or(py.None())
978+
to_encode.getattr(py, "__class__").unwrap_or(py.None()),
979+
ty.type_def
962980
)));
963981
}
964982
}
@@ -1058,6 +1076,9 @@ mod bt_decode {
10581076
portable_registry: &PyPortableRegistry,
10591077
to_encode: Py<PyAny>,
10601078
) -> PyResult<Vec<u8>> {
1079+
// Initialize logging
1080+
pyo3_log::try_init();
1081+
10611082
// Create a memoization table for the type string to type id conversion
10621083
let mut memo = HashMap::<String, u32>::new();
10631084

0 commit comments

Comments
 (0)