Skip to content

Commit

Permalink
Compactified and typed the log entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed May 26, 2024
1 parent ae87365 commit 7b4c915
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 100 deletions.
2 changes: 1 addition & 1 deletion copper_log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[lib]
name = "copper_log"
path = "src/macros.rs"
path = "src/lib.rs"
proc-macro = true

[[bin]]
Expand Down
19 changes: 6 additions & 13 deletions copper_log/src/macros.rs → copper_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ pub fn debug(input: TokenStream) -> TokenStream {
let prefix = quote! {
use copper_log_runtime::value::Value;
use copper_log_runtime::value::to_value;
use copper_log_runtime::CuLogEntry;
use copper_log_runtime::ANONYMOUS;
let msg = #msg;
let index = #index;
let mut params = Vec::<Value>::new();
let mut params_istring = Vec::<Value>::new();
let mut log_entry = CuLogEntry::new(#index);
};

let mut unnamed_params = vec![];
Expand All @@ -50,29 +49,23 @@ pub fn debug(input: TokenStream) -> TokenStream {

let unnamed_prints = unnamed_params.iter().map(|value| {
quote! {
let istring = ANONYMOUS;
let param = to_value(#value).expect("Failed to convert a parameter to a Value");
params_istring.push(istring);
params.push(param);
log_entry.add_param(ANONYMOUS, param);
}
});

let named_prints = named_params.iter().map(|(name, value)| {
let index = intern_string(quote!(#name).to_string().as_str())
.expect("Failed to insert log string.");
quote! {
let istring = to_value(#index).unwrap();
let param = to_value(#value).expect("Failed to convert a parameter to a Value");
params_istring.push(istring);
params.push(param);
log_entry.add_param(#index, param);
}
});
let postfix = quote! {
let vparams = Value::Seq(params);
// to do add conditional
println!("{} {}", msg, &vparams);
let packed_value = Value::Seq(vec![to_value(index).unwrap(), Value::Seq(params_istring), vparams]);
copper_log_runtime::log(packed_value);
println!("{} {}", msg, &log_entry);
copper_log_runtime::log(log_entry);
};

let expanded = quote! {
Expand Down
1 change: 1 addition & 0 deletions copper_log_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ copper-value = { path = "../copper_value" }
lazy_static = "1.4.0"
serde = { version = "1.0.202", features = ["derive"] }
bincode = { version = "2.0.0-rc.3", features = ["serde"] }
bincode_derive = "2.0.0-rc.3"
pretty-hex = "0.4.1"
ctor = "0.2.8"
ctrlc = { version = "3.4.4", features = ["termination"] }
Expand Down
50 changes: 41 additions & 9 deletions copper_log_runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
use bincode;
use bincode::enc::write::Writer;
pub use copper_value as value;
use bincode_derive::{Decode, Encode};
pub use copper_value as value; // Part of the API, do not remove.
use copper_value::Value;
use kanal::{bounded, Sender};
use lazy_static::lazy_static;
use pretty_hex::pretty_hex;
use std::fmt::Display;
use std::io::{stdout, Write};
use std::sync::{Arc, Mutex};
use std::thread;
use value::Value;

#[allow(dead_code)]
pub const ANONYMOUS: Value = Value::U32(0);
pub const ANONYMOUS: u32 = 0;

#[derive(Debug, Encode, Decode)]
pub struct CuLogEntry {
pub msg_index: u32,
pub paramname_indexes: Vec<u32>,
pub params: Vec<Value>,
}

impl Display for CuLogEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"CuLogEntry {{ msg_index: {}, paramname_indexes: {:?}, params: {:?} }}",
self.msg_index, self.paramname_indexes, self.params
)
}
}

impl CuLogEntry {
pub fn new(msg_index: u32) -> Self {
CuLogEntry {
msg_index,
paramname_indexes: Vec::new(),
params: Vec::new(),
}
}

pub fn add_param(&mut self, paramname_index: u32, param: Value) {
self.paramname_indexes.push(paramname_index);
self.params.push(param);
}
}

lazy_static! {
static ref QUEUE: Sender<Value> = initialize_queue();
static ref QUEUE: Sender<CuLogEntry> = initialize_queue();
}

/// The lifetime of this struct is the lifetime of the logger.
Expand All @@ -37,8 +69,8 @@ impl Drop for LoggerRuntime {
}
}

fn initialize_queue() -> Sender<Value> {
let (sender, receiver) = bounded::<Value>(100);
fn initialize_queue() -> Sender<CuLogEntry> {
let (sender, receiver) = bounded::<CuLogEntry>(100);
let config = bincode::config::standard();

let handle = thread::spawn(move || loop {
Expand All @@ -65,9 +97,9 @@ fn initialize_queue() -> Sender<Value> {

/// Function called from generated code to log data.
#[inline]
pub fn log(data: Value) {
pub fn log(entry: CuLogEntry) {
// If the queue is closed, we just drop the data.
if QUEUE.send(data).is_err() {
if QUEUE.send(entry).is_err() {
if !QUEUE.is_closed() {
eprintln!("Copper: Failed to send data to the logger, some data got dropped.");
if QUEUE.is_full() {
Expand Down
64 changes: 64 additions & 0 deletions copper_value/src/bdec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::Value;
use bincode::BorrowDecode;
use std::collections::BTreeMap;

use bincode::de::Decoder;
use bincode::de::{BorrowDecoder, Decode};
use bincode::error::DecodeError;

// TODO: Unharcode all those enum types values
impl Decode for Value {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
match u8::decode(decoder)? {
0 => Ok(Value::U8(u8::decode(decoder)?)),
1 => Ok(Value::U16(u16::decode(decoder)?)),
2 => Ok(Value::U32(u32::decode(decoder)?)),
3 => Ok(Value::U64(u64::decode(decoder)?)),
4 => Ok(Value::I8(i8::decode(decoder)?)),
5 => Ok(Value::I16(i16::decode(decoder)?)),
6 => Ok(Value::I32(i32::decode(decoder)?)),
7 => Ok(Value::I64(i64::decode(decoder)?)),
8 => Ok(Value::F32(f32::decode(decoder)?)),
9 => Ok(Value::F64(f64::decode(decoder)?)),
10 => Ok(Value::Bool(bool::decode(decoder)?)),
11 => Ok(Value::Char(char::decode(decoder)?)),
12 => Ok(Value::String(String::decode(decoder)?)),
13 => Ok(Value::Bytes(Vec::<u8>::decode(decoder)?)),
14 => Ok(Value::Unit),
15 => Ok(Value::Seq(Vec::<Value>::decode(decoder)?)),
16 => Ok(Value::Map(BTreeMap::<Value, Value>::decode(decoder)?)),
17 => Ok(Value::Option(Option::<Box<Value>>::decode(decoder)?)),
18 => Ok(Value::Newtype(Box::<Value>::decode(decoder)?)),
_ => Err(DecodeError::Other("Unknown Value variant")),
}
}
}

impl<'de> BorrowDecode<'de> for Value {
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
match u8::borrow_decode(decoder)? {
0 => Ok(Value::U8(u8::borrow_decode(decoder)?)),
1 => Ok(Value::U16(u16::borrow_decode(decoder)?)),
2 => Ok(Value::U32(u32::borrow_decode(decoder)?)),
3 => Ok(Value::U64(u64::borrow_decode(decoder)?)),
4 => Ok(Value::I8(i8::borrow_decode(decoder)?)),
5 => Ok(Value::I16(i16::borrow_decode(decoder)?)),
6 => Ok(Value::I32(i32::borrow_decode(decoder)?)),
7 => Ok(Value::I64(i64::borrow_decode(decoder)?)),
8 => Ok(Value::F32(f32::borrow_decode(decoder)?)),
9 => Ok(Value::F64(f64::borrow_decode(decoder)?)),
10 => Ok(Value::Bool(bool::borrow_decode(decoder)?)),
11 => Ok(Value::Char(char::borrow_decode(decoder)?)),
12 => Ok(Value::String(String::borrow_decode(decoder)?)),
13 => Ok(Value::Bytes(Vec::<u8>::borrow_decode(decoder)?)),
14 => Ok(Value::Unit),
15 => Ok(Value::Seq(Vec::<Value>::borrow_decode(decoder)?)),
16 => Ok(Value::Map(BTreeMap::<Value, Value>::borrow_decode(
decoder,
)?)),
17 => Ok(Value::Option(Option::<Box<Value>>::borrow_decode(decoder)?)),
18 => Ok(Value::Newtype(Box::<Value>::borrow_decode(decoder)?)),
_ => Err(DecodeError::Other("Unknown Value variant")),
}
}
}
Loading

0 comments on commit 7b4c915

Please sign in to comment.