added base logging infrastructure, more to come SOON

This commit is contained in:
Noah Santschi-Cooney 2022-03-15 00:59:16 +00:00
parent b775bd2cd5
commit 7cf009ee61
No known key found for this signature in database
GPG key ID: 3B22282472C8AE48
5 changed files with 292 additions and 17 deletions

220
server/Cargo.lock generated
View file

@ -32,6 +32,23 @@ version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1"
[[package]]
name = "arc-swap"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5d78ce20460b82d3fa150275ed9d55e21064fc7951177baacf86a145c4a4b1f"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -310,6 +327,27 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
[[package]]
name = "dirs-next"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
dependencies = [
"cfg-if 1.0.0",
"dirs-sys-next",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "dispatch"
version = "0.2.0"
@ -413,6 +451,17 @@ version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c7e4c2612746b0df8fed4ce0c69156021b704c9aefa360311c04e6e9e002eed"
[[package]]
name = "getrandom"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi",
]
[[package]]
name = "gl"
version = "0.14.0"
@ -521,6 +570,15 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "ident_case"
version = "1.0.1"
@ -575,6 +633,12 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "jni-sys"
version = "0.3.0"
@ -675,6 +739,7 @@ name = "mcshader-lsp"
version = "0.9.6"
dependencies = [
"anyhow",
"arc-swap",
"bit-set",
"chan",
"ctor",
@ -684,14 +749,20 @@ dependencies = [
"hamcrest2",
"lazy_static",
"mockall",
"once_cell",
"path-slash",
"percent-encoding",
"petgraph",
"pretty_assertions",
"rand 0.8.5",
"regex",
"rust_lsp",
"serde",
"serde_json",
"slog",
"slog-atomic",
"slog-scope",
"slog-term",
"tempdir",
"thiserror",
"url",
@ -961,6 +1032,15 @@ dependencies = [
"syn",
]
[[package]]
name = "num_threads"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c539a50b93a303167eded6e8dff5220cd39447409fb659f4cd24b1f72fe4f133"
dependencies = [
"libc",
]
[[package]]
name = "objc"
version = "0.2.7"
@ -1014,7 +1094,7 @@ dependencies = [
"cfg-if 1.0.0",
"instant",
"libc",
"redox_syscall",
"redox_syscall 0.1.57",
"smallvec",
"winapi",
]
@ -1047,6 +1127,12 @@ version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "predicates"
version = "2.1.1"
@ -1140,6 +1226,27 @@ dependencies = [
"winapi",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core 0.6.3",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.3",
]
[[package]]
name = "rand_core"
version = "0.3.1"
@ -1155,6 +1262,15 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "raw-window-handle"
version = "0.4.2"
@ -1179,6 +1295,25 @@ version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "redox_syscall"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8380fe0152551244f0747b1bf41737e0f8a74f97a14ccefd1148187271634f3c"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
dependencies = [
"getrandom",
"redox_syscall 0.2.11",
]
[[package]]
name = "regex"
version = "1.4.3"
@ -1237,6 +1372,12 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cfffa8a89d8758be2dd5605c5fc62bce055af2491ebf3ce953d4d31512c59fd"
[[package]]
name = "rustversion"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2cc38e8fa666e2de3c4aba7edeb5ffc5246c1c2ed0e3d17e560aeeba736b23f"
[[package]]
name = "ryu"
version = "1.0.5"
@ -1290,7 +1431,7 @@ version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a"
dependencies = [
"itoa",
"itoa 0.4.7",
"ryu",
"serde",
]
@ -1316,6 +1457,46 @@ dependencies = [
"libc",
]
[[package]]
name = "slog"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8347046d4ebd943127157b94d63abb990fcf729dc4e9978927fdf4ac3c998d06"
[[package]]
name = "slog-atomic"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6b517f2dda9e1458733eb8350bad1a3632ffed8141be4c0f3d6def899a9b066"
dependencies = [
"arc-swap",
"slog",
]
[[package]]
name = "slog-scope"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f95a4b4c3274cd2869549da82b57ccc930859bdbf5bcea0424bc5f140b3c786"
dependencies = [
"arc-swap",
"lazy_static",
"slog",
]
[[package]]
name = "slog-term"
version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87d29185c55b7b258b4f120eab00f48557d4d9bc814f41713f449d35b0f8977c"
dependencies = [
"atty",
"slog",
"term",
"thread_local",
"time",
]
[[package]]
name = "smallvec"
version = "1.6.1"
@ -1368,6 +1549,17 @@ dependencies = [
"remove_dir_all",
]
[[package]]
name = "term"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f"
dependencies = [
"dirs-next",
"rustversion",
"winapi",
]
[[package]]
name = "thiserror"
version = "1.0.29"
@ -1397,6 +1589,24 @@ dependencies = [
"once_cell",
]
[[package]]
name = "time"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "004cbc98f30fa233c61a38bc77e96a9106e65c88f2d3bef182ae952027e5753d"
dependencies = [
"itoa 1.0.1",
"libc",
"num_threads",
"time-macros",
]
[[package]]
name = "time-macros"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25eb0ca3468fc0acc11828786797f6ef9aa1555e4a211a60d64cc8e4d1be47d6"
[[package]]
name = "tinyvec"
version = "1.1.0"
@ -1481,6 +1691,12 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
version = "0.2.79"

View file

@ -23,6 +23,13 @@ gl = "0.14"
ctor = "0.1"
mockall = "0.11"
path-slash = "0.1"
slog = { version = "2.7", features = [ "max_level_trace", "release_max_level_trace" ] }
slog-term = "2.9"
slog-scope = "4.4"
slog-atomic = "3.1"
once_cell = "1.7"
rand = "0.8"
arc-swap = "1.5.0"
[dev-dependencies]
tempdir = "0.3"

42
server/src/logging.rs Normal file
View file

@ -0,0 +1,42 @@
use rand::{rngs, Rng};
use slog::slog_o;
use slog_scope::GlobalLoggerGuard;
use slog_term::{FullFormat, PlainSyncDecorator};
use std::{cell::RefCell, io::Stderr, sync::Arc};
use lazy_static::lazy_static;
use slog::*;
use slog_atomic::*;
fn new_trace_id() -> String {
let rng = CURRENT_RNG.with(|rng| rng.borrow_mut().gen::<[u8; 4]>());
return format!("{:04x}", u32::from_be_bytes(rng));
}
pub fn slog_with_trace_id<F: FnOnce()>(f: F) {
slog_scope::scope(&slog_scope::logger().new(slog_o!("trace" => new_trace_id())), || {
f()
})
}
pub fn set_logger_with_level(level: Level) -> GlobalLoggerGuard {
let drain = Arc::new(logger_base(level).fuse());
DRAIN_SWITCH.ctrl().set(drain.clone());
slog_scope::set_global_logger(Logger::root(drain, o!()))
}
fn logger_base(level: Level) -> LevelFilter<Fuse<FullFormat<PlainSyncDecorator<Stderr>>>> {
let plain = slog_term::PlainSyncDecorator::new(std::io::stderr());
let drain = slog_term::FullFormat::new(plain).build().fuse();
drain.filter_level(level)
}
thread_local! {
static CURRENT_RNG: RefCell<rngs::ThreadRng> = RefCell::new(rngs::ThreadRng::default());
}
lazy_static! {
static ref DRAIN_SWITCH: AtomicSwitch<()> = {
let logger = logger_base(Level::Info).fuse();
AtomicSwitch::new(logger)
};
}

View file

@ -17,6 +17,7 @@ use std::io::{stdin, stdout, BufRead, BufReader};
use std::rc::Rc;
use std::fs;
use std::iter::{Extend, FromIterator};
use slog::Level;
use path_slash::PathBufExt;
@ -33,6 +34,7 @@ mod dfs;
mod merge_views;
mod consts;
mod opengl;
mod logging;
mod url_norm;
#[cfg(test)]
@ -46,8 +48,9 @@ lazy_static! {
}
fn main() {
let stdin = stdin();
let guard = logging::set_logger_with_level(Level::Info);
let stdin = stdin();
let endpoint_output = LSPEndpoint::create_lsp_output_with_output_stream(stdout);
let cache_graph = graph::CachedStableGraph::new();
@ -57,7 +60,8 @@ fn main() {
graph: Rc::new(RefCell::new(cache_graph)),
root: "".into(),
command_provider: None,
opengl_context: Rc::new(opengl::OpenGlContext::new())
opengl_context: Rc::new(opengl::OpenGlContext::new()),
_log_guard: Some(guard),
};
langserver.command_provider = Some(commands::CustomCommandProvider::new(vec![
@ -83,7 +87,8 @@ struct MinecraftShaderLanguageServer {
graph: Rc<RefCell<graph::CachedStableGraph>>,
root: PathBuf,
command_provider: Option<commands::CustomCommandProvider>,
opengl_context: Rc<dyn opengl::ShaderValidator>
opengl_context: Rc<dyn opengl::ShaderValidator>,
_log_guard: Option<slog_scope::GlobalLoggerGuard>,
}
#[derive(Clone, PartialEq, Eq, Hash)]

View file

@ -6,6 +6,8 @@ use std::io::Result;
use hamcrest2::prelude::*;
use pretty_assertions::assert_eq;
use slog::Logger;
use slog::o;
use tempdir::TempDir;
use petgraph::algo::is_cyclic_directed;
@ -34,17 +36,20 @@ impl io::Write for StdoutNewline {
}
}
fn new_temp_server() -> MinecraftShaderLanguageServer {
let endpoint = LSPEndpoint::create_lsp_output_with_output_stream(|| StdoutNewline {
s: Box::new(io::sink()),
});
fn new_temp_server(opengl_context: Option<Box<dyn opengl::ShaderValidator>>) -> MinecraftShaderLanguageServer {
let endpoint = LSPEndpoint::create_lsp_output_with_output_stream(|| StdoutNewline { s: Box::new(io::sink()) });
let context = opengl_context.unwrap_or_else(|| Box::new(opengl::MockShaderValidator::new()));
let logger = Logger::root(slog::Discard, o!());
let guard = slog_scope::set_global_logger(logger);
MinecraftShaderLanguageServer {
endpoint,
graph: Rc::new(RefCell::new(graph::CachedStableGraph::new())),
root: "".into(),
command_provider: None,
opengl_context: Rc::new(opengl::MockShaderValidator::new()),
opengl_context: context.into(),
_log_guard: Some(guard),
}
}
@ -80,7 +85,7 @@ fn copy_to_tmp_dir(test_path: &str) -> (Rc<TempDir>, PathBuf) {
#[allow(deprecated)]
#[test]
fn test_empty_initialize() {
let mut server = new_temp_server();
let mut server = new_temp_server(None);
let tmp_dir = TempDir::new("mcshader").unwrap();
let tmp_path = tmp_dir.path();
@ -131,7 +136,7 @@ fn test_empty_initialize() {
#[allow(deprecated)]
#[test]
fn test_01_initialize() {
let mut server = new_temp_server();
let mut server = new_temp_server(None);
let (_tmp_dir, tmp_path) = copy_to_tmp_dir("./testdata/01");
@ -197,7 +202,7 @@ fn test_01_initialize() {
#[test]
fn test_05_initialize() {
let mut server = new_temp_server();
let mut server = new_temp_server(None);
let (_tmp_dir, tmp_path) = copy_to_tmp_dir("./testdata/05");
@ -902,7 +907,7 @@ fn test_graph_dfs_cycle() {
#[test]
fn test_generate_merge_list_01() {
let mut server = new_temp_server();
let mut server = new_temp_server(None);
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/01", &mut server);
server.endpoint.request_shutdown();
@ -941,7 +946,7 @@ fn test_generate_merge_list_01() {
#[test]
fn test_generate_merge_list_02() {
let mut server = new_temp_server();
let mut server = new_temp_server(None);
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/02", &mut server);
server.endpoint.request_shutdown();
@ -1014,7 +1019,7 @@ fn test_generate_merge_list_02() {
#[test]
fn test_generate_merge_list_03() {
let mut server = new_temp_server();
let mut server = new_temp_server(None);
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/03", &mut server);
server.endpoint.request_shutdown();
@ -1087,7 +1092,7 @@ fn test_generate_merge_list_03() {
#[test]
fn test_generate_merge_list_04() {
let mut server = new_temp_server();
let mut server = new_temp_server(None);
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/04", &mut server);
server.endpoint.request_shutdown();