Coverage Report - net.sourceforge.acelogger.LogEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
LogEvent
86%
43/50
60%
6/10
1,263
 
 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;
 16  
 
 17  
 import java.io.Serializable;
 18  
 import java.util.Date;
 19  
 import java.util.concurrent.atomic.AtomicLong;
 20  
 
 21  
 import net.sourceforge.acelogger.constants.LevelFilterConstants;
 22  
 import net.sourceforge.acelogger.level.LogLevel;
 23  
 import net.sourceforge.acelogger.location.LogEventLocation;
 24  
 
 25  
 /**
 26  
  * Used to store log event data.
 27  
  * 
 28  
  * @author Zardi (https://sourceforge.net/users/daniel_zardi)
 29  
  * @version 1.0.0
 30  
  * @since 1.0.0
 31  
  */
 32  
 public class LogEvent implements Serializable {
 33  
 
 34  
         /**
 35  
          * This constant is used when a serialized object is read to ensure that the object is
 36  
          * compatible with the current version of this class. For more info on version changes see
 37  
          * <a href="http://java.sun.com/javase/6/docs/platform/serialization/spec/version.html#6678">
 38  
          * Type Changes Affecting Serialization
 39  
          * </a>.
 40  
          */
 41  
         private static final long serialVersionUID = 1L;
 42  
 
 43  
         /**
 44  
          * The time of the first access to this class.
 45  
          */
 46  
         // FIXME: Remove from this class
 47  1
         private static final Date START_TIME = new Date();
 48  
 
 49  
         /**
 50  
          * Identifier generator. This is used to give a unique identifier to each log event.
 51  
          */
 52  
         // FIXME: Remove from this class
 53  1
         private static final AtomicLong LOG_SEQUENCE = new AtomicLong();
 54  
 
 55  
         /**
 56  
          * The identifier of this event, this number should be unique.
 57  
          */
 58  
         private long identifier;
 59  
 
 60  
         /**
 61  
          * The time this event was generated.
 62  
          */
 63  
         private Date callTime;
 64  
 
 65  
         /**
 66  
          * The message to be formatted and logged.
 67  
          */
 68  
         private String message;
 69  
 
 70  
         /**
 71  
          * The parameters used to format the message.
 72  
          */
 73  
         private Object[] messageParameters;
 74  
 
 75  
         /**
 76  
          * The caller location.
 77  
          */
 78  
         private LogEventLocation location;
 79  
 
 80  
         /**
 81  
          * The level of this log event.
 82  
          */
 83  
         private LogLevel level;
 84  
 
 85  
         /**
 86  
          * The cause that generated this log event.
 87  
          */
 88  
         private Throwable cause;
 89  
 
 90  
         /**
 91  
          * Constructs a new log event using the supplied information.
 92  
          * 
 93  
          * @param message
 94  
          *            The description for this event.
 95  
          * @param level
 96  
          *            The severity level for this event.
 97  
          * @since 1.0.0
 98  
          */
 99  
         public LogEvent(String message, LogLevel level) {
 100  0
                 this(message, level, new Object[] {});
 101  0
         }
 102  
 
 103  
         /**
 104  
          * Constructs a new log event using the supplied information.
 105  
          * 
 106  
          * @param message
 107  
          *            The description for this event.
 108  
          * @param level
 109  
          *            The severity level for this event.
 110  
          * @param messageParameters
 111  
          *            The parameters to be interpolated into the message.
 112  
          * @since 1.0.0
 113  
          */
 114  
         public LogEvent(String message, LogLevel level, Object... messageParameters) {
 115  1
                 this(message, level, null, messageParameters);
 116  1
         }
 117  
 
 118  
         /**
 119  
          * Constructs a new log event using the supplied information.
 120  
          * 
 121  
          * @param message
 122  
          *            The description for this event.
 123  
          * @param level
 124  
          *            The severity level for this event.
 125  
          * @param location
 126  
          *            The location that originated this event.
 127  
          * @param messageParameters
 128  
          *            The parameters to be interpolated into the message.
 129  
          * @since 1.0.0
 130  
          */
 131  
         public LogEvent(String message, LogLevel level, LogEventLocation location,
 132  
                         Object... messageParameters) {
 133  1
                 this(message, level, location, null, messageParameters);
 134  1
         }
 135  
 
 136  
         /**
 137  
          * Constructs a new log event using the supplied information.
 138  
          * 
 139  
          * @param message
 140  
          *            The description for this event.
 141  
          * @param level
 142  
          *            The severity level for this event.
 143  
          * @param location
 144  
          *            The location that originated this event.
 145  
          * @param cause
 146  
          *            The problem that originated this event.
 147  
          * @param messageParameters
 148  
          *            The parameters to be interpolated into the message.
 149  
          * @since 1.0.0
 150  
          */
 151  
         public LogEvent(String message, LogLevel level, LogEventLocation location, Throwable cause,
 152  11
                         Object... messageParameters) {
 153  11
                 setIdentifier(LOG_SEQUENCE.incrementAndGet());
 154  11
                 setCallTime(new Date());
 155  11
                 setMessage(message);
 156  11
                 setMessageParameters(messageParameters);
 157  11
                 setLevel(level);
 158  11
                 setLocation(location);
 159  11
                 setCause(cause);
 160  11
         }
 161  
 
 162  
         /**
 163  
          * Gets the level for this log event.
 164  
          * 
 165  
          * @return The log event's level.
 166  
          * @since 1.0.0
 167  
          */
 168  
         public final LogLevel getLevel() {
 169  18
                 return level;
 170  
         }
 171  
 
 172  
         /**
 173  
          * Sets the level for this log event.
 174  
          * 
 175  
          * @param level
 176  
          *            The log event's level.
 177  
          * @since 1.0.0
 178  
          */
 179  
         private final void setLevel(LogLevel level) {
 180  11
                 if (level == null) {
 181  0
                         this.level = LevelFilterConstants.DEFAULT_LEVEL;
 182  
                 } else {
 183  11
                         this.level = level;
 184  
                 }
 185  11
         }
 186  
 
 187  
         /**
 188  
          * Gets the identifier for this log event.
 189  
          * 
 190  
          * @return The log event's identifier.
 191  
          * @since 1.0.0
 192  
          */
 193  
         public final long getIdentifier() {
 194  0
                 return identifier;
 195  
         }
 196  
 
 197  
         /**
 198  
          * Sets the identifier for this log event.
 199  
          * 
 200  
          * @param identifier
 201  
          *            The log event's identifier.
 202  
          * @since 1.0.0
 203  
          */
 204  
         private final void setIdentifier(long identifier) {
 205  11
                 this.identifier = identifier;
 206  11
         }
 207  
 
 208  
         /**
 209  
          * Gets the call time for this log event.
 210  
          * 
 211  
          * @return The log event's call time.
 212  
          * @since 1.0.0
 213  
          */
 214  
         public final Date getCallTime() {
 215  18
                 return new Date(callTime.getTime());
 216  
         }
 217  
 
 218  
         /**
 219  
          * Sets the call time for this log event.
 220  
          * 
 221  
          * @param callTime
 222  
          *            The log event's call time.
 223  
          * @since 1.0.0
 224  
          */
 225  
         private final void setCallTime(Date callTime) {
 226  11
                 this.callTime = callTime;
 227  11
         }
 228  
 
 229  
         /**
 230  
          * Gets the message for this log event.
 231  
          * 
 232  
          * @return The log event's message.
 233  
          * @since 1.0.0
 234  
          */
 235  
         public final String getMessage() {
 236  27
                 return message;
 237  
         }
 238  
 
 239  
         /**
 240  
          * Sets the message for this log event.
 241  
          * 
 242  
          * @param message
 243  
          *            The log event's message.
 244  
          * @since 1.0.0
 245  
          */
 246  
         private final void setMessage(String message) {
 247  11
                 if (message == null) {
 248  0
                         this.message = "";
 249  
                 } else {
 250  11
                         this.message = message;
 251  
                 }
 252  11
         }
 253  
 
 254  
         /**
 255  
          * Gets the message parameters for this log event.
 256  
          * 
 257  
          * @return The log event's message parameters.
 258  
          * @since 1.0.0
 259  
          */
 260  
         public final Object[] getMessageParameters() {
 261  27
                 Object[] parametersCopy = new Object[messageParameters.length];
 262  27
                 System.arraycopy(messageParameters, 0, parametersCopy, 0, messageParameters.length);
 263  27
                 return parametersCopy;
 264  
         }
 265  
 
 266  
         /**
 267  
          * Sets the message parameters for this log event.
 268  
          * 
 269  
          * @param parameters
 270  
          *            The log event's message parameters.
 271  
          * @since 1.0.0
 272  
          */
 273  
         private final void setMessageParameters(Object[] parameters) {
 274  11
                 if (parameters == null || parameters.length == 0) {
 275  0
                         this.messageParameters = new Object[] {};
 276  
                 } else {
 277  11
                         this.messageParameters = new Object[parameters.length];
 278  11
                         System.arraycopy(parameters, 0, this.messageParameters, 0, parameters.length);
 279  
                 }
 280  11
         }
 281  
 
 282  
         /**
 283  
          * Gets the call location for this log event.
 284  
          * 
 285  
          * @return The log event's call location.
 286  
          * @since 1.0.0
 287  
          */
 288  
         public final LogEventLocation getLocation() {
 289  18
                 return location;
 290  
         }
 291  
 
 292  
         /**
 293  
          * Sets the call location for this log event.
 294  
          * 
 295  
          * @param location
 296  
          *            The log event's call location.
 297  
          * @since 1.0.0
 298  
          */
 299  
         private final void setLocation(LogEventLocation location) {
 300  11
                 if (location == null) {
 301  1
                         this.location = new LogEventLocation();
 302  
                 } else {
 303  10
                         this.location = location;
 304  
                 }
 305  11
         }
 306  
 
 307  
         /**
 308  
          * Gets the cause for this log event.
 309  
          * 
 310  
          * @return The log event's cause.
 311  
          * @since 1.0.0
 312  
          */
 313  
         public final Throwable getCause() {
 314  28
                 return cause;
 315  
         }
 316  
 
 317  
         /**
 318  
          * Sets the cause for this log event.
 319  
          * 
 320  
          * @param cause
 321  
          *            The log event's cause.
 322  
          * @since 1.0.0
 323  
          */
 324  
         private final void setCause(Throwable cause) {
 325  11
                 this.cause = cause;
 326  11
         }
 327  
 
 328  
         /**
 329  
          * Gets the start time for the application.
 330  
          * 
 331  
          * @return The log system's start time.
 332  
          * @since 1.0.0
 333  
          */
 334  
         public static final Date getStartTime() {
 335  0
                 return new Date(START_TIME.getTime());
 336  
         }
 337  
 
 338  
 }