[Lldb-commits] [lldb] r236174 - Introduce a NullLog class, which ignores all messages.

Zachary Turner zturner at google.com
Wed Apr 29 15:55:28 PDT 2015


Author: zturner
Date: Wed Apr 29 17:55:28 2015
New Revision: 236174

URL: http://llvm.org/viewvc/llvm-project?rev=236174&view=rev
Log:
Introduce a NullLog class, which ignores all messages.

The purpose of this class is so that GetLogIfAllCategoriesSet
can always return an instance of some class, whether it be a real
logging class or a "null" class, which ignores messages.  Code
that is littered with if statements that only log if the pointer
is non-null can get very unwieldy very quickly, so this should
help code readability in such circumstances.

Since I'm in this code anyway, I'm also deleting the
PrintfWithFlags methods, as well as all the flags, since they
appear to be dead code that have been superceded by newer
mechanisms and all the flags are simply ignored.

Added:
    lldb/trunk/include/lldb/Core/NullLog.h
    lldb/trunk/source/Core/NullLog.cpp
Modified:
    lldb/trunk/include/lldb/Core/Log.h
    lldb/trunk/source/Core/CMakeLists.txt
    lldb/trunk/source/Core/Log.cpp

Modified: lldb/trunk/include/lldb/Core/Log.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Log.h?rev=236174&r1=236173&r2=236174&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/Log.h (original)
+++ lldb/trunk/include/lldb/Core/Log.h Wed Apr 29 17:55:28 2015
@@ -26,17 +26,6 @@
 #include "lldb/Core/PluginInterface.h"
 
 //----------------------------------------------------------------------
-// Logging types
-//----------------------------------------------------------------------
-#define LLDB_LOG_FLAG_STDOUT    (1u << 0)
-#define LLDB_LOG_FLAG_STDERR    (1u << 1)
-#define LLDB_LOG_FLAG_FATAL     (1u << 2)
-#define LLDB_LOG_FLAG_ERROR     (1u << 3)
-#define LLDB_LOG_FLAG_WARNING   (1u << 4)
-#define LLDB_LOG_FLAG_DEBUG     (1u << 5)
-#define LLDB_LOG_FLAG_VERBOSE   (1u << 6)
-
-//----------------------------------------------------------------------
 // Logging Options
 //----------------------------------------------------------------------
 #define LLDB_LOG_OPTION_THREADSAFE              (1u << 0)
@@ -61,12 +50,10 @@ public:
     //------------------------------------------------------------------
     // Callback definitions for abstracted plug-in log access.
     //------------------------------------------------------------------
-    typedef void (*DisableCallback) (const char **categories, Stream *feedback_strm);
-    typedef Log * (*EnableCallback) (lldb::StreamSP &log_stream_sp,
-                                     uint32_t log_options,
-                                     const char **categories,
-                                     Stream *feedback_strm);
-    typedef void (*ListCategoriesCallback) (Stream *strm);
+  typedef void (*DisableCallback)(const char **categories, Stream *feedback_strm);
+  typedef Log *(*EnableCallback)(lldb::StreamSP &log_stream_sp, uint32_t log_options, const char **categories,
+                                 Stream *feedback_strm);
+  typedef void (*ListCategoriesCallback)(Stream *strm);
 
     struct Callbacks
     {
@@ -79,86 +66,78 @@ public:
     // Static accessors for logging channels
     //------------------------------------------------------------------
     static void
-    RegisterLogChannel (const ConstString &channel,
-                        const Log::Callbacks &log_callbacks);
+    RegisterLogChannel(const ConstString &channel, const Log::Callbacks &log_callbacks);
 
     static bool
-    UnregisterLogChannel (const ConstString &channel);
+    UnregisterLogChannel(const ConstString &channel);
 
     static bool
-    GetLogChannelCallbacks (const ConstString &channel,
-                            Log::Callbacks &log_callbacks);
-
+    GetLogChannelCallbacks(const ConstString &channel, Log::Callbacks &log_callbacks);
 
     static void
-    EnableAllLogChannels (lldb::StreamSP &log_stream_sp,
-                          uint32_t log_options,
-                          const char **categories,
-                          Stream *feedback_strm);
+    EnableAllLogChannels(lldb::StreamSP &log_stream_sp, uint32_t log_options, const char **categories,
+                         Stream *feedback_strm);
 
     static void
-    DisableAllLogChannels (Stream *feedback_strm);
+    DisableAllLogChannels(Stream *feedback_strm);
 
     static void
-    ListAllLogChannels (Stream *strm);
+    ListAllLogChannels(Stream *strm);
 
     static void
-    Initialize ();
+    Initialize();
 
     static void
-    Terminate ();
-    
+    Terminate();
+
     //------------------------------------------------------------------
     // Auto completion
     //------------------------------------------------------------------
     static void
-    AutoCompleteChannelName (const char *channel_name, 
-                             StringList &matches);
+    AutoCompleteChannelName(const char *channel_name, StringList &matches);
 
     //------------------------------------------------------------------
     // Member functions
     //------------------------------------------------------------------
-    Log ();
+    Log();
 
-    Log (const lldb::StreamSP &stream_sp);
+    Log(const lldb::StreamSP &stream_sp);
 
-    ~Log ();
-
-    void
-    PutCString (const char *cstr);
+    virtual
+    ~Log();
 
-    void
-    Printf (const char *format, ...)  __attribute__ ((format (printf, 2, 3)));
+    virtual void
+    PutCString(const char *cstr);
 
-    void
-    VAPrintf (const char *format, va_list args);
+    virtual void
+    Printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
 
-    void
-    PrintfWithFlags( uint32_t flags, const char *format, ...)  __attribute__ ((format (printf, 3, 4)));
+    virtual void
+    VAPrintf(const char *format, va_list args);
 
-    void
-    LogIf (uint32_t mask, const char *fmt, ...)  __attribute__ ((format (printf, 3, 4)));
+    virtual void
+    LogIf(uint32_t mask, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
 
-    void
-    Debug (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
+    virtual void
+    Debug(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
-    void
-    DebugVerbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
+    virtual void
+    DebugVerbose(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
-    void
-    Error (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
+    virtual void
+    Error(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
-    void
-    FatalError (int err, const char *fmt, ...)  __attribute__ ((format (printf, 3, 4)));
+    virtual void
+    FatalError(int err, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
 
-    void
-    Verbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
+    virtual void
+    Verbose(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
-    void
-    Warning (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
+    virtual void
+    Warning(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
-    void
-    WarningVerbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
+    virtual void
+    WarningVerbose(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 
     Flags &
     GetOptions();
@@ -179,7 +158,7 @@ public:
     GetDebug() const;
 
     void
-    SetStream (const lldb::StreamSP &stream_sp)
+    SetStream(const lldb::StreamSP &stream_sp)
     {
         m_stream_sp = stream_sp;
     }
@@ -192,43 +171,35 @@ protected:
     Flags m_options;
     Flags m_mask_bits;
 
-    void
-    PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args);
-
 private:
-    DISALLOW_COPY_AND_ASSIGN (Log);
+  DISALLOW_COPY_AND_ASSIGN(Log);
 };
 
 
 class LogChannel : public PluginInterface
 {
 public:
-    LogChannel ();
+  LogChannel();
 
-    virtual
-    ~LogChannel ();
+  virtual ~LogChannel();
 
-    static lldb::LogChannelSP
-    FindPlugin (const char *plugin_name);
+  static lldb::LogChannelSP FindPlugin(const char *plugin_name);
 
     // categories is a an array of chars that ends with a NULL element.
-    virtual void
-    Disable (const char **categories, Stream *feedback_strm) = 0;
+  virtual void Disable(const char **categories, Stream *feedback_strm) = 0;
 
-    virtual bool
-    Enable (lldb::StreamSP &log_stream_sp,
-            uint32_t log_options,
-            Stream *feedback_strm,      // Feedback stream for argument errors etc
-            const char **categories) = 0;// The categories to enable within this logging stream, if empty, enable default set
+  virtual bool Enable(
+      lldb::StreamSP &log_stream_sp, uint32_t log_options,
+      Stream *feedback_strm,        // Feedback stream for argument errors etc
+      const char **categories) = 0; // The categories to enable within this logging stream, if empty, enable default set
 
-    virtual void
-    ListCategories (Stream *strm) = 0;
+  virtual void ListCategories(Stream *strm) = 0;
 
 protected:
     std::unique_ptr<Log> m_log_ap;
 
 private:
-    DISALLOW_COPY_AND_ASSIGN (LogChannel);
+  DISALLOW_COPY_AND_ASSIGN(LogChannel);
 };
 
 

Added: lldb/trunk/include/lldb/Core/NullLog.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/NullLog.h?rev=236174&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Core/NullLog.h (added)
+++ lldb/trunk/include/lldb/Core/NullLog.h Wed Apr 29 17:55:28 2015
@@ -0,0 +1,58 @@
+//===-- NullLog.h -----------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_Core_NullLog_H_
+#define liblldb_Core_NullLog_H_
+
+#include "lldb/Core/Log.h"
+
+//----------------------------------------------------------------------
+// Logging Functions
+//----------------------------------------------------------------------
+namespace lldb_private
+{
+
+class NullLog : public Log
+{
+    NullLog(NullLog &) = delete;
+    NullLog &operator=(NullLog &) = delete;
+
+  public:
+    //------------------------------------------------------------------
+    // Member functions
+    //------------------------------------------------------------------
+    NullLog();
+    ~NullLog();
+
+    void PutCString(const char *cstr) override;
+
+    void Printf(const char *format, ...) override __attribute__((format(printf, 2, 3)));
+
+    void VAPrintf(const char *format, va_list args) override;
+
+    void LogIf(uint32_t mask, const char *fmt, ...) override __attribute__((format(printf, 3, 4)));
+
+    void Debug(const char *fmt, ...) override __attribute__((format(printf, 2, 3)));
+
+    void DebugVerbose(const char *fmt, ...) override __attribute__((format(printf, 2, 3)));
+
+    void Error(const char *fmt, ...) override __attribute__((format(printf, 2, 3)));
+
+    void FatalError(int err, const char *fmt, ...) override __attribute__((format(printf, 3, 4)));
+
+    void Verbose(const char *fmt, ...) override __attribute__((format(printf, 2, 3)));
+
+    void Warning(const char *fmt, ...) override __attribute__((format(printf, 2, 3)));
+
+    void WarningVerbose(const char *fmt, ...) override __attribute__((format(printf, 2, 3)));
+};
+
+} // namespace lldb_private
+
+#endif // liblldb_Core_NullLog_H_

Modified: lldb/trunk/source/Core/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/CMakeLists.txt?rev=236174&r1=236173&r2=236174&view=diff
==============================================================================
--- lldb/trunk/source/Core/CMakeLists.txt (original)
+++ lldb/trunk/source/Core/CMakeLists.txt Wed Apr 29 17:55:28 2015
@@ -38,6 +38,7 @@ add_lldb_library(lldbCore
   Module.cpp
   ModuleChild.cpp
   ModuleList.cpp
+  NullLog.cpp
   Opcode.cpp
   PluginManager.cpp
   RegisterValue.cpp

Modified: lldb/trunk/source/Core/Log.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Log.cpp?rev=236174&r1=236173&r2=236174&view=diff
==============================================================================
--- lldb/trunk/source/Core/Log.cpp (original)
+++ lldb/trunk/source/Core/Log.cpp Wed Apr 29 17:55:28 2015
@@ -77,6 +77,23 @@ Log::GetMask() const
     return m_mask_bits;
 }
 
+void
+Log::PutCString(const char *cstr)
+{
+    Printf("%s", cstr);
+}
+
+//----------------------------------------------------------------------
+// Simple variable argument logging with flags.
+//----------------------------------------------------------------------
+void
+Log::Printf(const char *format, ...)
+{
+    va_list args;
+    va_start(args, format);
+    VAPrintf(format, args);
+    va_end(args);
+}
 
 //----------------------------------------------------------------------
 // All logging eventually boils down to this function call. If we have
@@ -84,7 +101,7 @@ Log::GetMask() const
 // a valid file handle, we also log to the file.
 //----------------------------------------------------------------------
 void
-Log::PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args)
+Log::VAPrintf(const char *format, va_list args)
 {
     // Make a copy of our stream shared pointer in case someone disables our
     // log while we are logging and releases the stream
@@ -136,59 +153,20 @@ Log::PrintfWithFlagsVarArg (uint32_t fla
     }
 }
 
-
-void
-Log::PutCString (const char *cstr)
-{
-    Printf ("%s", cstr);
-}
-
-
-//----------------------------------------------------------------------
-// Simple variable argument logging with flags.
-//----------------------------------------------------------------------
-void
-Log::Printf(const char *format, ...)
-{
-    va_list args;
-    va_start (args, format);
-    PrintfWithFlagsVarArg (0, format, args);
-    va_end (args);
-}
-
-void
-Log::VAPrintf (const char *format, va_list args)
-{
-    PrintfWithFlagsVarArg (0, format, args);
-}
-
-
-//----------------------------------------------------------------------
-// Simple variable argument logging with flags.
-//----------------------------------------------------------------------
-void
-Log::PrintfWithFlags (uint32_t flags, const char *format, ...)
-{
-    va_list args;
-    va_start (args, format);
-    PrintfWithFlagsVarArg (flags, format, args);
-    va_end (args);
-}
-
 //----------------------------------------------------------------------
 // Print debug strings if and only if the global debug option is set to
 // a non-zero value.
 //----------------------------------------------------------------------
 void
-Log::Debug (const char *format, ...)
+Log::Debug(const char *format, ...)
 {
-    if (GetOptions().Test(LLDB_LOG_OPTION_DEBUG))
-    {
-        va_list args;
-        va_start (args, format);
-        PrintfWithFlagsVarArg (LLDB_LOG_FLAG_DEBUG, format, args);
-        va_end (args);
-    }
+    if (!GetOptions().Test(LLDB_LOG_OPTION_DEBUG))
+        return;
+
+    va_list args;
+    va_start(args, format);
+    VAPrintf(format, args);
+    va_end(args);
 }
 
 
@@ -197,15 +175,15 @@ Log::Debug (const char *format, ...)
 // a non-zero value.
 //----------------------------------------------------------------------
 void
-Log::DebugVerbose (const char *format, ...)
+Log::DebugVerbose(const char *format, ...)
 {
-    if (GetOptions().AllSet (LLDB_LOG_OPTION_DEBUG | LLDB_LOG_OPTION_VERBOSE))
-    {
-        va_list args;
-        va_start (args, format);
-        PrintfWithFlagsVarArg (LLDB_LOG_FLAG_DEBUG | LLDB_LOG_FLAG_VERBOSE, format, args);
-        va_end (args);
-    }
+    if (!GetOptions().AllSet(LLDB_LOG_OPTION_DEBUG | LLDB_LOG_OPTION_VERBOSE))
+        return;
+
+    va_list args;
+    va_start(args, format);
+    VAPrintf(format, args);
+    va_end(args);
 }
 
 
@@ -213,34 +191,34 @@ Log::DebugVerbose (const char *format, .
 // Log only if all of the bits are set
 //----------------------------------------------------------------------
 void
-Log::LogIf (uint32_t bits, const char *format, ...)
+Log::LogIf(uint32_t bits, const char *format, ...)
 {
-    if (m_options.AllSet (bits))
-    {
-        va_list args;
-        va_start (args, format);
-        PrintfWithFlagsVarArg (0, format, args);
-        va_end (args);
-    }
+    if (!m_options.AllSet(bits))
+        return;
+
+    va_list args;
+    va_start(args, format);
+    VAPrintf(format, args);
+    va_end(args);
 }
 
 //----------------------------------------------------------------------
 // Printing of errors that are not fatal.
 //----------------------------------------------------------------------
 void
-Log::Error (const char *format, ...)
+Log::Error(const char *format, ...)
 {
-    char *arg_msg = NULL;
+    char *arg_msg = nullptr;
     va_list args;
-    va_start (args, format);
-    ::vasprintf (&arg_msg, format, args);
-    va_end (args);
+    va_start(args, format);
+    ::vasprintf(&arg_msg, format, args);
+    va_end(args);
 
-    if (arg_msg != NULL)
-    {
-        PrintfWithFlags (LLDB_LOG_FLAG_ERROR, "error: %s", arg_msg);
-        free (arg_msg);
-    }
+    if (arg_msg == nullptr)
+        return;
+
+    VAPrintf("error: %s", arg_msg);
+    free(arg_msg);
 }
 
 //----------------------------------------------------------------------
@@ -248,20 +226,20 @@ Log::Error (const char *format, ...)
 // immediately.
 //----------------------------------------------------------------------
 void
-Log::FatalError (int err, const char *format, ...)
+Log::FatalError(int err, const char *format, ...)
 {
-    char *arg_msg = NULL;
+    char *arg_msg = nullptr;
     va_list args;
-    va_start (args, format);
-    ::vasprintf (&arg_msg, format, args);
-    va_end (args);
+    va_start(args, format);
+    ::vasprintf(&arg_msg, format, args);
+    va_end(args);
 
-    if (arg_msg != NULL)
+    if (arg_msg != nullptr)
     {
-        PrintfWithFlags (LLDB_LOG_FLAG_ERROR | LLDB_LOG_FLAG_FATAL, "error: %s", arg_msg);
-        ::free (arg_msg);
+        VAPrintf("error: %s", arg_msg);
+        ::free(arg_msg);
     }
-    ::exit (err);
+    ::exit(err);
 }
 
 
@@ -270,15 +248,15 @@ Log::FatalError (int err, const char *fo
 // enabled.
 //----------------------------------------------------------------------
 void
-Log::Verbose (const char *format, ...)
+Log::Verbose(const char *format, ...)
 {
-    if (m_options.Test(LLDB_LOG_OPTION_VERBOSE))
-    {
-        va_list args;
-        va_start (args, format);
-        PrintfWithFlagsVarArg (LLDB_LOG_FLAG_VERBOSE, format, args);
-        va_end (args);
-    }
+    if (!m_options.Test(LLDB_LOG_OPTION_VERBOSE))
+        return;
+
+    va_list args;
+    va_start(args, format);
+    VAPrintf(format, args);
+    va_end(args);
 }
 
 //----------------------------------------------------------------------
@@ -286,40 +264,40 @@ Log::Verbose (const char *format, ...)
 // enabled.
 //----------------------------------------------------------------------
 void
-Log::WarningVerbose (const char *format, ...)
+Log::WarningVerbose(const char *format, ...)
 {
-    if (m_options.Test(LLDB_LOG_OPTION_VERBOSE))
-    {
-        char *arg_msg = NULL;
-        va_list args;
-        va_start (args, format);
-        ::vasprintf (&arg_msg, format, args);
-        va_end (args);
+    if (!m_options.Test(LLDB_LOG_OPTION_VERBOSE))
+        return;
 
-        if (arg_msg != NULL)
-        {
-            PrintfWithFlags (LLDB_LOG_FLAG_WARNING | LLDB_LOG_FLAG_VERBOSE, "warning: %s", arg_msg);
-            free (arg_msg);
-        }
-    }
+    char *arg_msg = nullptr;
+    va_list args;
+    va_start(args, format);
+    ::vasprintf(&arg_msg, format, args);
+    va_end(args);
+
+    if (arg_msg == nullptr)
+        return;
+
+    VAPrintf("warning: %s", arg_msg);
+    free(arg_msg);
 }
 //----------------------------------------------------------------------
 // Printing of warnings that are not fatal.
 //----------------------------------------------------------------------
 void
-Log::Warning (const char *format, ...)
+Log::Warning(const char *format, ...)
 {
-    char *arg_msg = NULL;
+    char *arg_msg = nullptr;
     va_list args;
-    va_start (args, format);
-    ::vasprintf (&arg_msg, format, args);
-    va_end (args);
+    va_start(args, format);
+    ::vasprintf(&arg_msg, format, args);
+    va_end(args);
 
-    if (arg_msg != NULL)
-    {
-        PrintfWithFlags (LLDB_LOG_FLAG_WARNING, "warning: %s", arg_msg);
-        free (arg_msg);
-    }
+    if (arg_msg == nullptr)
+        return;
+
+    VAPrintf("warning: %s", arg_msg);
+    free(arg_msg);
 }
 
 typedef std::map <ConstString, Log::Callbacks> CallbackMap;

Added: lldb/trunk/source/Core/NullLog.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/NullLog.cpp?rev=236174&view=auto
==============================================================================
--- lldb/trunk/source/Core/NullLog.cpp (added)
+++ lldb/trunk/source/Core/NullLog.cpp Wed Apr 29 17:55:28 2015
@@ -0,0 +1,74 @@
+//===-- NullLog.cpp ---------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Core/NullLog.h"
+
+using namespace lldb_private;
+
+NullLog::NullLog()
+{
+}
+NullLog::~NullLog()
+{
+}
+
+void
+NullLog::PutCString(const char *cstr)
+{
+}
+
+void
+NullLog::Printf(const char *format, ...)
+{
+}
+
+void
+NullLog::VAPrintf(const char *format, va_list args)
+{
+}
+
+void
+NullLog::LogIf(uint32_t mask, const char *fmt, ...)
+{
+}
+
+void
+NullLog::Debug(const char *fmt, ...)
+{
+}
+
+void
+NullLog::DebugVerbose(const char *fmt, ...)
+{
+}
+
+void
+NullLog::Error(const char *fmt, ...)
+{
+}
+
+void
+NullLog::FatalError(int err, const char *fmt, ...)
+{
+}
+
+void
+NullLog::Verbose(const char *fmt, ...)
+{
+}
+
+void
+NullLog::Warning(const char *fmt, ...)
+{
+}
+
+void
+NullLog::WarningVerbose(const char *fmt, ...)
+{
+}





More information about the lldb-commits mailing list