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 · 2 comments
Labels
enhancement New feature or request

Comments

@pigeoner
Copy link
Contributor

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 :)

@Reverier-Xu
Copy link
Member

Good day.

That's a good suggestion, only a little problem: your implementation dropped timezone information. The Z in the end of time string means +00:00, which allows people translate it to local time in anywhere.

maybe we can turn it into local ISO time (such as 2024-11-29T12:01:39+08:00). However, considering that the organizers and players may not be in the same time zone, in order to avoid possible regional discrimination issues, this matter remains to be discussed.

@Reverier-Xu Reverier-Xu added the enhancement New feature or request label Nov 29, 2024
@pigeoner
Copy link
Contributor Author

Good day.

That's a good suggestion, only a little problem: your implementation dropped timezone information. The Z in the end of time string means +00:00, which allows people translate it to local time in anywhere.

maybe we can turn it into local ISO time (such as 2024-11-29T12:01:39+08:00). However, considering that the organizers and players may not be in the same time zone, in order to avoid possible regional discrimination issues, this matter remains to be discussed.

Thank you for your reply! I agree with your point that we should be cautious when handling the time display for users in different regions, in order to avoid unnecessary misunderstandings. Looking forward to this issue being resolved in the future.

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

No branches or pull requests

2 participants