Skip to content

Commit

Permalink
Merge pull request iden3#104 from phated/phated/fix-log-printing
Browse files Browse the repository at this point in the history
fix: Log messages from wasm with better formatting
  • Loading branch information
alrubio authored Aug 25, 2022
2 parents 696e180 + a8e4c9d commit ae87f6c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
25 changes: 21 additions & 4 deletions code_producers/src/wasm_elements/common/witness_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = async function builder(code, options) {
let wc;

let errStr = "";
let msgStr = "";

const instance = await WebAssembly.instantiate(wasmModule, {
runtime: {
Expand Down Expand Up @@ -41,7 +42,19 @@ module.exports = async function builder(code, options) {
// console.error(getMessage());
},
writeBufferMessage : function() {
process.stdout.write(getMessage());
const msg = getMessage();
// Any calls to `log()` will always end with a `\n`, so that's when we print and reset
if (msg === "\n") {
console.log(msgStr);
msgStr = "";
} else {
// If we've buffered other content, put a space in between the items
if (msgStr !== "") {
msgStr += " "
}
// Then append the message to the message we are creating
msgStr += msg;
}
},
showSharedRWMemory : function() {
printSharedRWMemory ();
Expand Down Expand Up @@ -82,9 +95,13 @@ module.exports = async function builder(code, options) {
arr[shared_rw_memory_size-1-j] = instance.exports.readSharedRWMemory(j);
}

process.stdout.write(fromArray32(arr).toString());
//console.log(fromArray32(arr));
}
// If we've buffered other content, put a space in between the items
if (msgStr !== "") {
msgStr += " "
}
// Then append the value to the message we are creating
msgStr += (fromArray32(arr).toString());
}

};

Expand Down
16 changes: 14 additions & 2 deletions compiler/src/intermediate_representation/log_bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl WriteC for LogBucket {
fn produce_c(&self, producer: &CProducer) -> (Vec<String>, String) {
use c_code_generator::*;
let mut log_c = Vec::new();
let mut index = 0;
for logarg in &self.argsprint {
if let LogBucketArg::LogExp(exp) = logarg {
let (mut argument_code, argument_result) = exp.produce_c(producer);
Expand Down Expand Up @@ -129,14 +130,25 @@ impl WriteC for LogBucket {
else{
unreachable!();
}
if index != self.argsprint.len() - 1 {
let print_c =
build_call(
"printf".to_string(),
vec![format!("\" \"")]
);
log_c.push("{".to_string());
log_c.push(format!("{};", print_c));
log_c.push("}".to_string());
}
index += 1;
}
let print_end_line = build_call(
"printf".to_string(),
vec![format!("\"\\n\"")]
);
log_c.push("{".to_string());
log_c.push(format!("{};", print_end_line));
log_c.push("}".to_string());
log_c.push(format!("{};", print_end_line));
log_c.push("}".to_string());
(log_c, "".to_string())
}
}
5 changes: 5 additions & 0 deletions constraint_generation/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ fn execute_statement(
}
LogCall { args, .. } => {
if flag_verbose{
let mut index = 0;
for arglog in args {
if let LogArgument::LogExp(arg) = arglog{
let f_result = execute_expression(arg, program_archive, runtime, flag_verbose)?;
Expand All @@ -314,6 +315,10 @@ fn execute_statement(
else if let LogArgument::LogStr(s) = arglog {
print!("{}",s);
}
if index != args.len()-1{
print!(" ");
}
index += 1;
}
println!("");
}
Expand Down

0 comments on commit ae87f6c

Please sign in to comment.