1 /* 2 * This file is part of AceLogger. 3 * 4 * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU 5 * Lesser General Public License as published by the Free Software Foundation, either version 3 of 6 * the License, or (at your option) any later version. 7 * 8 * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * Lesser General Public License for more details. 11 * 12 * You should have received a copy of the GNU Lesser General Public License along with AceLogger. 13 * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>. 14 */ 15 package net.sourceforge.acelogger.formatter; 16 17 import java.io.PrintWriter; 18 import java.io.StringWriter; 19 20 import net.sourceforge.acelogger.execution.LogController; 21 import net.sourceforge.acelogger.interpolator.TextInterpolator; 22 23 /** 24 * Abstracts all common tasks for {@link Formatter}. 25 * 26 * @author Zardi (https://sourceforge.net/users/daniel_zardi) 27 * @version 1.0.0 28 * @since 1.0.0 29 */ 30 public abstract class BaseFormatter implements Formatter { 31 32 /** 33 * The identifier of this formatter. 34 */ 35 private String identifier; 36 37 /** 38 * The text interpolator used to format the messages. 39 */ 40 private TextInterpolator interpolator; 41 42 /** 43 * Constructs a new BaseFormatter with the supplied identifier. 44 * 45 * @param identifier 46 * The string that identifies this formatter. 47 * @param interpolator 48 * The interpolator to be used by this formatter. 49 * @since 1.0.0 50 */ 51 public BaseFormatter(String identifier, TextInterpolator interpolator) { 52 setIdentifier(identifier); 53 setInterpolator(interpolator); 54 } 55 56 /** {@inheritDoc} */ 57 public final String getIdentifier() { 58 return identifier; 59 } 60 61 /** 62 * Sets the string that identifies this object. 63 * 64 * @param identifier 65 * The identifier of this object. 66 * @since 1.0.0 67 */ 68 private void setIdentifier(String identifier) { 69 this.identifier = identifier; 70 } 71 72 /** 73 * Sets the string that identifies this object. 74 * 75 * @param interpolator 76 * The text interpolator used to format the messages. 77 * @since 1.0.0 78 */ 79 private void setInterpolator(TextInterpolator interpolator) { 80 this.interpolator = interpolator; 81 } 82 83 /** 84 * Formats a message filling it with the supplied parameters, this method is just a convenient 85 * way to format messages without the need to catch a {@link IllegalArgumentException}. 86 * 87 * @param message 88 * The message to be filled with parameters, the format is dependent of the 89 * interpolator used. 90 * @param params 91 * The parameters that will be used to fill-in the message. 92 * @see TextInterpolator#interpolate(String, Object...) 93 * @return A string representing the supplied messages filled with parameters. 94 * @since 1.0.0 95 */ 96 protected String formatMessage(String message, Object... params) { 97 String formattedMessage = ""; 98 try { 99 formattedMessage = interpolator.interpolate(message, params); 100 } catch (IllegalArgumentException e) { 101 LogController.getInternalLogger().error( 102 "It was not possible to format \"{0}\" with [{1}]", e, message, params); 103 } 104 return formattedMessage; 105 } 106 107 /** 108 * Formats a string representing a stack trace based on the passed Throwable. 109 * 110 * @param cause 111 * The throwable to get the stack trace. 112 * @return A string representing the stack trace of this throwable. 113 * @since 1.0.0 114 */ 115 protected String formatStackTrace(Throwable cause) { 116 String formattedStack = ""; 117 if (cause != null) { 118 StringWriter stackTraceDestination = new StringWriter(); 119 PrintWriter stackTraceWriter = new PrintWriter(stackTraceDestination); 120 cause.printStackTrace(stackTraceWriter); 121 stackTraceWriter.flush(); 122 stackTraceWriter.close(); 123 formattedStack = stackTraceDestination.toString(); 124 } 125 return formattedStack; 126 } 127 128 }