1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.mozilla.org/MPL/MPL-1.1.html 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import org.as2lib.core.BasicClass; 18 import org.as2lib.util.Stringifier; 19 import org.as2lib.env.log.LogLevel; 20 import org.as2lib.env.log.stringifier.PatternLogMessageStringifier; 21 22 /** 23 * {@code LogMessage} is a dumb data holder that contains all the information about 24 * the message to log. 25 * 26 * <p>These information are the the message to log, its level and the name of the 27 * logger that is responsible for logging the message. 28 * 29 * <p>The {@link #toString} method uses the set stringifier to obtain its string 30 * representation. If you want a different appearance of the log message you can 31 * use the static {@link #setStringifier} method to set your custom stringifier. 32 * 33 * <p>The {@link PatternLogMessageStringifier} supports different presentation styles. 34 * It allows to switch the log level, the logger name and the time on and off. 35 * 36 * @author Simon Wacker 37 */ 38 class org.as2lib.env.log.LogMessage extends BasicClass { 39 40 /** The currently used stringifier. */ 41 private static var stringifier:Stringifier; 42 43 /** The message object to log. */ 44 private var message; 45 46 /** The level the of the log message. */ 47 private var level:LogLevel; 48 49 /** The name of the logger that logs the message. */ 50 private var loggerName:String; 51 52 /** The number of milliseconds elapsed from 1/1/1970 until log message was created. */ 53 private var timeStamp:Number; 54 55 /** 56 * Returns either the stringifier set via {@link #setStringifier} or the default 57 * one which is an instance of class {@link PatternLogMessageStringifier}. 58 * 59 * @return the currently used stringifier 60 */ 61 public static function getStringifier(Void):Stringifier { 62 if (!stringifier) stringifier = new PatternLogMessageStringifier(true, true, "HH:nn:ss.S"); 63 return stringifier; 64 } 65 66 /** 67 * Sets a new stringifier to be used by the {@link #toString} method. 68 * 69 * <p>If {@code newStringifier} is {@code null} the {@link #getStringifier} method 70 * will return the default stringifier. 71 * 72 * @param newStringifier the new stringifier to be used 73 */ 74 public static function setStringifier(newStringifier:Stringifier):Void { 75 stringifier = newStringifier; 76 } 77 78 /** 79 * Constructs a new {@code LogMessage} instance. 80 * 81 * <p>If {@code timeStamp} is {@code null} or {@code undefined} this constructor 82 * sets it by itself using the current time. 83 * 84 * @param message the message object to log 85 * @param level the level of the passed-in {@code message} 86 * @param loggerName the name of the logger that logs the {@code message} 87 * @param timeStamp (optional) the number of milliseconds elapsed from 1/1/1970 88 * until log this message was created 89 */ 90 public function LogMessage(message, level:LogLevel, loggerName:String, timeStamp:Number) { 91 this.message = message; 92 this.level = level; 93 this.loggerName = loggerName; 94 // new Date().getTime() is not mtasc compatible 95 this.timeStamp = timeStamp == null ? (new Date()).getTime() : timeStamp; 96 } 97 98 /** 99 * Returns the message object to log 100 * 101 * @return message the message object to log 102 */ 103 public function getMessage(Void) { 104 return message; 105 } 106 107 /** 108 * Returns the level of the message. 109 * 110 * @return the level of the message 111 */ 112 public function getLevel(Void):LogLevel { 113 return level; 114 } 115 116 /** 117 * Returns the name of the logger that logs the message. 118 * 119 * @return the name of the logging logger 120 */ 121 public function getLoggerName(Void):String { 122 return loggerName; 123 } 124 125 /** 126 * Returns the number of milliseconds elapsed from 1/1/1970 until message was 127 * created. 128 * 129 * @returns the number of milliseconds elapsed from 1/1/1970 until message was 130 * created. 131 */ 132 public function getTimeStamp(Void):Number { 133 return timeStamp; 134 } 135 136 /** 137 * Uses the stringifier returned by the static {@link #getStringifier} method 138 * to stringify this instance. 139 * 140 * @return the string representation of this log message 141 */ 142 public function toString():String { 143 return getStringifier().execute(this); 144 } 145 146 }