Skip to content

Commit

Permalink
Cider debugger logging (#1802)
Browse files Browse the repository at this point in the history
* Got logging to work

* Still working

* Got logging but found weird bug

* Fixed bug -- working on finishing logging

* Finished logging (except those outside of main)

* Added tmp path for logger, passed logger in as args for other fns

* Resolved comments (moved code and imported `slog::info`)
  • Loading branch information
kadenlei authored Dec 11, 2023
1 parent dfd45c6 commit 2a6852f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions cider-dap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ serde_json = "1.0"
serde.workspace= true
owo-colors = "^3.5"
argh = "0.1"
slog = "2.7.0"
slog-term = "2.8.0"
slog-async = "2.7.0"

[[bin]]
name = "cider-dap"
Expand Down
3 changes: 2 additions & 1 deletion cider-dap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ the cider-dap binary somewhere on your path. From some directory on your PATH:
ln -s <PATH TO CALYX ROOT>/target/debug/cider-dap
```

You will have to configure user settings of cider-dap in VSCode and input your cider binary path, session type, and port number (if debug adapter is started as a server). You can then launch the adapter with the Launch Program (Multi Session) action.
You will have to configure user settings of cider-dap in VSCode and input your cider binary path, session type, and port number (if debug adapter is started as a server). You can then launch the adapter with the Debug w/ Cider action.

## Known issues

Expand Down Expand Up @@ -50,6 +50,7 @@ In `main.rs`, our program is set up to accommodate both single and multi-session

At the start of the `main()` function:

- Initializes a logger to log to the terminal if in multi-session and a file in single-session.
- The Opts struct captures command-line arguments. This struct contains an optional file path, a switch to determine if the application runs in multi-session mode, and a port number (with a default of 8080).
- `argh::from_env()` processes the command-line arguments based on the defined struct. The use of argh simplifies command-line parsing, allowing you to focus on the main logic without getting bogged down in argument processing.

Expand Down
2 changes: 1 addition & 1 deletion cider-dap/calyxDebug/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class CiderDebugAdapterDescriptorFactoryExecutable {

return new vscode.DebugAdapterExecutable(
vscode.workspace.getConfiguration("cider-dap").path,
[programName],
[],
{ cwd: vscode.workspace.rootPath }
);
}
Expand Down
50 changes: 39 additions & 11 deletions cider-dap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use error::MyAdapterError;

use dap::prelude::*;
use error::AdapterResult;
use slog::{info, Drain};
use std::fs::File;
use std::fs::OpenOptions;
use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write};
use std::net::TcpListener;

Expand All @@ -28,35 +30,60 @@ struct Opts {

fn main() -> Result<(), MyAdapterError> {
let opts: Opts = argh::from_env();
// Initializing logger
let log_path = "/tmp/output.log"; // Stores in tmp file for now, if testing, use a relative path
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(log_path)
.unwrap();

// Different decorators and drains for terminal and file logging -- async_drain picks the right one based on session
let async_drain = if opts.is_multi_session {
let term_decorator = slog_term::TermDecorator::new().build();
let term_drain =
slog_term::FullFormat::new(term_decorator).build().fuse();
slog_async::Async::new(term_drain).build().fuse()
} else {
let file_decorator = slog_term::PlainDecorator::new(file);
let file_drain =
slog_term::FullFormat::new(file_decorator).build().fuse();
slog_async::Async::new(file_drain).build().fuse()
};
let logger = slog::Logger::root(async_drain, slog::o!());

info!(logger, "Logging initialized");
if opts.is_multi_session {
eprintln!("running multi-session");
info!(logger, "running multi-session");
let listener = TcpListener::bind(("127.0.0.1", opts.port))?;
eprintln!("bound on port: {} ", opts.port);
info!(logger, "bound on port: {} ", opts.port);
let (stream, addr) = listener.accept()?;
eprintln!("Accepted client on: {}", addr); // changed to eprintln!
info!(logger, "Accepted client on: {}", addr);
let read_stream = BufReader::new(stream.try_clone()?);
let write_stream = BufWriter::new(stream);
let mut server = Server::new(read_stream, write_stream);

// Get the adapter from the init function
let adapter = multi_session_init(&mut server)?;
let adapter = multi_session_init(&mut server, &logger)?;

// Run the server using the adapter
run_server(&mut server, adapter)?;
run_server(&mut server, adapter, &logger)?;
} else {
eprintln!("running single-session");
info!(logger, "running single-session");
let write = BufWriter::new(stdout());
let read = BufReader::new(stdin());
let mut server = Server::new(read, write);
let adapter = multi_session_init(&mut server)?;
run_server(&mut server, adapter)?;
let adapter = multi_session_init(&mut server, &logger)?;
run_server(&mut server, adapter, &logger)?;
}
eprintln!("exited run_Server");
info!(logger, "exited run_Server");
Ok(())
}

fn multi_session_init<R, W>(
server: &mut Server<R, W>,
logger: &slog::Logger,
) -> AdapterResult<MyAdapter>
where
R: Read,
Expand Down Expand Up @@ -93,7 +120,7 @@ where
let program_path = if let Command::Launch(params) = &req.command {
if let Some(data) = &params.additional_data {
if let Some(program_path) = data.get("program") {
eprintln!("Program path: {}", program_path);
info!(logger, "Program path: {}", program_path);
program_path
.as_str()
.ok_or(MyAdapterError::InvalidPathError)?
Expand Down Expand Up @@ -136,6 +163,7 @@ where
fn run_server<R: Read, W: Write>(
server: &mut Server<R, W>,
mut adapter: MyAdapter,
logger: &slog::Logger,
) -> AdapterResult<()> {
loop {
// Start looping here
Expand Down Expand Up @@ -188,7 +216,7 @@ fn run_server<R: Read, W: Write>(
server.respond(rsp)?;

//Exit
eprintln!("exited debugger");
info!(logger, "exited debugger");
return Ok(());
}
// Send StackTrace, may be useful to make it more robust in the future
Expand Down

0 comments on commit 2a6852f

Please sign in to comment.