All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.android.build.gradle.internal.LoggerWrapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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.android.build.gradle.internal;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.common.res2.MergingException;
import com.android.utils.ILogger;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;

/**
 * Implementation of Android's {@link ILogger} over Gradle's {@link Logger}.
 *
 * Note that this maps info to the default user-visible lifecycle.
 */
public class LoggerWrapper implements ILogger {

    // Mapping from ILogger method call to gradle log level.
    private static final LogLevel ILOGGER_ERROR = LogLevel.ERROR;
    private static final LogLevel ILOGGER_WARNING = LogLevel.WARN;
    private static final LogLevel ILOGGER_INFO = LogLevel.LIFECYCLE;
    private static final LogLevel ILOGGER_VERBOSE = LogLevel.INFO;

    private final Logger logger;

    @NonNull
    public static LoggerWrapper getLogger(@NonNull Class klass) {
        return new LoggerWrapper(Logging.getLogger(klass));
    }

    public LoggerWrapper(@NonNull Logger logger) {
        this.logger = logger;
    }

    @Override
    public void error(@Nullable Throwable throwable, @Nullable String s, Object... objects) {
        if (throwable instanceof MergingException) {
            // MergingExceptions have a known cause: they aren't internal errors, they
            // are errors in the user's code, so a full exception is not helpful (and
            // these exceptions should include a pointer to the user's error right in
            // the message).
            //
            // Furthermore, these exceptions are already caught by the MergeResources
            // and MergeAsset tasks, so don't duplicate the output
            return;
        }

        if (!logger.isEnabled(ILOGGER_ERROR)) {
            return;
        }

        if (s == null) {
            s = "[no message defined]";
        } else if (objects != null && objects.length > 0) {
            s = String.format(s, objects);
        }

        if (throwable == null) {
            logger.log(ILOGGER_ERROR, s);

        } else {
            logger.log(ILOGGER_ERROR, s, throwable);
        }
    }

    @Override
    public void warning(@NonNull String s, Object... objects) {
        log(ILOGGER_WARNING, s, objects);
    }

    @Override
    public void info(@NonNull String s, Object... objects) {
        log(ILOGGER_INFO, s, objects);
    }

    @Override
    public void verbose(@NonNull String s, Object... objects) {
        log(ILOGGER_VERBOSE, s, objects);
    }

    private void log(@NonNull LogLevel logLevel, @NonNull String s, @Nullable Object[] objects){
        if (!logger.isEnabled(logLevel)) {
            return;
        }
        if (objects == null || objects.length == 0) {
            logger.log(logLevel, s);

        } else {
            logger.log(logLevel, String.format(s, objects));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy