1/*
2 * Copyright (C) 2003, 2006, 2013 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#include "config.h"
27#include "Logging.h"
28#include "LogInitialization.h"
29
30#include <wtf/StdLibExtras.h>
31#include <wtf/text/CString.h>
32#include <wtf/text/WTFString.h>
33
34#if PLATFORM(COCOA)
35#include <notify.h>
36#include <wtf/BlockPtr.h>
37#endif
38
39namespace WebCore {
40
41#if !LOG_DISABLED || !RELEASE_LOG_DISABLED
42
43#define DEFINE_WEBCORE_LOG_CHANNEL(name) DEFINE_LOG_CHANNEL(name, LOG_CHANNEL_WEBKIT_SUBSYSTEM)
44WEBCORE_LOG_CHANNELS(DEFINE_WEBCORE_LOG_CHANNEL)
45
46static WTFLogChannel* logChannels[] = {
47 WEBCORE_LOG_CHANNELS(LOG_CHANNEL_ADDRESS)
48};
49
50static const size_t logChannelCount = WTF_ARRAY_LENGTH(logChannels);
51
52bool isLogChannelEnabled(const String& name)
53{
54 WTFLogChannel* channel = WTFLogChannelByName(logChannels, logChannelCount, name.utf8().data());
55 if (!channel)
56 return false;
57 return channel->state != WTFLogChannelState::Off;
58}
59
60static bool logChannelsNeedInitialization = true;
61
62void setLogChannelToAccumulate(const String& name)
63{
64 WTFLogChannel* channel = WTFLogChannelByName(logChannels, logChannelCount, name.utf8().data());
65 if (!channel)
66 return;
67
68 channel->state = WTFLogChannelState::OnWithAccumulation;
69 logChannelsNeedInitialization = true;
70}
71
72void initializeLogChannelsIfNecessary(Optional<String> logChannelString)
73{
74 if (!logChannelsNeedInitialization && !logChannelString)
75 return;
76
77 logChannelsNeedInitialization = false;
78
79 String enabledChannelsString = logChannelString ? logChannelString.value() : logLevelString();
80 WTFInitializeLogChannelStatesFromString(logChannels, logChannelCount, enabledChannelsString.utf8().data());
81}
82
83WTFLogChannel* getLogChannel(const String& name)
84{
85 return WTFLogChannelByName(logChannels, logChannelCount, name.utf8().data());
86}
87
88#else
89
90WTFLogChannel* getLogChannel(const String&)
91{
92 return nullptr;
93}
94
95#endif // !LOG_DISABLED || !RELEASE_LOG_DISABLED
96
97} // namespace WebCore
98