| 1 | /* |
| 2 | * Copyright (C) 2017-2019 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (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. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include <wtf/CryptographicallyRandomNumber.h> |
| 29 | #include <wtf/Logger.h> |
| 30 | |
| 31 | namespace WTF { |
| 32 | |
| 33 | class LoggerHelper { |
| 34 | public: |
| 35 | virtual ~LoggerHelper() = default; |
| 36 | |
| 37 | virtual const Logger& logger() const = 0; |
| 38 | virtual const char* logClassName() const = 0; |
| 39 | virtual WTFLogChannel& logChannel() const = 0; |
| 40 | virtual const void* logIdentifier() const = 0; |
| 41 | |
| 42 | #if !RELEASE_LOG_DISABLED |
| 43 | |
| 44 | #define LOGIDENTIFIER WTF::Logger::LogSiteIdentifier(logClassName(), __func__, logIdentifier()) |
| 45 | |
| 46 | #define ALWAYS_LOG(...) logger().logAlways(logChannel(), __VA_ARGS__) |
| 47 | #define ERROR_LOG(...) logger().error(logChannel(), __VA_ARGS__) |
| 48 | #define WARNING_LOG(...) logger().warning(logChannel(), __VA_ARGS__) |
| 49 | #define INFO_LOG(...) logger().info(logChannel(), __VA_ARGS__) |
| 50 | #define DEBUG_LOG(...) logger().debug(logChannel(), __VA_ARGS__) |
| 51 | #define WILL_LOG(_level_) logger().willLog(logChannel(), _level_) |
| 52 | |
| 53 | #define ALWAYS_LOG_IF(condition, ...) if (condition) logger().logAlways(logChannel(), __VA_ARGS__) |
| 54 | #define ERROR_LOG_IF(condition, ...) if (condition) logger().error(logChannel(), __VA_ARGS__) |
| 55 | #define WARNING_LOG_IF(condition, ...) if (condition) logger().warning(logChannel(), __VA_ARGS__) |
| 56 | #define INFO_LOG_IF(condition, ...) if (condition) logger().info(logChannel(), __VA_ARGS__) |
| 57 | #define DEBUG_LOG_IF(condition, ...) if (condition) logger().debug(logChannel(), __VA_ARGS__) |
| 58 | |
| 59 | const void* childLogIdentifier(uint64_t identifier) const |
| 60 | { |
| 61 | static const int64_t parentMask = 0xffffffffffff0000l; |
| 62 | static const int64_t maskLowerWord = 0xffffl; |
| 63 | return reinterpret_cast<const void*>((reinterpret_cast<uint64_t>(logIdentifier()) & parentMask) | (identifier & maskLowerWord)); |
| 64 | } |
| 65 | |
| 66 | static const void* uniqueLogIdentifier() |
| 67 | { |
| 68 | static uint64_t logIdentifier = cryptographicallyRandomNumber(); |
| 69 | return reinterpret_cast<const void*>(++logIdentifier); |
| 70 | } |
| 71 | #else |
| 72 | |
| 73 | #define LOGIDENTIFIER (WTF::nullopt) |
| 74 | |
| 75 | #define ALWAYS_LOG(...) ((void)0) |
| 76 | #define ERROR_LOG(...) ((void)0) |
| 77 | #define ERROR_LOG(...) ((void)0) |
| 78 | #define WARNING_LOG(...) ((void)0) |
| 79 | #define NOTICE_LOG(...) ((void)0) |
| 80 | #define INFO_LOG(...) ((void)0) |
| 81 | #define DEBUG_LOG(...) ((void)0) |
| 82 | #define WILL_LOG(_level_) false |
| 83 | |
| 84 | #define ALWAYS_LOG_IF(condition, ...) ((void)0) |
| 85 | #define ERROR_LOG_IF(condition, ...) ((void)0) |
| 86 | #define WARNING_LOG_IF(condition, ...) ((void)0) |
| 87 | #define INFO_LOG_IF(condition, ...) ((void)0) |
| 88 | #define DEBUG_LOG_IF(condition, ...) ((void)0) |
| 89 | |
| 90 | #endif |
| 91 | |
| 92 | }; |
| 93 | |
| 94 | } // namespace WTF |
| 95 | |
| 96 | using WTF::LoggerHelper; |
| 97 | |
| 98 | |