[pkg-d-commits] [ldc] 124/211: Add code for logging within D code.

Matthias Klumpp mak at moszumanska.debian.org
Sun Apr 23 22:36:16 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.1.0
in repository ldc.

commit fe8cf9151093acf36c35da7af3afc4f7e8900d37
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Tue Nov 1 22:17:58 2016 +0900

    Add code for logging within D code.
---
 gen/logger.cpp | 19 ++++++++------
 gen/logger.d   | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gen/logger.h   | 10 +++++---
 3 files changed, 97 insertions(+), 11 deletions(-)

diff --git a/gen/logger.cpp b/gen/logger.cpp
index b469d90..1bf2843 100644
--- a/gen/logger.cpp
+++ b/gen/logger.cpp
@@ -42,29 +42,32 @@ void Stream::writeValue(std::ostream &OS, const llvm::Value &V) {
   }
 }
 
+// This variable is pulled out of the Logger namespace to work around D bug 15576.
+// https://issues.dlang.org/show_bug.cgi?id=15576
+bool _Logger_enabled;
+
 namespace Logger {
 static std::string indent_str;
-bool _enabled;
 
 static llvm::cl::opt<bool, true>
     enabledopt("vv", llvm::cl::desc("Print front-end/glue code debug log"),
-               llvm::cl::location(_enabled), llvm::cl::ZeroOrMore);
+               llvm::cl::location(_Logger_enabled), llvm::cl::ZeroOrMore);
 
 void indent() {
-  if (_enabled) {
+  if (_Logger_enabled) {
     indent_str += "* ";
   }
 }
 
 void undent() {
-  if (_enabled) {
+  if (_Logger_enabled) {
     assert(!indent_str.empty());
     indent_str.resize(indent_str.size() - 2);
   }
 }
 
 Stream cout() {
-  if (_enabled) {
+  if (_Logger_enabled) {
     return Stream(std::cout << indent_str);
   }
   return Stream(nullptr);
@@ -89,8 +92,10 @@ static inline void search_and_replace(std::string &str, const std::string &what,
 #define WORKAROUND_C99_SPECIFIERS_BUG(f)
 #endif
 
+void printIndentation() { printf("%s", indent_str.c_str()); }
+
 void println(const char *fmt, ...) {
-  if (_enabled) {
+  if (_Logger_enabled) {
     printf("%s", indent_str.c_str());
     va_list va;
     va_start(va, fmt);
@@ -101,7 +106,7 @@ void println(const char *fmt, ...) {
   }
 }
 void print(const char *fmt, ...) {
-  if (_enabled) {
+  if (_Logger_enabled) {
     printf("%s", indent_str.c_str());
     va_list va;
     va_start(va, fmt);
diff --git a/gen/logger.d b/gen/logger.d
new file mode 100644
index 0000000..53a3ac5
--- /dev/null
+++ b/gen/logger.d
@@ -0,0 +1,79 @@
+//===-- gen/logger.d - Codegen debug logging ----------------------*- D -*-===//
+//
+//                         LDC – the LLVM D compiler
+//
+// This file is distributed under the BSD-style LDC license. See the LICENSE
+// file for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// D implementation of the functionality of logger.{h/cpp}.
+//
+//===----------------------------------------------------------------------===//
+
+module gen.logger;
+
+private extern (C++) extern __gshared bool _Logger_enabled;
+extern (C++, Logger)
+{
+    void indent();
+    void undent();
+    private void printIndentation();
+}
+
+struct Log
+{
+    static bool enabled()
+    {
+        return _Logger_enabled;
+    }
+
+    static void indent()
+    {
+        if (enabled())
+            .indent();
+    }
+
+    static void undent()
+    {
+        if (enabled())
+            .undent();
+    }
+
+    // Usage:  auto _ = Log.newScope();
+    static auto newScope()
+    {
+        struct ScopeExitUndenter
+        {
+            ~this()
+            {
+                Logger.undent();
+            }
+        }
+
+        Logger.indent();
+        return ScopeExitUndenter();
+    }
+
+    static void printfln(T...)(T args)
+    {
+        static import std.stdio;
+
+        if (enabled())
+        {
+            printIndentation();
+            std.stdio.writefln(args);
+        }
+    }
+
+    static void printf(T...)(T args)
+    {
+        static import std.stdio;
+
+        if (enabled())
+        {
+            printIndentation();
+            std.stdio.writef(args);
+        }
+    }
+}
diff --git a/gen/logger.h b/gen/logger.h
index 883acdd..2de6b80 100644
--- a/gen/logger.h
+++ b/gen/logger.h
@@ -92,17 +92,19 @@ private:
   short sfinae_bait(...);
 };
 
+extern bool _Logger_enabled;
+
 namespace Logger {
-extern bool _enabled;
 
 void indent();
 void undent();
 Stream cout();
+void printIndentation();
 void println(const char *fmt, ...) IS_PRINTF(1);
 void print(const char *fmt, ...) IS_PRINTF(1);
-inline void enable() { _enabled = true; }
-inline void disable() { _enabled = false; }
-inline bool enabled() { return _enabled; }
+inline void enable() { _Logger_enabled = true; }
+inline void disable() { _Logger_enabled = false; }
+inline bool enabled() { return _Logger_enabled; }
 
 void attention(Loc loc, const char *fmt, ...) IS_PRINTF(2);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list