Changeset 221288 in webkit


Ignore:
Timestamp:
Aug 28, 2017 7:59:18 PM (7 years ago)
Author:
eric.carlson@apple.com
Message:

Logger should use makeString instead of String::format
https://bugs.webkit.org/show_bug.cgi?id=176035

Reviewed by Jer Noble.

Source/WebCore/PAL:

  • pal/Logger.h:

(PAL::LogArgument::toString):
(PAL::Logger::logAlways):
(PAL::Logger::error):
(PAL::Logger::warning):
(PAL::Logger::notice):
(PAL::Logger::info):
(PAL::Logger::debug):
(PAL::Logger::MethodAndPointer::MethodAndPointer):
(PAL::Logger::log):
(PAL::LogArgument<Logger::MethodAndPointer>::toString):

Tools:

  • TestWebKitAPI/Tests/WebCore/Logging.cpp:

(TestWebKitAPI::TEST_F): Update test.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/PAL/ChangeLog

    r221276 r221288  
     12017-08-28  Eric Carlson  <eric.carlson@apple.com>
     2
     3        Logger should use makeString instead of String::format
     4        https://bugs.webkit.org/show_bug.cgi?id=176035
     5
     6        Reviewed by Jer Noble.
     7
     8        * pal/Logger.h:
     9        (PAL::LogArgument::toString):
     10        (PAL::Logger::logAlways):
     11        (PAL::Logger::error):
     12        (PAL::Logger::warning):
     13        (PAL::Logger::notice):
     14        (PAL::Logger::info):
     15        (PAL::Logger::debug):
     16        (PAL::Logger::MethodAndPointer::MethodAndPointer):
     17        (PAL::Logger::log):
     18        (PAL::LogArgument<Logger::MethodAndPointer>::toString):
     19
    1202017-08-28  Andy Estes  <aestes@apple.com>
    221
  • trunk/Source/WebCore/PAL/pal/Logger.h

    r221221 r221288  
    2121 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    2222 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
     23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2424 */
    2525
     
    2727
    2828#include <wtf/Assertions.h>
     29#include <wtf/HexNumber.h>
    2930#include <wtf/Noncopyable.h>
    3031#include <wtf/RefCounted.h>
    3132#include <wtf/RefPtr.h>
     33#include <wtf/text/StringBuilder.h>
    3234#include <wtf/text/WTFString.h>
    3335
    3436namespace PAL {
     37
     38template<typename T>
     39struct LogArgument {
     40    template<typename U = T> static typename std::enable_if<std::is_same<U, int>::value, String>::type toString(int argument) { return String::number(argument); }
     41    template<typename U = T> static typename std::enable_if<std::is_same<U, float>::value, String>::type toString(float argument) { return String::number(argument); }
     42    template<typename U = T> static typename std::enable_if<std::is_same<U, double>::value, String>::type toString(double argument) { return String::number(argument); }
     43    template<typename U = T> static typename std::enable_if<std::is_same<U, String>::value, String>::type toString(String argument) { return argument; }
     44    template<typename U = T> static typename std::enable_if<std::is_same<U, const char*>::value, String>::type toString(const char* argument) { return String(argument); }
     45    template<size_t length> static String toString(const char (&argument)[length]) { return String(argument); }
     46};
    3547
    3648class Logger : public RefCounted<Logger> {
     
    4355
    4456    template<typename... Arguments>
    45     inline void logAlways(WTFLogChannel& channel, const char* format, const Arguments&... arguments) const WTF_ATTRIBUTE_PRINTF(3, 0)
     57    inline void logAlways(WTFLogChannel& channel, const Arguments&... arguments)
    4658    {
    4759#if RELEASE_LOG_DISABLED
     
    4961        //  on some systems, so don't allow it.
    5062        UNUSED_PARAM(channel);
    51         UNUSED_PARAM(format);
    5263#else
    5364        if (!willLog(channel, WTFLogLevelAlways))
    5465            return;
    5566
    56         log(channel, format, arguments...);
     67        log(channel, arguments...);
    5768#endif
    5869    }
    5970
    6071    template<typename... Arguments>
    61     inline void error(WTFLogChannel& channel, const char* format, const Arguments&... arguments) const WTF_ATTRIBUTE_PRINTF(3, 0)
     72    inline void error(WTFLogChannel& channel, const Arguments&... arguments)
    6273    {
    6374        if (!willLog(channel, WTFLogLevelError))
    6475            return;
    6576
    66         log(channel, format, arguments...);
     77        log(channel, arguments...);
    6778    }
    6879
    6980    template<typename... Arguments>
    70     inline void warning(WTFLogChannel& channel, const char* format, const Arguments&... arguments) const WTF_ATTRIBUTE_PRINTF(3, 0)
     81    inline void warning(WTFLogChannel& channel, const Arguments&... arguments)
    7182    {
    7283        if (!willLog(channel, WTFLogLevelWarning))
    7384            return;
    7485
    75         log(channel, format, arguments...);
     86        log(channel, arguments...);
    7687    }
    7788
    7889    template<typename... Arguments>
    79     inline void notice(WTFLogChannel& channel, const char* format, const Arguments&... arguments) const WTF_ATTRIBUTE_PRINTF(3, 0)
     90    inline void notice(WTFLogChannel& channel, const Arguments&... arguments)
    8091    {
    8192        if (!willLog(channel, WTFLogLevelNotice))
    8293            return;
    8394
    84         log(channel, format, arguments...);
     95        log(channel, arguments...);
    8596    }
    8697
    8798    template<typename... Arguments>
    88     inline void info(WTFLogChannel& channel, const char* format, const Arguments&... arguments) const WTF_ATTRIBUTE_PRINTF(3, 0)
     99    inline void info(WTFLogChannel& channel, const Arguments&... arguments)
    89100    {
    90101        if (!willLog(channel, WTFLogLevelInfo))
    91102            return;
    92103
    93         log(channel, format, arguments...);
     104        log(channel, arguments...);
    94105    }
    95106
    96107    template<typename... Arguments>
    97     inline void debug(WTFLogChannel& channel, const char* format, const Arguments&... arguments) const WTF_ATTRIBUTE_PRINTF(3, 0)
     108    inline void debug(WTFLogChannel& channel, const Arguments&... arguments)
    98109    {
    99110        if (!willLog(channel, WTFLogLevelDebug))
    100111            return;
    101112
    102         log(channel, format, arguments...);
     113        log(channel, arguments...);
    103114    }
    104115
     
    122133    }
    123134
     135    struct MethodAndPointer {
     136        MethodAndPointer(const char* methodName, void* objectPtr)
     137            : methodName(methodName)
     138            , objectPtr(reinterpret_cast<uintptr_t>(objectPtr))
     139        {
     140        }
     141
     142        const char* methodName;
     143        uintptr_t objectPtr;
     144    };
     145
    124146private:
    125147    Logger(const void* owner) { m_owner = owner; }
    126148
    127     static inline void log(WTFLogChannel& channel, const char* format, ...)
     149    template<typename... Argument>
     150    static inline void log(WTFLogChannel& channel, const Argument&... arguments)
    128151    {
    129         va_list arguments;
    130         va_start(arguments, format);
    131 
    132 #if COMPILER(GCC_OR_CLANG)
    133 #pragma GCC diagnostic push
    134 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
    135 #endif
    136         String string = String::formatWithArguments(format, arguments);
    137 #if COMPILER(GCC_OR_CLANG)
    138 #pragma GCC diagnostic pop
    139 #endif
     152        String string = makeString(LogArgument<Argument>::toString(arguments)...);
    140153
    141154#if RELEASE_LOG_DISABLED
     
    144157        os_log(channel.osLogChannel, "%{public}s", string.utf8().data());
    145158#endif
    146 
    147         va_end(arguments);
    148159    }
    149160
     
    152163};
    153164
     165template <>
     166struct LogArgument<Logger::MethodAndPointer> {
     167    static String toString(const Logger::MethodAndPointer& value)
     168    {
     169        StringBuilder builder;
     170        builder.append(value.methodName);
     171        builder.appendLiteral("(0x");
     172        appendUnsigned64AsHex(value.objectPtr, builder);
     173        builder.appendLiteral(") ");
     174        return builder.toString();
     175    }
     176};
     177
    154178} // namespace PAL
    155179
  • trunk/Tools/ChangeLog

    r221284 r221288  
     12017-08-28  Eric Carlson  <eric.carlson@apple.com>
     2
     3        Logger should use makeString instead of String::format
     4        https://bugs.webkit.org/show_bug.cgi?id=176035
     5
     6        Reviewed by Jer Noble.
     7
     8        * TestWebKitAPI/Tests/WebCore/Logging.cpp:
     9        (TestWebKitAPI::TEST_F): Update test.
     10
    1112017-08-28  Michael Catanzaro  <mcatanzaro@igalia.com>
    212
  • trunk/Tools/TestWebKitAPI/Tests/WebCore/Logging.cpp

    r221221 r221288  
    268268
    269269    WTFSetLogChannelLevel(&TestChannel1, WTFLogLevelError);
    270     logger->error(TestChannel1, "%s %s", "What,", "ridden on a horse?");
     270    logger->error(TestChannel1, "You're using coconuts!");
     271    EXPECT_TRUE(output().contains("You're using coconuts!", false));
     272    logger->warning(TestChannel1, "You're using coconuts!");
     273    EXPECT_EQ(0u, output().length());
     274    logger->notice(TestChannel1, "You're using coconuts!");
     275    EXPECT_EQ(0u, output().length());
     276    logger->info(TestChannel1, "You're using coconuts!");
     277    EXPECT_EQ(0u, output().length());
     278    logger->debug(TestChannel1, "You're using coconuts!");
     279    EXPECT_EQ(0u, output().length());
     280
     281    logger->error(TestChannel1, Logger::MethodAndPointer("LoggingTest::Logger", this) , ": test output");
     282    EXPECT_TRUE(output().contains("LoggingTest::Logger(", false));
     283
     284    logger->error(TestChannel1, "What is ", 1, " + " , 12.5F, "?");
     285    EXPECT_TRUE(output().contains("What is 1 + 12.5?", false));
     286
     287    logger->error(TestChannel1, "What, ", "ridden on a horse?");
    271288    EXPECT_TRUE(output().contains("What, ridden on a horse?", false));
    272 
    273     logger->warning(TestChannel1, "You're using coconuts!");
    274     EXPECT_EQ(0u, output().length());
    275     logger->notice(TestChannel1, "You're using coconuts!");
    276     EXPECT_EQ(0u, output().length());
    277     logger->info(TestChannel1, "You're using coconuts!");
    278     EXPECT_EQ(0u, output().length());
    279     logger->debug(TestChannel1, "You're using coconuts!");
    280     EXPECT_EQ(0u, output().length());
    281289
    282290    logger->setEnabled(this, false);
     
    287295    logger->setEnabled(this, true);
    288296    EXPECT_TRUE(logger->enabled());
    289     logger->error(TestChannel1, "%s %s", "You've got two empty halves of", "coconuts!");
    290     EXPECT_TRUE(output().contains("You've got two empty halves of coconuts!", false));
     297    logger->error(TestChannel1, "You've got ", 2, " empty halves of ", "coconuts!");
     298    EXPECT_TRUE(output().contains("You've got 2 empty halves of coconuts!", false));
    291299
    292300    WTFSetLogChannelLevel(&TestChannel1, WTFLogLevelError);
    293     logger->logAlways(TestChannel1, "%s", "I shall taunt you a second time!");
     301    logger->logAlways(TestChannel1, "I shall taunt you a second time!");
    294302    EXPECT_TRUE(output().contains("I shall taunt you a second time!", false));
    295303
Note: See TracChangeset for help on using the changeset viewer.