<commit_before>/**
 * Created by <NAME>.
 *
 * Copyright (c) 2012 Couchbase, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

package com.couchbase.lite.util;

import java.util.concurrent.ConcurrentHashMap;

public class Log {

    private static Logger logger = LoggerFactory.createLogger();

    /**
     * A map of tags and their enabled log level
     */
    private static ConcurrentHashMap<String, Integer> enabledTags;

    /**
     * Logging tags
     */
    public static final String TAG = "CBLite";  // default "catch-all" tag
    public static final String TAG_SYNC = "Sync";
    public static final String TAG_REMOTE_REQUEST = "RemoteRequest";
    public static final String TAG_VIEW = "View";
    public static final String TAG_QUERY = "Query";
    public static final String TAG_CHANGE_TRACKER = "ChangeTracker";
    public static final String TAG_ROUTER = "Router";
    public static final String TAG_DATABASE = "Database";
    public static final String TAG_LISTENER = "Listener";
    public static final String TAG_MULTI_STREAM_WRITER = "MultistreamWriter";
    public static final String TAG_BLOB_STORE = "BlobStore";


    /**
     * Logging levels -- values match up with android.util.Log
     */
    public static final int VERBOSE = 2;
    public static final int DEBUG = 3;
    public static final int INFO = 4;
    public static final int WARN = 5;
    public static final int ERROR = 6;
    public static final int ASSERT = 7;

    static {
        enabledTags = new ConcurrentHashMap<String, Integer>();
        enabledTags.put(Log.TAG, WARN);
        enabledTags.put(Log.TAG_SYNC, WARN);
        enabledTags.put(Log.TAG_REMOTE_REQUEST, WARN);
        enabledTags.put(Log.TAG_VIEW, WARN);
        enabledTags.put(Log.TAG_QUERY, WARN);
        enabledTags.put(Log.TAG_CHANGE_TRACKER, WARN);
        enabledTags.put(Log.TAG_ROUTER, WARN);
        enabledTags.put(Log.TAG_DATABASE, WARN);
        enabledTags.put(Log.TAG_LISTENER, WARN);
        enabledTags.put(Log.TAG_MULTI_STREAM_WRITER, WARN);
    }

    /**
     * Enable logging for a particular tag / loglevel combo
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param logLevel The loglevel to enable.  Anything matching this loglevel
     *                 or having a more urgent loglevel will be emitted.  Eg, Log.VERBOSE.
     */
    public static void enableLogging(String tag, int logLevel) {
        enabledTags.put(tag, logLevel);
    }

    /**
     * Is logging enabled for given tag / loglevel combo?
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param logLevel The loglevel to check whether it's enabled.  Will match this loglevel
     *                 or a more urgent loglevel.  Eg, if Log.ERROR is enabled and Log.VERBOSE
     *                 is passed as a paremeter, it will return true.
     * @return boolean indicating whether logging is enabled.
     */
    /* package */ static boolean isLoggingEnabled(String tag, int logLevel) {
        if (enabledTags.containsKey(tag)) {
            int logLevelForTag = enabledTags.get(tag);
            return logLevel >= logLevelForTag;
        }
        return false;
    }

    /**
     * Send a VERBOSE message.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    public static void v(String tag, String msg) {
        if (logger != null) logger.v(tag, msg);
    }

    /**
     * Send a VERBOSE message and log the exception.<commit_msg>Issue #151 - comment regarding efficiency<commit_after>/**
 * Created by <NAME>.
 *
 * Copyright (c) 2012 Couchbase, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

package com.couchbase.lite.util;

import java.util.concurrent.ConcurrentHashMap;

public class Log {

    private static Logger logger = LoggerFactory.createLogger();

    /**
     * A map of tags and their enabled log level
     */
    private static ConcurrentHashMap<String, Integer> enabledTags;

    /**
     * Logging tags
     */
    public static final String TAG = "CBLite";  // default "catch-all" tag
    public static final String TAG_SYNC = "Sync";
    public static final String TAG_REMOTE_REQUEST = "RemoteRequest";
    public static final String TAG_VIEW = "View";
    public static final String TAG_QUERY = "Query";
    public static final String TAG_CHANGE_TRACKER = "ChangeTracker";
    public static final String TAG_ROUTER = "Router";
    public static final String TAG_DATABASE = "Database";
    public static final String TAG_LISTENER = "Listener";
    public static final String TAG_MULTI_STREAM_WRITER = "MultistreamWriter";
    public static final String TAG_BLOB_STORE = "BlobStore";


    /**
     * Logging levels -- values match up with android.util.Log
     */
    public static final int VERBOSE = 2;
    public static final int DEBUG = 3;
    public static final int INFO = 4;
    public static final int WARN = 5;
    public static final int ERROR = 6;
    public static final int ASSERT = 7;

    static {
        enabledTags = new ConcurrentHashMap<String, Integer>();
        enabledTags.put(Log.TAG, WARN);
        enabledTags.put(Log.TAG_SYNC, WARN);
        enabledTags.put(Log.TAG_REMOTE_REQUEST, WARN);
        enabledTags.put(Log.TAG_VIEW, WARN);
        enabledTags.put(Log.TAG_QUERY, WARN);
        enabledTags.put(Log.TAG_CHANGE_TRACKER, WARN);
        enabledTags.put(Log.TAG_ROUTER, WARN);
        enabledTags.put(Log.TAG_DATABASE, WARN);
        enabledTags.put(Log.TAG_LISTENER, WARN);
        enabledTags.put(Log.TAG_MULTI_STREAM_WRITER, WARN);
    }

    /**
     * Enable logging for a particular tag / loglevel combo
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param logLevel The loglevel to enable.  Anything matching this loglevel
     *                 or having a more urgent loglevel will be emitted.  Eg, Log.VERBOSE.
     */
    public static void enableLogging(String tag, int logLevel) {
        enabledTags.put(tag, logLevel);
    }

    /**
     * Is logging enabled for given tag / loglevel combo?
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param logLevel The loglevel to check whether it's enabled.  Will match this loglevel
     *                 or a more urgent loglevel.  Eg, if Log.ERROR is enabled and Log.VERBOSE
     *                 is passed as a paremeter, it will return true.
     * @return boolean indicating whether logging is enabled.
     */
    /* package */ static boolean isLoggingEnabled(String tag, int logLevel) {

        // this hashmap lookup might be a little expensive, and so it might make
        // sense to convert this over to a CopyOnWriteArrayList
        if (enabledTags.containsKey(tag)) {
            int logLevelForTag = enabledTags.get(tag);
            return logLevel >= logLevelForTag;
        }
        return false;
    }

    /**
     * Send a VERBOSE message.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    public static void v(String tag, String msg) {
        if (logger != null) logger.v(tag, msg);
    }

    /**
     * Send a VERBOSE message and log the exception.
{'fraction_non_alphanumeric': 0.07824601366742596, 'fraction_numerical': 0.003530751708428246, 'mean_word_length': 2.7097591888466415, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2642781', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2351, 'n_words': 944}