Changeset 191474 in webkit


Ignore:
Timestamp:
Oct 22, 2015, 2:29:54 PM (10 years ago)
Author:
Simon Fraser
Message:

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):

Location:
trunk/Source/WebCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r191473 r191474  
     12015-10-22  Simon Fraser  <simon.fraser@apple.com>
     2
     3        Add ways to log to log channels via a functional syntax, and via a TextStream
     4        https://bugs.webkit.org/show_bug.cgi?id=150472
     5 
     6        Reviewed by Tim Horton.
     7 
     8        Make it possible to write to a WTFLogChannel with a std::function that returns
     9        a const char*, and with stream syntax.
     10 
     11        Enhance TextStream to allow it to generate single-line output.
     12
     13        * platform/Logging.cpp:
     14        (WebCore::logFunctionResult):
     15        * platform/Logging.h:
     16        * platform/text/TextStream.cpp:
     17        (WebCore::TextStream::startGroup):
     18        (WebCore::TextStream::endGroup):
     19        (WebCore::TextStream::nextLine):
     20        (WebCore::TextStream::writeIndent):
     21        * platform/text/TextStream.h:
     22        (WebCore::TextStream::TextStream):
     23
    1242015-10-22  Alex Christensen  <achristensen@webkit.org>
    225
  • trunk/Source/WebCore/platform/Logging.cpp

    r191450 r191474  
    8383#endif
    8484
     85void logFunctionResult(WTFLogChannel* channel, std::function<const char*()> function)
     86{
     87    WTFLog(channel, "%s", function());
    8588}
    8689
     90} // namespace WebCore
     91
    8792#endif // !LOG_DISABLED
  • trunk/Source/WebCore/platform/Logging.h

    r191439 r191474  
    3131#include <wtf/Forward.h>
    3232
    33 #if !LOG_DISABLED
     33namespace WebCore {
     34
     35#if LOG_DISABLED
     36
     37#define LOG_RESULT(channel, function) ((void)0)
     38#define LOG_WITH_STREAM(channel, commands) ((void)0)
     39
     40#else
    3441
    3542#ifndef LOG_CHANNEL_PREFIX
    3643#define LOG_CHANNEL_PREFIX Log
    3744#endif
    38 
    39 namespace WebCore {
    4045
    4146#define WEBCORE_LOG_CHANNELS(M) \
     
    8893#undef DECLARE_LOG_CHANNEL
    8994
    90     String logLevelString();
    91     bool isLogChannelEnabled(const String& name);
    92     WEBCORE_EXPORT void initializeLoggingChannelsIfNecessary();
     95String logLevelString();
     96bool isLogChannelEnabled(const String& name);
     97WEBCORE_EXPORT void initializeLoggingChannelsIfNecessary();
    9398#ifndef NDEBUG
    94     void registerNotifyCallback(const String& notifyID, std::function<void()> callback);
     99void registerNotifyCallback(const String& notifyID, std::function<void()> callback);
    95100#endif
    96 }
     101
     102void logFunctionResult(WTFLogChannel*, std::function<const char*()>);
     103
     104#define LOG_RESULT(channel, function) logFunctionResult(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), function)
     105
     106#define LOG_WITH_STREAM(channel, commands) logFunctionResult(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), \
     107    [&]() { \
     108        TextStream stream(TextStream::LineMode::SingleLine); \
     109        commands; \
     110        return stream.release().utf8().data(); \
     111    });
    97112
    98113#endif // !LOG_DISABLED
    99114
     115} // namespace WebCore
     116
    100117#endif // Logging_h
  • trunk/Source/WebCore/platform/text/TextStream.cpp

    r191243 r191474  
    143143{
    144144    TextStream& ts = *this;
    145     ts << "\n";
    146     ts.writeIndent();
    147     ts << "(";
    148     ts.increaseIndent();
     145
     146    if (m_multiLineMode) {
     147        ts << "\n";
     148        ts.writeIndent();
     149        ts << "(";
     150        ts.increaseIndent();
     151    } else
     152        ts << " (";
    149153}
    150154
     
    153157    TextStream& ts = *this;
    154158    ts << ")";
    155     ts.decreaseIndent();
     159    if (m_multiLineMode)
     160        ts.decreaseIndent();
    156161}
    157162
     
    159164{
    160165    TextStream& ts = *this;
    161     ts << "\n";
    162     ts.writeIndent();
     166    if (m_multiLineMode) {
     167        ts << "\n";
     168        ts.writeIndent();
     169    } else
     170        ts << " ";
    163171}
    164172
    165173void TextStream::writeIndent()
    166174{
    167     WebCore::writeIndent(*this, m_indent);
     175    if (m_multiLineMode)
     176        WebCore::writeIndent(*this, m_indent);
    168177}
    169178
  • trunk/Source/WebCore/platform/text/TextStream.h

    r191310 r191474  
    3232namespace WebCore {
    3333
    34 class FloatPoint;
    35 class IntPoint;
    36 class LayoutPoint;
    37 class LayoutRect;
    3834class LayoutUnit;
    3935
    40 // FIXME: this should move to platform/
    4136class TextStream {
    4237public:
     
    4540        double value;
    4641    };
     42   
     43    enum class LineMode { SingleLine, MultipleLine };
     44    TextStream(LineMode lineMode = LineMode::MultipleLine)
     45        : m_multiLineMode(lineMode == LineMode::MultipleLine)
     46    {
     47    }
    4748
    4849    WEBCORE_EXPORT TextStream& operator<<(bool);
     
    101102    StringBuilder m_text;
    102103    int m_indent { 0 };
     104    bool m_multiLineMode { true };
    103105};
    104106
Note: See TracChangeset for help on using the changeset viewer.