
org.jboss.logging.processor.apt.ToolLogger Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.jboss.logging.processor.apt;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic.Kind;
import org.jboss.logging.processor.model.DelegatingElement;
/**
* A logger for logging messages for annotation processors.
*
* @author James R. Perkins
* @author Kevin Pollet - SERLI - ([email protected])
*/
public final class ToolLogger {
private final Messager messager;
private final boolean isDebugEnabled;
private ToolLogger(final Messager messager, final boolean isDebugEnabled) {
this.messager = messager;
this.isDebugEnabled = isDebugEnabled;
}
/**
* Creates a new tool logger.
*
* @param processingEnv the processing environment
*
* @return a new tool logger
*/
public static ToolLogger getLogger(final ProcessingEnvironment processingEnv) {
String debug = processingEnv.getOptions().get(LoggingToolsProcessor.DEBUG_OPTION);
boolean isDebugEnabled = Boolean.parseBoolean(debug);
return new ToolLogger(processingEnv.getMessager(), isDebugEnabled);
}
/**
* Returns {@code true} if debugging is enabled, otherwise {@code false}.
*
* It is not necessary to invoke this method before invoking {@code debug}
* methods. The debug methods will only log messages if debugging is
* enabled.
*
*
* @return {@code true} if debugging is enabled, otherwise {@code false}.
*/
public boolean isDebugEnabled() {
return isDebugEnabled;
}
/**
* Prints a note message.
*
* @param element the element to print with the note.
* @param message the message.
*/
public void note(final Element element, final String message) {
log(Kind.NOTE, element, message);
}
/**
* Prints a formatted note message.
*
* @param element the element to print with the note.
* @param messageFormat the message format.
* @param args the format arguments.
*/
public void note(final Element element, final String messageFormat, final Object... args) {
log(Kind.NOTE, element, messageFormat, args);
}
/**
* Prints a formatted debug message if debugging is enabled.
*
* @param messageFormat the message format.
* @param args the format arguments.
*/
public void debug(final String messageFormat, final Object... args) {
if (isDebugEnabled) {
debug(null, messageFormat, args);
}
}
/**
* Prints a debug message.
*
* @param element the element to print with the note.
* @param message the message.
*/
public void debug(final Element element, final String message) {
if (isDebugEnabled) {
other(element, message);
}
}
/**
* Prints a formatted debug message if debugging is enabled.
*
* @param element the element to print with the note.
* @param messageFormat the message format.
* @param args the format arguments.
*/
public void debug(final Element element, final String messageFormat, final Object... args) {
if (isDebugEnabled) {
other(null, messageFormat, element, args);
}
}
/**
* Prints a warning message.
*
* @param element the element to print with the message.
* @param message the message.
*/
public void warn(final Element element, final String message) {
log(Kind.WARNING, element, message);
}
/**
* Prints a formatted warning message.
*
* @param element the element that caused the warning.
* @param messageFormat the message format.
* @param args the format arguments.
*/
public void warn(final Element element, final String messageFormat, final Object... args) {
log(Kind.WARNING, element, messageFormat, args);
}
/**
* Prints a warning message.
*
* @param element the element to print with the message.
* @param message the message.
*/
public void mandatoryWarning(final Element element, final String message) {
log(Kind.MANDATORY_WARNING, element, message);
}
/**
* Prints a formatted warning message.
*
* @param element the element that caused the warning.
* @param messageFormat the message format.
* @param args the format arguments.
*/
public void mandatoryWarning(final Element element, final String messageFormat, final Object... args) {
log(Kind.MANDATORY_WARNING, element, messageFormat, args);
}
/**
* Prints a formatted error message.
*
* @param messageFormat the message format.
* @param args the format arguments.
*/
public void error(final String messageFormat, final Object... args) {
log(Kind.ERROR, null, messageFormat, args);
}
/**
* Prints a error message.
*
* @param element the element to print with the message.
* @param message the message.
*/
public void error(final Element element, final String message) {
log(Kind.ERROR, element, message);
}
/**
* Prints a formatted error message.
*
* @param messageFormat the message format.
* @param element the element that caused the warning.
* @param args the format arguments.
*/
public void error(final Element element, final String messageFormat, final Object... args) {
log(Kind.ERROR, element, messageFormat, args);
}
/**
* Prints an error message.
*
* @param cause the cause of the error.
*/
public void error(final Throwable cause) {
error(null, cause);
}
/**
* Prints a error message.
*
* @param cause the cause of the error.
* @param element the element to print with the message.
* @param message the message.
*/
public void error(final Throwable cause, final Element element, final String message) {
log(Kind.ERROR, element, cause, message);
}
/**
* Prints an error message.
*
* @param cause the cause of the error.
* @param messageFormat the message format.
* @param args the format arguments.
*/
public void error(final Throwable cause, final String messageFormat, final Object... args) {
error(null, cause, messageFormat, args);
}
/**
* Prints an error message.
*
* @param cause the cause of the error.
* @param element the element that caused the error.
*/
public void error(final Element element, final Throwable cause) {
log(Kind.ERROR, element, cause, null);
}
/**
* Prints an error message.
*
* @param cause the cause of the error.
* @param messageFormat the message format.
* @param element the element that caused the warning.
* @param args the format arguments.
*/
public void error(final Element element, final Throwable cause, final String messageFormat, final Object... args) {
log(Kind.ERROR, element, cause, messageFormat, args);
}
/**
* Prints a message that does not fit the other types.
*
* @param element the element to print with the message.
* @param message the message.
*/
public void other(final Element element, final String message) {
log(Kind.OTHER, element, message);
}
/**
* Prints a formatted message that does not fit the other types.
*
* @param element the element to print with the note.
* @param messageFormat the message format.
* @param args the format arguments.
*/
public void other(final Element element, final String messageFormat, final Object... args) {
log(Kind.OTHER, element, messageFormat, args);
}
private void log(final Kind kind, final Element element, final String message) {
if (element == null) {
messager.printMessage(kind, message);
} else {
messager.printMessage(kind, message, getElement(element));
}
}
private void log(final Kind kind, final Element element, final String messageFormat, final Object... args) {
try {
String message = ((args == null || args.length == 0) ? messageFormat : String.format(messageFormat, args));
if (element == null) {
messager.printMessage(kind, message);
} else {
messager.printMessage(kind, message, getElement(element));
}
// Fail gracefully
} catch (Throwable t) {
if (element == null) {
messager.printMessage(Kind.ERROR, "Error logging original message: " + messageFormat);
} else {
messager.printMessage(Kind.ERROR, "Error logging original message: " + messageFormat, getElement(element));
}
}
}
private void log(final Kind kind, final Element element, final Throwable cause, final String messageFormat,
final Object... args) {
String stringCause = stackTraceToString(cause);
if (messageFormat == null) {
log(kind, element, stringCause);
} else {
String messageWithCause = messageFormat.concat(", cause : %s");
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy