将日志通过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

View File

@@ -1,11 +1,24 @@
use ::log::{debug, error, info, trace, warn};
use encoding_rs::Encoding;
use tokio::runtime::Runtime;
use crate::protobuf::gou::{MessageType, Starpoles};
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 network;
mod config;
pub mod protobuf;
pub mod utils;
#[cxx::bridge]
@@ -15,7 +28,7 @@ mod ffi {
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);
@@ -31,30 +44,85 @@ mod ffi {
fn log_error(msg: &cxx::CxxString) {
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) {
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) {
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) {
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) {
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);
}
@@ -78,5 +146,3 @@ fn http_get(url: &cxx::CxxString) -> Result<String, Box<dyn std::error::Error>>
let url = cxx_string_to_string(url);
rt.block_on(network::http_get(&url))
}

View File

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