Add ways to log to log channels via a functional syntax, and via a TextStream
authorsimon.fraser@apple.com <simon.fraser@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 22 Oct 2015 21:29:54 +0000 (21:29 +0000)
committersimon.fraser@apple.com <simon.fraser@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 22 Oct 2015 21:29:54 +0000 (21:29 +0000)
https://bugs.webkit.org/show_bug.cgi?id=150472

Reviewed by Tim Horton.

Make it possible to write to a WTFLogChannel with a std::function that returns
a const char*, and with stream syntax.

Enhance TextStream to allow it to generate single-line output.

* platform/Logging.cpp:
(WebCore::logFunctionResult):
* platform/Logging.h:
* platform/text/TextStream.cpp:
(WebCore::TextStream::startGroup):
(WebCore::TextStream::endGroup):
(WebCore::TextStream::nextLine):
(WebCore::TextStream::writeIndent):
* platform/text/TextStream.h:
(WebCore::TextStream::TextStream):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@191474 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/Logging.cpp
Source/WebCore/platform/Logging.h
Source/WebCore/platform/text/TextStream.cpp
Source/WebCore/platform/text/TextStream.h

index 97840d1..ce560f5 100644 (file)
@@ -1,3 +1,26 @@
+2015-10-22  Simon Fraser  <simon.fraser@apple.com>
+
+        Add ways to log to log channels via a functional syntax, and via a TextStream
+        https://bugs.webkit.org/show_bug.cgi?id=150472
+        Reviewed by Tim Horton.
+        Make it possible to write to a WTFLogChannel with a std::function that returns
+        a const char*, and with stream syntax.
+        Enhance TextStream to allow it to generate single-line output.
+
+        * platform/Logging.cpp:
+        (WebCore::logFunctionResult):
+        * platform/Logging.h:
+        * platform/text/TextStream.cpp:
+        (WebCore::TextStream::startGroup):
+        (WebCore::TextStream::endGroup):
+        (WebCore::TextStream::nextLine):
+        (WebCore::TextStream::writeIndent):
+        * platform/text/TextStream.h:
+        (WebCore::TextStream::TextStream):
+
 2015-10-22  Alex Christensen  <achristensen@webkit.org>
 
         Progress towards CMake on Mac
index 28510db..0003b78 100644 (file)
@@ -82,6 +82,11 @@ void registerNotifyCallback(const String& notifyID, std::function<void()> callba
 }
 #endif
 
+void logFunctionResult(WTFLogChannel* channel, std::function<const char*()> function)
+{
+    WTFLog(channel, "%s", function());
 }
 
+} // namespace WebCore
+
 #endif // !LOG_DISABLED
index e92c1e6..9c48555 100644 (file)
 #include <wtf/Assertions.h>
 #include <wtf/Forward.h>
 
-#if !LOG_DISABLED
+namespace WebCore {
+
+#if LOG_DISABLED
+
+#define LOG_RESULT(channel, function) ((void)0)
+#define LOG_WITH_STREAM(channel, commands) ((void)0)
+
+#else
 
 #ifndef LOG_CHANNEL_PREFIX
 #define LOG_CHANNEL_PREFIX Log
 #endif
 
-namespace WebCore {
-
 #define WEBCORE_LOG_CHANNELS(M) \
     M(Animations) \
     M(Archives) \
@@ -87,14 +92,26 @@ WEBCORE_LOG_CHANNELS(DECLARE_LOG_CHANNEL)
 
 #undef DECLARE_LOG_CHANNEL
 
-    String logLevelString();
-    bool isLogChannelEnabled(const String& name);
-    WEBCORE_EXPORT void initializeLoggingChannelsIfNecessary();
+String logLevelString();
+bool isLogChannelEnabled(const String& name);
+WEBCORE_EXPORT void initializeLoggingChannelsIfNecessary();
 #ifndef NDEBUG
-    void registerNotifyCallback(const String& notifyID, std::function<void()> callback);
+void registerNotifyCallback(const String& notifyID, std::function<void()> callback);
 #endif
-}
+
+void logFunctionResult(WTFLogChannel*, std::function<const char*()>);
+
+#define LOG_RESULT(channel, function) logFunctionResult(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), function)
+
+#define LOG_WITH_STREAM(channel, commands) logFunctionResult(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), \
+    [&]() { \
+        TextStream stream(TextStream::LineMode::SingleLine); \
+        commands; \
+        return stream.release().utf8().data(); \
+    });
 
 #endif // !LOG_DISABLED
 
+} // namespace WebCore
+
 #endif // Logging_h
index e5e4c58..63e57d5 100644 (file)
@@ -142,29 +142,38 @@ String TextStream::release()
 void TextStream::startGroup()
 {
     TextStream& ts = *this;
-    ts << "\n";
-    ts.writeIndent();
-    ts << "(";
-    ts.increaseIndent();
+
+    if (m_multiLineMode) {
+        ts << "\n";
+        ts.writeIndent();
+        ts << "(";
+        ts.increaseIndent();
+    } else
+        ts << " (";
 }
 
 void TextStream::endGroup()
 {
     TextStream& ts = *this;
     ts << ")";
-    ts.decreaseIndent();
+    if (m_multiLineMode)
+        ts.decreaseIndent();
 }
 
 void TextStream::nextLine()
 {
     TextStream& ts = *this;
-    ts << "\n";
-    ts.writeIndent();
+    if (m_multiLineMode) {
+        ts << "\n";
+        ts.writeIndent();
+    } else
+        ts << " ";
 }
 
 void TextStream::writeIndent()
 {
-    WebCore::writeIndent(*this, m_indent);
+    if (m_multiLineMode)
+        WebCore::writeIndent(*this, m_indent);
 }
 
 void writeIndent(TextStream& ts, int indent)
index 14aec46..87eecb3 100644 (file)
 
 namespace WebCore {
 
-class FloatPoint;
-class IntPoint;
-class LayoutPoint;
-class LayoutRect;
 class LayoutUnit;
 
-// FIXME: this should move to platform/
 class TextStream {
 public:
     struct FormatNumberRespectingIntegers {
         FormatNumberRespectingIntegers(double number) : value(number) { }
         double value;
     };
+    
+    enum class LineMode { SingleLine, MultipleLine };
+    TextStream(LineMode lineMode = LineMode::MultipleLine)
+        : m_multiLineMode(lineMode == LineMode::MultipleLine)
+    {
+    }
 
     WEBCORE_EXPORT TextStream& operator<<(bool);
     WEBCORE_EXPORT TextStream& operator<<(int);
@@ -100,6 +101,7 @@ public:
 private:
     StringBuilder m_text;
     int m_indent { 0 };
+    bool m_multiLineMode { true };
 };
 
 template<typename Item>