Skip to content

Commit

Permalink
feat(lsp): use local time with shorter format
Browse files Browse the repository at this point in the history
Change from UTC time to local time in any log messages, and shorten the
time format to only the time component (stripping off the date).
  • Loading branch information
dnaka91 committed Dec 5, 2023
1 parent 21e5001 commit e9407a4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
25 changes: 13 additions & 12 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions crates/stef-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
stef-compiler = { path = "../stef-compiler" }
stef-parser = { path = "../stef-parser" }
tokio = { version = "1.34.0", features = ["fs", "io-std", "io-util", "macros", "rt-multi-thread"] }
time = { version = "0.3.30", features = ["formatting", "local-offset", "macros"] }
tokio = { version = "1.34.0", features = ["fs", "io-std", "io-util", "rt-multi-thread"] }
tower-lsp = { version = "0.20.0", features = ["proposed"] }
tracing = "0.1.40"
tracing-appender = "0.2.3"
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3.18", features = ["time"] }

[lints]
workspace = true
62 changes: 40 additions & 22 deletions crates/stef-lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::{ensure, Context, Result};
use directories::ProjectDirs;
use ouroboros::self_referencing;
use stef_parser::Schema;
use time::{macros::format_description, UtcOffset};
use tokio::sync::{Mutex, RwLock};
use tower_lsp::{
async_trait,
Expand All @@ -23,7 +24,11 @@ use tower_lsp::{
Client, LanguageServer, LspService, Server,
};
use tracing::{debug, error, Level};
use tracing_subscriber::{filter::Targets, fmt::MakeWriter, prelude::*};
use tracing_subscriber::{
filter::Targets,
fmt::{time::OffsetTime, MakeWriter},
prelude::*,
};

use self::cli::Cli;

Expand Down Expand Up @@ -243,9 +248,12 @@ impl LanguageServer for Backend {
}
}

#[tokio::main]
async fn main() -> Result<()> {
fn main() -> Result<()> {
let cli = Cli::parse();
let timer = OffsetTime::new(
UtcOffset::current_local_offset().context("failed retrieving local UTC offset")?,
format_description!("[hour]:[minute]:[second]"),
);

let dirs = ProjectDirs::from("rocks", "dnaka91", env!("CARGO_PKG_NAME"))
.context("failed locating project directories")?;
Expand All @@ -258,9 +266,14 @@ async fn main() -> Result<()> {
.with(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_timer(timer.clone())
.with_writer(ClientLogWriter::new(client.clone())),
)
.with(tracing_subscriber::fmt::layer().with_writer(file_appender))
.with(
tracing_subscriber::fmt::layer()
.with_timer(timer)
.with_writer(file_appender),
)
.with(Targets::new().with_default(Level::WARN).with_targets([
(env!("CARGO_CRATE_NAME"), Level::TRACE),
("stef_compiler", Level::TRACE),
Expand All @@ -276,24 +289,29 @@ async fn main() -> Result<()> {
}
});

if cli.stdio {
let (stdin, stdout) = (tokio::io::stdin(), tokio::io::stdout());
Server::new(stdin, stdout, socket).serve(service).await;
} else if let Some(file) = cli.pipe {
let file = tokio::fs::File::options()
.read(true)
.write(true)
.open(file)
.await
.context("failed to open provided pipe/socket")?;

let (read, write) = tokio::io::split(file);
Server::new(read, write, socket).serve(service).await;
} else if let Some(port) = cli.socket {
unimplemented!("open TCP connection on port {port}");
}

Ok(())
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?
.block_on(async move {
if cli.stdio {
let (stdin, stdout) = (tokio::io::stdin(), tokio::io::stdout());
Server::new(stdin, stdout, socket).serve(service).await;
} else if let Some(file) = cli.pipe {
let file = tokio::fs::File::options()
.read(true)
.write(true)
.open(file)
.await
.context("failed to open provided pipe/socket")?;

let (read, write) = tokio::io::split(file);
Server::new(read, write, socket).serve(service).await;
} else if let Some(port) = cli.socket {
unimplemented!("open TCP connection on port {port}");
}

anyhow::Ok(())
})
}

struct ClientLogWriter {
Expand Down

0 comments on commit e9407a4

Please sign in to comment.