将日志通过WebSocket发送出去

This commit is contained in:
2026-01-29 01:58:34 +08:00
parent dd7c5151dc
commit 85d5b179e2
9 changed files with 526 additions and 17 deletions

314
Cargo.lock generated
View File

@@ -6,6 +6,7 @@ version = 4
name = "DnfUtils" name = "DnfUtils"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bytes",
"chrono", "chrono",
"colored", "colored",
"cxx", "cxx",
@@ -17,9 +18,13 @@ dependencies = [
"indicatif", "indicatif",
"libc", "libc",
"log", "log",
"prost",
"prost-build",
"reqwest", "reqwest",
"serde", "serde",
"spin",
"tokio", "tokio",
"tokio-tungstenite",
"toml", "toml",
"tracing-appender", "tracing-appender",
] ]
@@ -92,6 +97,12 @@ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
[[package]]
name = "anyhow"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]] [[package]]
name = "atomic-waker" name = "atomic-waker"
version = "1.1.2" version = "1.1.2"
@@ -116,6 +127,15 @@ version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.19.1" version = "3.19.1"
@@ -238,6 +258,15 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "crossbeam-channel" name = "crossbeam-channel"
version = "0.5.15" version = "0.5.15"
@@ -253,6 +282,16 @@ version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "crypto-common"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
dependencies = [
"generic-array",
"typenum",
]
[[package]] [[package]]
name = "cxx" name = "cxx"
version = "1.0.192" version = "1.0.192"
@@ -264,7 +303,7 @@ dependencies = [
"cxxbridge-cmd", "cxxbridge-cmd",
"cxxbridge-flags", "cxxbridge-flags",
"cxxbridge-macro", "cxxbridge-macro",
"foldhash", "foldhash 0.2.0",
"link-cplusplus", "link-cplusplus",
] ]
@@ -315,6 +354,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "data-encoding"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7a1e2f27636f116493b8b860f5546edb47c8d8f8ea73e1d2a20be88e28d1fea"
[[package]] [[package]]
name = "deranged" name = "deranged"
version = "0.5.5" version = "0.5.5"
@@ -324,6 +369,16 @@ dependencies = [
"powerfmt", "powerfmt",
] ]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]] [[package]]
name = "displaydoc" name = "displaydoc"
version = "0.2.5" version = "0.2.5"
@@ -335,6 +390,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]] [[package]]
name = "encode_unicode" name = "encode_unicode"
version = "1.0.0" version = "1.0.0"
@@ -410,6 +471,18 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db" checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db"
[[package]]
name = "fixedbitset"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99"
[[package]]
name = "foldhash"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]] [[package]]
name = "foldhash" name = "foldhash"
version = "0.2.0" version = "0.2.0"
@@ -501,6 +574,16 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.3.4" version = "0.3.4"
@@ -513,12 +596,27 @@ dependencies = [
"wasip2", "wasip2",
] ]
[[package]]
name = "hashbrown"
version = "0.15.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
dependencies = [
"foldhash 0.1.5",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.16.1" version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "http" name = "http"
version = "1.4.0" version = "1.4.0"
@@ -752,7 +850,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown 0.16.1",
] ]
[[package]] [[package]]
@@ -790,6 +888,15 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.17" version = "1.0.17"
@@ -895,6 +1002,12 @@ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
[[package]]
name = "multimap"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084"
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.14" version = "0.2.14"
@@ -1012,6 +1125,17 @@ version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "petgraph"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455"
dependencies = [
"fixedbitset",
"hashbrown 0.15.5",
"indexmap",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.16" version = "0.2.16"
@@ -1060,6 +1184,25 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "prettyplease"
version = "0.2.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
dependencies = [
"proc-macro2",
"syn",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.105" version = "1.0.105"
@@ -1069,6 +1212,57 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "prost"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2ea70524a2f82d518bce41317d0fae74151505651af45faf1ffbd6fd33f0568"
dependencies = [
"bytes",
"prost-derive",
]
[[package]]
name = "prost-build"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "343d3bd7056eda839b03204e68deff7d1b13aba7af2b2fd16890697274262ee7"
dependencies = [
"heck",
"itertools",
"log",
"multimap",
"petgraph",
"prettyplease",
"prost",
"prost-types",
"regex",
"syn",
"tempfile",
]
[[package]]
name = "prost-derive"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27c6023962132f4b30eb4c172c91ce92d933da334c59c23cddee82358ddafb0b"
dependencies = [
"anyhow",
"itertools",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "prost-types"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8991c4cbdb8bc5b11f0b074ffe286c30e523de90fee5ba8132f1399f23cb3dd7"
dependencies = [
"prost",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.43" version = "1.0.43"
@@ -1084,6 +1278,35 @@ version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.5.18" version = "0.5.18"
@@ -1269,6 +1492,17 @@ dependencies = [
"serde_core", "serde_core",
] ]
[[package]]
name = "sha1"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]] [[package]]
name = "sharded-slab" name = "sharded-slab"
version = "0.1.7" version = "0.1.7"
@@ -1316,6 +1550,15 @@ dependencies = [
"windows-sys 0.60.2", "windows-sys 0.60.2",
] ]
[[package]]
name = "spin"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5fe4ccb98d9c292d56fec89a5e07da7fc4cf0dc11e156b41793132775d3e591"
dependencies = [
"lock_api",
]
[[package]] [[package]]
name = "stable_deref_trait" name = "stable_deref_trait"
version = "1.2.1" version = "1.2.1"
@@ -1489,6 +1732,18 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "tokio-tungstenite"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857"
dependencies = [
"futures-util",
"log",
"tokio",
"tungstenite",
]
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.7.18" version = "0.7.18"
@@ -1634,6 +1889,29 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "tungstenite"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8628dcc84e5a09eb3d8423d6cb682965dea9133204e8fb3efee74c2a0c259442"
dependencies = [
"bytes",
"data-encoding",
"http",
"httparse",
"log",
"rand",
"sha1",
"thiserror",
"utf-8",
]
[[package]]
name = "typenum"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.22" version = "1.0.22"
@@ -1664,6 +1942,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "utf-8"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]] [[package]]
name = "utf8_iter" name = "utf8_iter"
version = "1.0.4" version = "1.0.4"
@@ -1682,6 +1966,12 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]] [[package]]
name = "want" name = "want"
version = "0.3.1" version = "0.3.1"
@@ -1990,6 +2280,26 @@ dependencies = [
"synstructure", "synstructure",
] ]
[[package]]
name = "zerocopy"
version = "0.8.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdea86ddd5568519879b8187e1cf04e24fce28f7fe046ceecbce472ff19a2572"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c15e1b46eff7c6c91195752e0eeed8ef040e391cdece7c25376957d5f15df22"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "zerofrom" name = "zerofrom"
version = "0.1.6" version = "0.1.6"

View File

@@ -23,6 +23,11 @@ encoding_rs = "0.8.35"
tracing-appender = "0.2.4" tracing-appender = "0.2.4"
serde = { version = "1.0.228", features = ["derive"] } serde = { version = "1.0.228", features = ["derive"] }
toml = "0.9.11" toml = "0.9.11"
prost = "0.14.3"
tokio-tungstenite = "0.28.0"
bytes = "1.11.0"
spin = "0.10.0"
[build-dependencies] [build-dependencies]
cxx-build = "1.0.192" cxx-build = "1.0.192"
prost-build = "0.14.3"

View File

@@ -1,7 +1,69 @@
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
fn main() { fn main() {
cxx_build::bridges(["src/lib.rs", "src/config.rs"]) cxx_build::bridges(["src/lib.rs", "src/config.rs"])
.std("c++20") .std("c++20")
.compile("dnf_utils"); .compile("dnf_utils");
println!("cargo:rerun-if-changed=src/lib.rs"); println!("cargo:rerun-if-changed=src/lib.rs");
println!("cargo:rerun-if-changed=src/config.rs"); println!("cargo:rerun-if-changed=src/config.rs");
let out_dir = "src/protobuf";
if PathBuf::from_str(out_dir).unwrap().exists() {
fs::remove_dir_all(out_dir).expect("清空输出目录出错");
}
fs::create_dir_all(out_dir).expect("创建输出目录出错");
let proto_list = get_filenames("proto")
.expect("读取Protobuf定义文件列表失败")
.iter()
.filter_map(|file| {
if file.ends_with(".proto") {
Some(file.clone())
} else {
None
}
})
.collect::<Vec<_>>();
prost_build::Config::new()
.out_dir(out_dir)
.compile_protos(&proto_list, &["proto/".to_string()]).unwrap();
//tonic_prost_build::configure()
// .out_dir(out_dir)
// .compile_protos(&proto_list, &["proto/".to_string()])?;
let mod_file = format!("{}/mod.rs", out_dir);
let mod_content = get_filenames(out_dir).expect("无法列出目录内容");
let mod_content = mod_content
.iter()
.map(|file| format!("pub mod {};", file.trim_end_matches(".rs")))
.collect::<Vec<_>>()
.join("\n");
fs::write(&mod_file, mod_content).expect("写出文件失败");
fs::write(
format!("{}/README.md", out_dir),
"# Warning\n\n[protobuf](../protobuf) 中的文件由构建脚本生成,重新生成时将会**删除**此目录内的**全部文件**\
,请不要在此目录内工作,否则可能丢失工作进度!\n\n## 你被警告了!",
).unwrap();
}
fn get_filenames(dir: &str) -> Result<Vec<String>, std::io::Error> {
println!("cargo:rerun-if-changed=proto");
let entries = fs::read_dir(Path::new(dir))?;
let filenames: Vec<String> = entries
.filter_map(|entry| {
let path = entry.ok()?.path();
if path.is_file() {
path.file_name()?.to_str().map(|s| s.to_owned())
} else {
None
}
})
.collect();
Ok(filenames)
} }

17
proto/message.proto Normal file
View File

@@ -0,0 +1,17 @@
syntax = "proto3";
package gou;
enum MessageType {
MESSAGE_TYPE_TRACE = 0;
MESSAGE_TYPE_DEBUG = 1;
MESSAGE_TYPE_INFO = 2;
MESSAGE_TYPE_WARNING = 3;
MESSAGE_TYPE_ERROR = 4;
}
message Starpoles {
MessageType type = 1;
string message = 2;
}

View File

@@ -1,11 +1,24 @@
use ::log::{debug, error, info, trace, warn}; use crate::protobuf::gou::{MessageType, Starpoles};
use encoding_rs::Encoding;
use tokio::runtime::Runtime;
use crate::utils::cxx_string_to_string; use crate::utils::cxx_string_to_string;
use ::log::{debug, error, info, trace, warn};
use bytes::Bytes;
use cxx::CxxString;
use encoding_rs::Encoding;
use futures_util::SinkExt;
use prost::Message as WebMessage;
use spin::Mutex;
use std::sync::mpsc;
use tokio::net::TcpStream;
use tokio::runtime::Runtime;
use tokio_tungstenite::tungstenite::Message;
use std::sync::OnceLock;
static LOGGER_SENDER: OnceLock<mpsc::Sender<Starpoles>> = OnceLock::new();
mod config;
mod log; mod log;
mod network; mod network;
mod config; pub mod protobuf;
pub mod utils; pub mod utils;
#[cxx::bridge] #[cxx::bridge]
@@ -15,7 +28,7 @@ mod ffi {
fn http_get(url: &CxxString) -> Result<String>; fn http_get(url: &CxxString) -> Result<String>;
fn init_log(is_debug: bool); fn init_log(is_debug: bool, ws_uel: &CxxString);
fn log_trace(msg: &CxxString); fn log_trace(msg: &CxxString);
@@ -31,30 +44,85 @@ mod ffi {
fn log_error(msg: &cxx::CxxString) { fn log_error(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg); let msg = cxx_string_to_string(msg);
error!("{}", msg); let lock = LOGGER_SENDER.get().unwrap();
let _ = lock.send(Starpoles {
r#type: MessageType::Error.into(),
message: msg,
});
//error!("{}", msg);
} }
fn log_warning(msg: &cxx::CxxString) { fn log_warning(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg); let msg = cxx_string_to_string(msg);
warn!("{}", msg); let lock = LOGGER_SENDER.get().unwrap();
let _ = lock.send(Starpoles {
r#type: MessageType::Warning.into(),
message: msg,
});
} }
fn log_info(msg: &cxx::CxxString) { fn log_info(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg); let msg = cxx_string_to_string(msg);
info!("{}", msg); let lock = LOGGER_SENDER.get().unwrap();
let _ = lock.send(Starpoles {
r#type: MessageType::Info.into(),
message: msg,
});
} }
fn log_debug(msg: &cxx::CxxString) { fn log_debug(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg); let msg = cxx_string_to_string(msg);
debug!("{}", msg); let lock = LOGGER_SENDER.get().unwrap();
let _ = lock.send(Starpoles {
r#type: MessageType::Error.into(),
message: msg,
});
} }
fn log_trace(msg: &cxx::CxxString) { fn log_trace(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg); let msg = cxx_string_to_string(msg);
trace!("{}", msg); let lock = LOGGER_SENDER.get().unwrap();
let _ = lock.send(Starpoles {
r#type: MessageType::Trace.into(),
message: msg,
});
} }
fn init_log(is_debug: bool) { fn init_log(is_debug: bool, ws_uel: &CxxString) {
let url = cxx_string_to_string(ws_uel);
let (tx, rx) = mpsc::channel::<Starpoles>();
LOGGER_SENDER.set(tx).expect("已经初始化过了");
std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(async move {
loop {
match tokio_tungstenite::connect_async(url.clone()).await {
Ok((mut stream, _resp)) => {
while let Ok(line) = rx.recv() {
let mut buffer = vec![];
// Vec总是具备足够的空间
line.encode(&mut buffer).unwrap();
if stream
.send(Message::Binary(Bytes::from(buffer)))
.await
.is_err()
{
break;
}
}
}
Err(_) => {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}
}
}
});
});
log::init_log(is_debug); log::init_log(is_debug);
} }
@@ -78,5 +146,3 @@ fn http_get(url: &cxx::CxxString) -> Result<String, Box<dyn std::error::Error>>
let url = cxx_string_to_string(url); let url = cxx_string_to_string(url);
rt.block_on(network::http_get(&url)) rt.block_on(network::http_get(&url))
} }

View File

@@ -6,7 +6,7 @@ use log::Level;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Write; use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::{env, fs}; use std::{env, fs, thread};
/// 获取可执行文件所在目录 /// 获取可执行文件所在目录
pub fn get_current_dir() -> PathBuf { pub fn get_current_dir() -> PathBuf {
@@ -51,7 +51,7 @@ pub fn init_log(is_debug: bool) {
.chain(std::io::stdout()); .chain(std::io::stdout());
if is_debug { if is_debug {
console_dispatch = console_dispatch.level(log::LevelFilter::Trace); console_dispatch = console_dispatch.level(log::LevelFilter::Debug);
} else { } else {
console_dispatch = console_dispatch.level(log::LevelFilter::Info); console_dispatch = console_dispatch.level(log::LevelFilter::Info);
} }

5
src/protobuf/README.md Normal file
View File

@@ -0,0 +1,5 @@
# Warning
[protobuf](../protobuf) 中的文件由构建脚本生成,重新生成时将会**删除**此目录内的**全部文件**,请不要在此目录内工作,否则可能丢失工作进度!
## 你被警告了!

43
src/protobuf/gou.rs Normal file
View File

@@ -0,0 +1,43 @@
// This file is @generated by prost-build.
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct Starpoles {
#[prost(enumeration = "MessageType", tag = "1")]
pub r#type: i32,
#[prost(string, tag = "2")]
pub message: ::prost::alloc::string::String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum MessageType {
Trace = 0,
Debug = 1,
Info = 2,
Warning = 3,
Error = 4,
}
impl MessageType {
/// String value of the enum field names used in the ProtoBuf definition.
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
Self::Trace => "MESSAGE_TYPE_TRACE",
Self::Debug => "MESSAGE_TYPE_DEBUG",
Self::Info => "MESSAGE_TYPE_INFO",
Self::Warning => "MESSAGE_TYPE_WARNING",
Self::Error => "MESSAGE_TYPE_ERROR",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"MESSAGE_TYPE_TRACE" => Some(Self::Trace),
"MESSAGE_TYPE_DEBUG" => Some(Self::Debug),
"MESSAGE_TYPE_INFO" => Some(Self::Info),
"MESSAGE_TYPE_WARNING" => Some(Self::Warning),
"MESSAGE_TYPE_ERROR" => Some(Self::Error),
_ => None,
}
}
}

1
src/protobuf/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod gou;