1/*
2 * Copyright (C) 2008 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 "InitializeLogging.h"
28#include "Logging.h"
29
30#if !LOG_DISABLED
31
32#include <windows.h>
33#include <wtf/OwnArrayPtr.h>
34#include <wtf/text/WTFString.h>
35
36namespace WebCore {
37
38static inline void initializeWithUserDefault(WTFLogChannel& channel)
39{
40    DWORD length = GetEnvironmentVariableA(channel.defaultName, 0, 0);
41    if (!length)
42        return;
43
44    OwnArrayPtr<char> buffer = adoptArrayPtr(new char[length]);
45
46    if (!GetEnvironmentVariableA(channel.defaultName, buffer.get(), length))
47        return;
48
49    String variableValue(buffer.get());
50
51    static const String& hexadecimalPrefix = *new String("0x");
52    if (variableValue.length() < 3 || !variableValue.startsWith(hexadecimalPrefix, false)) {
53        LOG_ERROR("Unable to parse hex value for %s (%s), logging is off", channel.defaultName, buffer.get());
54        return;
55    }
56
57    String unprefixedValue = variableValue.substring(2);
58
59    // Now parse the unprefixed string as a hexadecimal number.
60    bool parsedSuccessfully = false;
61    unsigned logLevel = unprefixedValue.toUIntStrict(&parsedSuccessfully, 16);
62
63    if (!parsedSuccessfully) {
64        LOG_ERROR("Unable to parse hex value for %s (%s), logging is off", channel.defaultName, buffer.get());
65        return;
66    }
67
68    if ((logLevel & channel.mask) == channel.mask)
69        channel.state = WTFLogChannelOn;
70    else
71        channel.state = WTFLogChannelOff;
72}
73
74void initializeLoggingChannelsIfNecessary()
75{
76    static bool haveInitializedLoggingChannels = false;
77    if (haveInitializedLoggingChannels)
78        return;
79    haveInitializedLoggingChannels = true;
80
81    initializeWithUserDefault(LogNotYetImplemented);
82    initializeWithUserDefault(LogFrames);
83    initializeWithUserDefault(LogLoading);
84    initializeWithUserDefault(LogPopupBlocking);
85    initializeWithUserDefault(LogEvents);
86    initializeWithUserDefault(LogEditing);
87    initializeWithUserDefault(LogLiveConnect);
88    initializeWithUserDefault(LogIconDatabase);
89    initializeWithUserDefault(LogSQLDatabase);
90    initializeWithUserDefault(LogSpellingAndGrammar);
91    initializeWithUserDefault(LogBackForward);
92    initializeWithUserDefault(LogHistory);
93    initializeWithUserDefault(LogPageCache);
94    initializeWithUserDefault(LogPlatformLeaks);
95    initializeWithUserDefault(LogResourceLoading);
96    initializeWithUserDefault(LogAnimations);
97    initializeWithUserDefault(LogNetwork);
98    initializeWithUserDefault(LogFTP);
99    initializeWithUserDefault(LogThreading);
100    initializeWithUserDefault(LogStorageAPI);
101    initializeWithUserDefault(LogMedia);
102    initializeWithUserDefault(LogPlugins);
103    initializeWithUserDefault(LogArchives);
104    initializeWithUserDefault(LogProgress);
105    initializeWithUserDefault(LogFileAPI);
106}
107
108} // namespace WebCore
109
110#endif // !LOG_DISABLED
111