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

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Thu May 9 13:12:04 PDT 2024


================
@@ -161,31 +166,39 @@ 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
 // Avoid "has no symbols" warning.
 namespace llvm {
   /// dbgs - Return errs().
   raw_ostream &dbgs() {
-    return errs();
+    if (LogDebugToStdOut) {
+      return outs();
+    } else {
+      return errs();
+    }
----------------
dwblaikie wrote:

similarly down here, this'd be:
```
if (LogDebugToStdOut)
  return outs();
return errs();
```
Or perhaps a conditional operator?
```
return LogDebugToStdOut ? outs() : errs();
```

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


More information about the llvm-commits mailing list