Skip to content
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

Add logging from tauri #697

Merged
merged 3 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/desktopbridge/loggerbridge.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { listen } from '@tauri-apps/api/event';
import { logger } from '../core/logger.mjs';

// listen for log events from the Tauri backend and log in the UI
await listen('log-event', (e) => {
if (e.payload == null) {
return;
}
const { message, message_type } = e.payload;
logger(message, message_type);
});
20 changes: 20 additions & 0 deletions src-tauri/src/loggerbridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::sync::Arc;
use tauri::Window;

#[derive(Clone, serde::Serialize)]
pub struct LoggerPayload {
pub message: String,
pub message_type: String,
}

#[derive(Clone)]
pub struct Logger {
pub window: Arc<Window>,
}

impl Logger {
pub fn log(&self, message: String, message_type: String) {
println!("{}", message);
let _ = self.window.emit("log-event", LoggerPayload { message, message_type });
}
}
25 changes: 21 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@

mod midibridge;
mod oscbridge;
mod loggerbridge;
use std::sync::Arc;

use loggerbridge::Logger;
use tauri::Manager;
use tokio::sync::mpsc;
use tokio::sync::Mutex;

// the payload type must implement `Serialize` and `Clone`.
#[derive(Clone, serde::Serialize)]
struct Payload {
message: String,
message_type: String,
}
fn main() {
let (async_input_transmitter_midi, async_input_receiver_midi) = mpsc::channel(1);
let (async_output_transmitter_midi, async_output_receiver_midi) = mpsc::channel(1);
Expand All @@ -20,9 +30,16 @@ fn main() {
inner: Mutex::new(async_input_transmitter_osc),
})
.invoke_handler(tauri::generate_handler![midibridge::sendmidi, oscbridge::sendosc])
.setup(|_app| {
midibridge::init(async_input_receiver_midi, async_output_receiver_midi, async_output_transmitter_midi);
oscbridge::init(async_input_receiver_osc, async_output_receiver_osc, async_output_transmitter_osc);
.setup(|app| {
let window = Arc::new(app.get_window("main").unwrap());
let logger = Logger { window };
midibridge::init(
logger.clone(),
async_input_receiver_midi,
async_output_receiver_midi,
async_output_transmitter_midi
);
oscbridge::init(logger, async_input_receiver_osc, async_output_receiver_osc, async_output_transmitter_osc);
Ok(())
})
.run(tauri::generate_context!())
Expand Down
20 changes: 15 additions & 5 deletions src-tauri/src/midibridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use midir::MidiOutput;

use tokio::sync::{ mpsc, Mutex };
use tokio::time::Instant;
use serde::Deserialize;
use std::thread::sleep;

use crate::loggerbridge::Logger;
pub struct MidiMessage {
pub message: Vec<u8>,
pub instant: Instant,
Expand All @@ -18,6 +21,7 @@ pub struct AsyncInputTransmit {
}

pub fn init(
logger: Logger,
async_input_receiver: mpsc::Receiver<Vec<MidiMessage>>,
mut async_output_receiver: mpsc::Receiver<Vec<MidiMessage>>,
async_output_transmitter: mpsc::Sender<Vec<MidiMessage>>
Expand Down Expand Up @@ -50,10 +54,16 @@ pub fn init(
let mut port_names = Vec::new();
//TODO: Send these print messages to the UI logger instead of the rust console so the user can see them
if out_ports.len() == 0 {
println!(" No MIDI devices found. Connect a device or enable IAC Driver.");
logger.log(
" No MIDI devices found. Connect a device or enable IAC Driver to enable midi.".to_string(),
"".to_string()
);
// logger(window, " No MIDI devices found. Connect a device or enable IAC Driver.".to_string(), None);
return;
}
println!("Found {} midi devices!", out_ports.len());
// give the frontend couple seconds to load on start, or the log messages will get lost
sleep(Duration::from_secs(3));
logger.log(format!("Found {} midi devices!", out_ports.len()), "".to_string());

// the user could reference any port at anytime during runtime,
// so let's go ahead and open them all (same behavior as web app)
Expand All @@ -63,7 +73,7 @@ pub fn init(
let ports = midiout.ports();
let port = ports.get(i).unwrap();
let port_name = midiout.port_name(port).unwrap();
println!("{}", port_name);
logger.log(port_name.clone(), "".to_string());
let out_con = midiout.connect(port, &port_name).unwrap();
port_names.insert(i, port_name.clone());
output_connections.insert(port_name, out_con);
Expand Down Expand Up @@ -96,10 +106,10 @@ pub fn init(
if out_con.is_some() {
// process the message
if let Err(err) = (&mut out_con.unwrap()).send(&message.message) {
println!("Midi message send error: {}", err);
logger.log(format!("Midi message send error: {}", err), "error".to_string());
}
} else {
println!("failed to find midi device: {}", message.requestedport);
logger.log(format!("failed to find midi device: {}", message.requestedport), "error".to_string());
}
return false;
});
Expand Down
9 changes: 8 additions & 1 deletion src-tauri/src/oscbridge.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use rosc::{ encoder, OscTime };
use rosc::{ OscMessage, OscPacket, OscType, OscBundle };

use std::net::UdpSocket;

use std::time::Duration;
use std::sync::Arc;
use tokio::sync::{ mpsc, Mutex };
use serde::Deserialize;
use std::thread::sleep;

use crate::loggerbridge::Logger;
pub struct OscMsg {
pub msg_buf: Vec<u8>,
pub timestamp: u64,
Expand All @@ -22,6 +25,7 @@ const NANOS_PER_SECOND: f64 = 1.0e9;
const SECONDS_PER_NANO: f64 = 1.0 / NANOS_PER_SECOND;

pub fn init(
logger: Logger,
async_input_receiver: mpsc::Receiver<Vec<OscMsg>>,
mut async_output_receiver: mpsc::Receiver<Vec<OscMsg>>,
async_output_transmitter: mpsc::Sender<Vec<OscMsg>>
Expand Down Expand Up @@ -64,7 +68,10 @@ pub fn init(
message_queue.retain(|message| {
let result = sock.send(&message.msg_buf);
if result.is_err() {
println!("OSC Message failed to send, the server might no longer be available");
logger.log(
format!("OSC Message failed to send, the server might no longer be available"),
"error".to_string()
);
}
return false;
});
Expand Down
12 changes: 10 additions & 2 deletions website/src/repl/Repl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ const modules = [
import('@strudel.cycles/core'),
import('@strudel.cycles/tonal'),
import('@strudel.cycles/mini'),
isTauri() ? import('@strudel/desktopbridge/midibridge.mjs') : import('@strudel.cycles/midi'),
import('@strudel.cycles/xen'),
import('@strudel.cycles/webaudio'),
isTauri() ? import('@strudel/desktopbridge/oscbridge.mjs') : import('@strudel.cycles/osc'),

import('@strudel.cycles/serial'),
import('@strudel.cycles/soundfonts'),
import('@strudel.cycles/csound'),
];
if (isTauri()) {
modules.concat([
import('@strudel/desktopbridge/loggerbridge.mjs'),
import('@strudel/desktopbridge/midibridge.mjs'),
import('@strudel/desktopbridge/oscbridge.mjs'),
]);
} else {
modules.concat([import('@strudel.cycles/midi'), import('@strudel.cycles/osc')]);
}

const modulesLoading = evalScope(
controls, // sadly, this cannot be exported from core direclty
Expand Down