Skip to content

Commit

Permalink
Basic named or anonymous params gets constructed
Browse files Browse the repository at this point in the history
this is for the log frontend
  • Loading branch information
gbin committed May 25, 2024
1 parent 600a759 commit 266fb39
Show file tree
Hide file tree
Showing 15 changed files with 1,586 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["copper", "copper_log_test", "copper_derive", "copper_derive_test", "copper_log", "examples/config_gen", "examples/pluginload", "examples/simplelogger", "examples/v4lsrc"]
members = ["copper", "copper_log_test", "copper_derive", "copper_derive_test", "copper_log", "examples/config_gen", "examples/pluginload", "examples/simplelogger", "examples/v4lsrc", "copper_log_runtime"]
resolver = "2"
44 changes: 24 additions & 20 deletions copper_log/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,53 @@ lazy_static! {
))
};
}

pub fn check_and_insert(filename: &str, line_number: u32, log_string: &str) -> Option<IndexType> {
let (counter_store, index_to_string, string_to_index, index_to_callsite) =
&mut *DBS.lock().unwrap();
pub fn intern_string(s: &str) -> Option<IndexType> {
let (counter_store, index_to_string, string_to_index, _) = &mut *DBS.lock().unwrap();
let index = {
let env = RKV.lock().unwrap();
// If this string already exists in the store, return the index
{
let reader = env.read().unwrap();
let entry = string_to_index.get(&reader, log_string);
// check if log_string is already in the string_to_index store
if let Ok(Some(Value::U64(index))) = string_to_index.get(&reader, log_string) {
println!(
"CLog: Returning existing index #{} -> {}",
index, log_string
);
if let Ok(Some(Value::U64(index))) = string_to_index.get(&reader, s) {
println!("CLog: Returning existing index #{} -> {}", index, s);
return Some(index as IndexType);
};
}
let mut writer = env.write().unwrap();
let next_index = get_next_index(&mut writer, counter_store).unwrap();
// Insert the new string into the store
index_to_string
.put(
&mut writer,
next_index.to_le_bytes(),
&Value::Str(log_string),
)
.put(&mut writer, next_index.to_le_bytes(), &Value::Str(s))
.unwrap();
string_to_index
.put(&mut writer, log_string, &Value::U64(next_index as u64))
.put(&mut writer, s, &Value::U64(next_index as u64))
.unwrap();
writer.commit().unwrap();
Some(next_index)
};
println!("CLog: Inserted #{} -> {}", index.unwrap(), s);
index
}

pub fn check_and_insert(filename: &str, line_number: u32, log_string: &str) -> Option<IndexType> {
let index = intern_string(log_string);
{
let (_, _, _, index_to_callsite) = &mut *DBS.lock().unwrap();
index?;
let lindex = index.unwrap();
let env = RKV.lock().unwrap();
let mut writer = env.write().unwrap();
index_to_callsite
.put(
&mut writer,
next_index.to_le_bytes(),
lindex.to_le_bytes(),
&Value::Str(format!("{}:{}", filename, line_number).as_str()),
)
.unwrap();
writer.commit().unwrap();
Some(next_index)
};
println!("CLog: Inserted #{} -> {}", index.unwrap(), log_string);
println!("CLog: Inserted #{} -> {}", lindex, log_string);
}
index
}

Expand Down
40 changes: 31 additions & 9 deletions copper_log/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ mod index;

extern crate proc_macro;

use crate::index::check_and_insert;
use crate::index::{check_and_insert, intern_string};
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenTree};
use quote::quote;
use syn::parse::Parser;
use syn::Token;
use syn::{custom_keyword, parse_macro_input, Expr, ExprAssign, ExprLit, Lit, LitStr};
use syn::{Expr, ExprAssign, ExprLit, Lit};

#[proc_macro]
pub fn debug(input: TokenStream) -> TokenStream {
Expand All @@ -28,7 +27,16 @@ pub fn debug(input: TokenStream) -> TokenStream {
} else {
panic!("The first parameter of the argument needs to be a string literal.");
};
println!("{} -> [{}]", index, msg);
let prefix = quote! {
use copper_log_runtime::value::Value;
use copper_log_runtime::value::to_value;
use copper_log_runtime::ANONYMOUS;
let msg = #msg;
let index = #index;
println!("{} -> [{}]", index, msg);
let mut params = Vec::<(Value, Value)>::new();

};

let mut unnamed_params = vec![];
let mut named_params = vec![];
Expand All @@ -43,20 +51,34 @@ pub fn debug(input: TokenStream) -> TokenStream {

let unnamed_prints = unnamed_params.iter().map(|param| {
quote! {
println!("{:?}", #param);
// 0 means an unnamed parameter
let param = (ANONYMOUS, to_value(#param).expect("Failed to convert a parameter to a Value"));
println!("anon param: {:?}", &param);
params.push(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! {
println!("{} = {:?}", stringify!(#name), #value);
let param = (to_value(#index).unwrap(), to_value(#value).expect("Failed to convert a parameter to a Value"));
println!("named param: {:?}", &param);
params.push(param);
}
});

let postfix = quote! {
println!("Would send {:?}", (index, params));
};

let expanded = quote! {
println!("Message: {}", #msg);
#(#unnamed_prints)*
#(#named_prints)*
{
#prefix
#(#unnamed_prints)*
#(#named_prints)*
#postfix
}
};

expanded.into()
Expand Down
7 changes: 7 additions & 0 deletions copper_log_runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "copper-log-runtime"
version = "0.1.0"
edition = "2021"

[dependencies]
copper-value = { path = "../copper_value" }
3 changes: 3 additions & 0 deletions copper_log_runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub use copper_value as value;
#[allow(dead_code)]
pub const ANONYMOUS: value::Value = value::Value::U32(0);
7 changes: 7 additions & 0 deletions copper_log_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ edition = "2021"

[dependencies]
copper-log = { path = "../copper_log" }
copper-log-runtime = { path = "../copper_log_runtime" }
copper-value = { path = "../copper_value" }
serde = { version = "1.0.202", features = ["derive"] }

[build-dependencies]
copper-log = { path = "../copper_log" }

27 changes: 22 additions & 5 deletions copper_log_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
use copper_log::debug;
use copper_value::to_value;
use serde::Serialize;

fn main() {
let a = 2;
let b = 3;
// debug!("Hello, world! {} {}", a, b);
debug!("Hello, world!");
debug!("Hello, world2!", a);
#[derive(Serialize)]
struct Test {
a: i32,
b: i32,
}
let t = Test { a: 2, b: 3 };

let v = to_value(t).unwrap();

println!("{:?}", v);

let mytuple = (1, "toto", 3.34f64, true, 'a');
let v = to_value(mytuple).unwrap();
println!("{:?}", v);
debug!("Just a string");
debug!("anonymous param constants {} {}", 3u16, 2u8);
debug!("named param constants {} {}", a = 3, b = 2);
debug!("mixed named param constants, {} {} {}", a = 3, 54, b = 2);
debug!("a tuple {}", mytuple);
}
5 changes: 5 additions & 0 deletions copper_value/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target/
/Cargo.lock
/.gitattributes
/.idea/
*.iml
37 changes: 37 additions & 0 deletions copper_value/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
language: rust
rust:
- nightly
- beta
- stable
sudo: false
os: linux
cache:
directories:
- $HOME/.cargo
- target
env:
global:
- secure: "RFoJK/j05ZzCrx3zJJjqn8FqL2UwAH0iSTE/pTNsEn4Xl1DFTC/ubb+cu8gFS8wpTY0+zPSmBrHp3R0MNKIpbQqZ2WexEpbzNEdpftcW+eUlEj45HvNWw3LdKn8w0O7txdRxbUVzgDgto+XGsFypMsA5wxjOryYDJWPnpXvt1QL520ro0/IzYToubBj0e6hhlXXvDY0gr4ip/Y/dooWJ0XmPdmHi1atHLBYaH5l0qd5Rc6pfqvj+ctaZQyVV6JEK84ODUeK6IHbthl45/ZwecMZW/egAXX2CCViv2FmCkqoIjGynPd5dVTMMZGYVEYpDMzdkvYze6JoNsj2YZkO3WprpTqUTgwf+7XdRIce0X7rIVm6P5fBiN/8NPcRVgfBR6WthJoRfQYdjkqhrg70wcBrJxRUulPxNxwcCkC9RDEbI8ZlG4ZhNKfAVzV0xMwztcJoz5qqsMSAkWc8HIAZ7CL9zplLe6P7tomaLcWFxkyvGsdOXCHtyBnp8TOwgvG4kGzuZ7myEOrGodWn23F9e5h00Kio9k/j1HDa7+ESQ7UW6MJu4YT+5XTTJi8Dr+kdrUVDwFirab8+A8/tfFmQxZhZdzny48uTjGeDFqmr4hQ+hC3elg+ZDpEl72/A8HMQcpxtpiBkyky0B5sJSCUOeCU8A5DN+UJN/9d6my+10flA="
- secure: "GNs+Daj2ses715lQWto6a+zFWjBBZ/4Rt/F6j0rpqYjm7NEForoRdDmRWHbN3xGSrxT83G7yuPdGRlAKG1AjM91Dxi2fEw/THvT61QXB5LSW21/2h7WETAJYoBzlFwAPo3RVM0tzkjPQfAm5C3zxbPXjo42D5GQkF50vKixBQZ5QG2FIQHAv80Ed9y2aozf6Z5V6lojjLm2xEfdSNA8Mj8/N3ux2eWESddufk6wdEoRkcpRe2e09TZaeamYQ48yj1paDPKNLFhyRFehtOFGdMQQgbWJ1GsYgtWkHu8f7ohuC4Pdx94f0xTApz+jpeADtDmgX8Cvd2IVCUEB8WLMJv2W+Zp2xrn1Ufr88uCzOUT78c6kiRJfQ9n9IEZSTuie/JtwgjlWuud1raarDDfmRKAGWHRx0WL1TKt4ADuy63pyV78FlLNnMTJ9BwZhUpxQqIbKtw31eIwWxkUiwpvEQ1IYLts4U9rSi8xjnLhA7B97ahIq1pMOWnC4HM2jCggCw2AQtkNNM6vuPli//3i1aF02JrLnlwsXparxnQ70HmyaxX0u38Tgr8zbobtJE8+TsDh2d3uFcI6BNARmsO7I1ifDbPj6aB2JOYK4M8knoP3MG28jO5v3qVXrEmfWm361LMNkmw1cM2MnDBq3uxKWhGZzW4XB72kcSzYgFNzUaaUw="
before_script:
- curl -L https://github.com/arcnmx/ci/archive/master.tar.gz | tar -xzC $HOME && . $HOME/ci-master/src

script:
- cargo build
- cargo test

deploy:
provider: script
script: "true"
on:
tags: true
all_branches: true
condition: "$TRAVIS_RUST_VERSION = stable"

before_deploy:
- cargo doc
- cargo package

after_deploy:
- cargo pages-publish
- cargo publish
19 changes: 19 additions & 0 deletions copper_value/COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 arcnmx

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
17 changes: 17 additions & 0 deletions copper_value/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "copper-value"
version = "0.7.0"
authors = ["arcnmx", "gbin"]
edition = "2021"

description = "Serialization value trees"
readme = "README.md"
keywords = ["serde"]
license = "MIT"

[dependencies]
serde = "1.0.202"
ordered-float = "4.2.0"

[dev-dependencies]
serde_derive = "1.0.202"
9 changes: 9 additions & 0 deletions copper_value/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# copper-value

[![license-badge][]][license]

`copper-value` provides a way to capture serialization value trees for later processing.
Customizations are made to enable a more compact representation for the structured logging of copper.

[license-badge]: https://img.shields.io/badge/license-MIT-lightgray.svg?style=flat-square
[license]: https://github.com/arcnmx/serde-value/blob/master/COPYING
Loading

0 comments on commit 266fb39

Please sign in to comment.