[llvm] Add option to log debugging to stdout (PR #91533)

Patrick Quist via llvm-commits llvm-commits at lists.llvm.org
Thu May 9 16:20:05 PDT 2024


https://github.com/partouf updated https://github.com/llvm/llvm-project/pull/91533

>From cb8386ad4837b159dce5de96c265fe666000efb4 Mon Sep 17 00:00:00 2001
From: Partouf <partouf at gmail.com>
Date: Wed, 8 May 2024 22:26:21 +0200
Subject: [PATCH 1/3] add option to log debugging to stdout

---
 llvm/lib/Support/Debug.cpp | 49 ++++++++++++++++++++++++--------------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/Support/Debug.cpp b/llvm/lib/Support/Debug.cpp
index 98a9ac4722b50..a04aabe676273 100644
--- a/llvm/lib/Support/Debug.cpp
+++ b/llvm/lib/Support/Debug.cpp
@@ -78,6 +78,11 @@ void setCurrentDebugTypes(const char **Types, unsigned Count) {
 }
 } // namespace llvm
 
+cl::opt<bool> LogDebugToStdOut(
+    "debug-to-stdout",
+    llvm::cl::desc("Log debugging to stdout instead of stderr"),
+    cl::init(false), cl::Hidden);
+
 // All Debug.h functionality is a no-op in NDEBUG mode.
 #ifndef NDEBUG
 
@@ -161,23 +166,27 @@ static void debug_user_sig_handler(void *Cookie) {
 
 /// dbgs - Return a circular-buffered debug stream.
 raw_ostream &llvm::dbgs() {
-  // Do one-time initialization in a thread-safe way.
-  static struct dbgstream {
-    circular_raw_ostream strm;
-
-    dbgstream()
-        : strm(errs(), "*** Debug Log Output ***\n",
-               (!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
-      if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
-        // TODO: Add a handler for SIGUSER1-type signals so the user can
-        // force a debug dump.
-        sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
-      // Otherwise we've already set the debug stream buffer size to
-      // zero, disabling buffering so it will output directly to errs().
-    }
-  } thestrm;
-
-  return thestrm.strm;
+  if (LogDebugToStdOut) {
+    return outs();
+  } else {
+    // Do one-time initialization in a thread-safe way.
+    static struct dbgstream {
+      circular_raw_ostream strm;
+
+      dbgstream()
+          : strm(errs(), "*** Debug Log Output ***\n",
+                 (!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
+        if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
+          // TODO: Add a handler for SIGUSER1-type signals so the user can
+          // force a debug dump.
+          sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
+        // Otherwise we've already set the debug stream buffer size to
+        // zero, disabling buffering so it will output directly to errs().
+      }
+    } thestrm;
+
+    return thestrm.strm;
+  }
 }
 
 #else
@@ -185,7 +194,11 @@ raw_ostream &llvm::dbgs() {
 namespace llvm {
   /// dbgs - Return errs().
   raw_ostream &dbgs() {
-    return errs();
+    if (LogDebugToStdOut) {
+      return outs();
+    } else {
+      return errs();
+    }
   }
 }
 void llvm::initDebugOptions() {}

>From cf9f8558422ea2b76eb607c063bad0045bb17c0d Mon Sep 17 00:00:00 2001
From: Partouf <partouf at gmail.com>
Date: Fri, 10 May 2024 01:17:30 +0200
Subject: [PATCH 2/3] refactor to address review

---
 llvm/lib/Support/Debug.cpp | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Support/Debug.cpp b/llvm/lib/Support/Debug.cpp
index a04aabe676273..e9a81d797c026 100644
--- a/llvm/lib/Support/Debug.cpp
+++ b/llvm/lib/Support/Debug.cpp
@@ -166,9 +166,9 @@ static void debug_user_sig_handler(void *Cookie) {
 
 /// dbgs - Return a circular-buffered debug stream.
 raw_ostream &llvm::dbgs() {
-  if (LogDebugToStdOut) {
+  if (LogDebugToStdOut)
     return outs();
-  } else {
+
     // Do one-time initialization in a thread-safe way.
     static struct dbgstream {
       circular_raw_ostream strm;
@@ -186,7 +186,6 @@ raw_ostream &llvm::dbgs() {
     } thestrm;
 
     return thestrm.strm;
-  }
 }
 
 #else
@@ -194,11 +193,7 @@ raw_ostream &llvm::dbgs() {
 namespace llvm {
   /// dbgs - Return errs().
   raw_ostream &dbgs() {
-    if (LogDebugToStdOut) {
-      return outs();
-    } else {
-      return errs();
-    }
+    return LogDebugToStdOut ? outs() : errs();
   }
 }
 void llvm::initDebugOptions() {}

>From e02da8a788c3feb9014dab0b52823f6c37e85d03 Mon Sep 17 00:00:00 2001
From: Partouf <partouf at gmail.com>
Date: Fri, 10 May 2024 01:19:49 +0200
Subject: [PATCH 3/3] refactor to address destructor warning

---
 llvm/lib/Support/Debug.cpp | 60 +++++++++++++++++++++++---------------
 1 file changed, 36 insertions(+), 24 deletions(-)

diff --git a/llvm/lib/Support/Debug.cpp b/llvm/lib/Support/Debug.cpp
index e9a81d797c026..382b2272e40bd 100644
--- a/llvm/lib/Support/Debug.cpp
+++ b/llvm/lib/Support/Debug.cpp
@@ -78,10 +78,19 @@ void setCurrentDebugTypes(const char **Types, unsigned Count) {
 }
 } // namespace llvm
 
-cl::opt<bool> LogDebugToStdOut(
-    "debug-to-stdout",
-    llvm::cl::desc("Log debugging to stdout instead of stderr"),
-    cl::init(false), cl::Hidden);
+namespace llvm {
+  struct CreateLogDebugToStdOut {
+    static void *call() {
+      return new cl::opt<bool>(
+        "debug-to-stdout",
+        cl::desc("Log debugging to stdout instead of stderr"),
+        cl::Hidden,
+        cl::init(false));
+    }
+  };
+
+  static ManagedStatic<cl::opt<bool>, CreateLogDebugToStdOut> LogDebugToStdOut;
+}
 
 // All Debug.h functionality is a no-op in NDEBUG mode.
 #ifndef NDEBUG
@@ -151,6 +160,7 @@ void llvm::initDebugOptions() {
   *Debug;
   *DebugBufferSize;
   *DebugOnly;
+  *LogDebugToStdOut;
 }
 
 // Signal handlers - dump debug output on termination.
@@ -166,26 +176,26 @@ static void debug_user_sig_handler(void *Cookie) {
 
 /// dbgs - Return a circular-buffered debug stream.
 raw_ostream &llvm::dbgs() {
-  if (LogDebugToStdOut)
+  if (*LogDebugToStdOut)
     return outs();
 
-    // Do one-time initialization in a thread-safe way.
-    static struct dbgstream {
-      circular_raw_ostream strm;
-
-      dbgstream()
-          : strm(errs(), "*** Debug Log Output ***\n",
-                 (!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
-        if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
-          // TODO: Add a handler for SIGUSER1-type signals so the user can
-          // force a debug dump.
-          sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
-        // Otherwise we've already set the debug stream buffer size to
-        // zero, disabling buffering so it will output directly to errs().
-      }
-    } thestrm;
-
-    return thestrm.strm;
+  // Do one-time initialization in a thread-safe way.
+  static struct dbgstream {
+    circular_raw_ostream strm;
+
+    dbgstream()
+        : strm(errs(), "*** Debug Log Output ***\n",
+                (!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
+      if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
+        // TODO: Add a handler for SIGUSER1-type signals so the user can
+        // force a debug dump.
+        sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
+      // Otherwise we've already set the debug stream buffer size to
+      // zero, disabling buffering so it will output directly to errs().
+    }
+  } thestrm;
+
+  return thestrm.strm;
 }
 
 #else
@@ -193,10 +203,12 @@ raw_ostream &llvm::dbgs() {
 namespace llvm {
   /// dbgs - Return errs().
   raw_ostream &dbgs() {
-    return LogDebugToStdOut ? outs() : errs();
+    return *LogDebugToStdOut ? outs() : errs();
   }
 }
-void llvm::initDebugOptions() {}
+void llvm::initDebugOptions() {
+  *LogDebugToStdOut;
+}
 #endif
 
 /// EnableDebugBuffering - Turn on signal handler installation.



More information about the llvm-commits mailing list