Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Adjust the log time from UTC to the time corresponding to the local time zone #9

Open
pigeoner opened this issue Nov 17, 2024 · 0 comments

Comments

@pigeoner
Copy link

System information for bug reporting and debugging

System	: Windows 11 Version 23H2
CPU	: x86_64
Kernel	: winnt-10.0.22631
ABI	: x86_64-little_endian-llp64
WSRX	: 0.2.31.g195a598
Machine	: LAPTOP-5HC8OVNN-cb79d211-728d-4bbb-b01d-c7f676d5c40d

Problem

I found that the time in the log information output by wsrx is UTC time. Can we change UTC to the time corresponding to the host time zone (such as East Zone 8)?
image

Suggestions

Refer to this article: Rust入门秘籍 - tracing简要说明, the code may be modified in this way:
desktop/pool.cc:

@@ -202,7 +202,7 @@ void LinkList::refreshStatus() {
             auto latency = timer.elapsed();
             if (reply->error() != QNetworkReply::NoError) {
                 if (m_logs) {
-                    m_logs->appendLog(Log(QDateTime::currentDateTimeUtc().toString(Qt::ISODate), EventLevel::ERROR,
+                    m_logs->appendLog(Log(QDateTime::currentDateTime().toString(Qt::ISODate), EventLevel::ERROR,
                                           reply->errorString(), u"wsrx::desktop::pool"_s));
                 }
                 setData(index(i), LinkStatus::DEAD, StatusRole);

wsrx/src/cli/logger.rs:

-use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
+use tracing_subscriber::{
+    fmt::{format::Writer, time::FormatTime},
+    layer::SubscriberExt,
+    util::SubscriberInitExt,
+};
+
+struct LocalTimer;
+
+const fn east8() -> Option<chrono::FixedOffset> {
+    chrono::FixedOffset::east_opt(8 * 3600)
+}
+
+impl FormatTime for LocalTimer {
+    fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
+        let now = chrono::Utc::now().with_timezone(&east8().unwrap());
+        write!(w, "{}", now.format("%Y-%m-%dT%H:%M:%S%.3f"))
+    }
+}

 pub fn init_logger(json: bool) {
     if json {
@@ -7,7 +24,11 @@ pub fn init_logger(json: bool) {
                 tracing_subscriber::EnvFilter::try_from_default_env()
                     .unwrap_or_else(|_| "wsrx=info,tower_http=info".into()),
             )
-            .with(tracing_subscriber::fmt::layer().json())
+            .with(
+                tracing_subscriber::fmt::layer()
+                    .with_timer(LocalTimer)
+                    .json(),
+            )
             .init();
     } else {
         tracing_subscriber::registry()

At the same time, there are many Utc::now() in the heartbeat module(in file wsrx/src/cli/daemon.rs), but due to insufficient experimentation, I am not sure if they should be modified.

I am a beginner in C++ and Rust, please forgive me if there are any unreasonable aspects :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant