Skip to content

logging #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ bindgen = "0.58.1"
count_tts = "*"
lazy_static = "*"
libc = "*"
log = "0.4.14"
pretty_env_logger = "0.4.0"
render = "*"
string-error = "0.1.0"
strum_macros = "0.20.1"
Expand Down
27 changes: 15 additions & 12 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::types::{
duckdb_blob, duckdb_date, duckdb_hugeint, duckdb_interval, duckdb_time, duckdb_timestamp,
duckdb_type as DuckDBType, DuckDBColumn, DuckDBResult,
};
use log::info;
use render::html;
use render::{rsx, SimpleElement};
use std::cell::RefCell;
Expand Down Expand Up @@ -190,7 +191,7 @@ impl<'a> Clone for ResolvedResult<'a> {
}
impl<'a> Drop for ResolvedResult<'a> {
fn drop(&mut self) {
println!("Dropping {:?}", self);
info!("Dropping {:?}", self);
unsafe { duckdb_destroy_result(self.result) };
}
}
Expand Down Expand Up @@ -295,10 +296,10 @@ unsafe fn run_async() -> Result<(), Box<dyn std::error::Error>> {
set_page_title("DuckDB Test".to_string());

let db = Some(DB::new(Some("db.db"))?);
println!("DB: {:?}", db);
info!("DB: {:?}", db);
database.with(|f| f.replace(db));

println!("DB open");
info!("DB open");

let string = html! { <>{form()}</> };
set_body_html(string);
Expand All @@ -309,7 +310,7 @@ unsafe fn run_async() -> Result<(), Box<dyn std::error::Error>> {
fn hook(info: &std::panic::PanicInfo) {
let mut msg = info.to_string();

println!("{:?}", msg);
info!("{:?}", msg);

// Add the error stack to our message.
//
Expand All @@ -325,10 +326,10 @@ fn hook(info: &std::panic::PanicInfo) {
// #[cfg(not(test))]
// {
// let error = js_sys::Error::new("test1");
// println!("{:?}", error);
// info!("{:?}", error);
// }
// let stack = error.stack();
// println!("{:?}", stack);
// info!("{:?}", stack);
// msg.push_str(stack.as_str().unwrap_or_default());

// Safari's devtools, on the other hand, _do_ mess with logged
Expand All @@ -338,26 +339,26 @@ fn hook(info: &std::panic::PanicInfo) {
msg.push_str("\n\n");

// Finally, log the panic with `console.error`!
println!("{}", msg);
info!("{}", msg);
}

#[no_mangle]
extern "C" fn callback(query_: *const c_char) {
let org = unsafe { CStr::from_ptr(query_) };
let query = org.to_string_lossy();

println!("you called?: {} {:?} {:?}", query, org, query_);
info!("you called?: {} {:?} {:?}", query, org, query_);

database.with(|borrowed| {
println!("borrowed: {:?}", borrowed);
info!("borrowed: {:?}", borrowed);
let yo = borrowed.borrow();
println!("yo: {:?}", yo);
info!("yo: {:?}", yo);

let conn = yo.as_ref().expect("no db?").connection().unwrap();

let string = match conn.query(&query) {
Ok(resolved) => {
println!("columns: {:?}", resolved.columns);
info!("columns: {:?}", resolved.columns);

let table = Table {
resolved: &resolved,
Expand All @@ -380,13 +381,15 @@ extern "C" fn callback(query_: *const c_char) {
}
};

println!("{}", string);
info!("{}", string);

set_body_html(string);
});
}

pub fn main() -> Result<(), Box<dyn std::error::Error>> {
pretty_env_logger::init();

std::panic::set_hook(Box::new(hook));

unsafe {
Expand Down
7 changes: 4 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
duckdb_connect, duckdb_disconnect, duckdb_open, duckdb_query, ext_duckdb_close, malloc,
Database, DuckDBState, ResolvedResult, PTR,
};
use log::info;
use std::ffi::{CStr, CString};

extern "C" {
Expand Down Expand Up @@ -31,13 +32,13 @@ impl DB {

pub fn connection(&self) -> Result<Connection, Box<dyn std::error::Error>> {
let connection: *const crate::Connection = unsafe { create_connection(self.db) };
println!("conn: {:?}", &connection);
info!("conn: {:?}", &connection);
Ok(Connection { connection })
}
}
impl Drop for DB {
fn drop(&mut self) {
println!("Dropping {:?}", self);
info!("Dropping {:?}", self);
unsafe { ext_duckdb_close(self.db) };
}
}
Expand Down Expand Up @@ -66,7 +67,7 @@ impl Connection {
}
impl Drop for Connection {
fn drop(&mut self) {
println!("Dropping {:?}", self);
info!("Dropping {:?}", self);
unsafe { duckdb_disconnect(self.connection) };
}
}
6 changes: 4 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
c_char, callback, duckdb_date, duckdb_time, duckdb_timestamp, emscripten_asm_const_int, hook,
main,
};
use log::info;
use speculate::speculate;
use std::ffi::{CStr, CString};

Expand All @@ -30,6 +31,7 @@ fn get_document_html() -> String {

speculate! {
before {
pretty_env_logger::init();
std::panic::set_hook(Box::new(hook));

jse!(b"global.document = {body: {}};\x00");
Expand Down Expand Up @@ -130,10 +132,10 @@ speculate! {
let db = DB::new(
None
).expect("db");
println!("db: {:?}", &db);
info!("db: {:?}", &db);

let conn = db.connection().expect("connection");
println!("conn: {:?}", &conn);
info!("conn: {:?}", &conn);

conn.query("select 1").expect("query");
}
Expand Down