00001 /******************************************************************************* 00002 00003 @file Layout.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, May 2004 00034 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.log.Layout; 00040 00041 private import mango.log.Event; 00042 00043 /******************************************************************************* 00044 00045 Base class for all Layout instances 00046 00047 *******************************************************************************/ 00048 00049 public class Layout 00050 { 00051 private import mango.log.Logger; 00052 00053 /*********************************************************************** 00054 00055 Subclasses should implement this method to perform the 00056 formatting of each message header 00057 00058 ***********************************************************************/ 00059 00060 abstract char[] header (Event event); 00061 00062 /*********************************************************************** 00063 00064 Subclasses should implement this method to perform the 00065 formatting of each message footer 00066 00067 ***********************************************************************/ 00068 00069 char[] footer (Event event) 00070 { 00071 return ""; 00072 } 00073 00074 /*********************************************************************** 00075 00076 Subclasses should implement this method to perform the 00077 formatting of the actual message content. 00078 00079 ***********************************************************************/ 00080 00081 char[] content (Event event) 00082 { 00083 return event.toString; 00084 } 00085 00086 /*********************************************************************** 00087 00088 Return a text string representing the event level. 00089 00090 ***********************************************************************/ 00091 00092 protected final char[] levelString (Event event) 00093 { 00094 return Logger.LevelNames [event.getLevel]; 00095 } 00096 00097 /*********************************************************************** 00098 00099 Convert a time value (in milliseconds) to ascii 00100 00101 ***********************************************************************/ 00102 00103 final char[] ultoa (char[] s, ulong l) 00104 in { 00105 assert (s.length > 0); 00106 } 00107 body 00108 { 00109 int len = s.length; 00110 do { 00111 s[--len] = l % 10 + '0'; 00112 l /= 10; 00113 } while (l && len); 00114 return s[len..s.length]; 00115 } 00116 } 00117 00118 00119 /******************************************************************************* 00120 00121 A simple layout comprised only of level, name, and message 00122 00123 *******************************************************************************/ 00124 00125 public class SimpleLayout : Layout 00126 { 00127 static protected SimpleLayout simpleLayout; 00128 00129 /*********************************************************************** 00130 00131 Make one of these available for use as the default layout 00132 00133 ***********************************************************************/ 00134 00135 static this () 00136 { 00137 simpleLayout = new SimpleLayout(); 00138 } 00139 00140 /*********************************************************************** 00141 00142 Format outgoing message 00143 00144 ***********************************************************************/ 00145 00146 char[] header (Event event) 00147 { 00148 event.append (levelString (event)) 00149 .append(event.getName()) 00150 .append(" - "); 00151 return event.getContent; 00152 } 00153 } 00154 00155 /******************************************************************************* 00156 00157 A simple layout comprised only of time(ms), level, name, and message 00158 00159 *******************************************************************************/ 00160 00161 public class SimpleTimerLayout : Layout 00162 { 00163 /*********************************************************************** 00164 00165 Format outgoing message 00166 00167 ***********************************************************************/ 00168 00169 char[] header (Event event) 00170 { 00171 char[20] tmp; 00172 00173 event.append (ultoa (tmp, event.getTime)) 00174 .append (" ") 00175 .append (levelString (event)) 00176 .append (event.getName) 00177 .append (" - "); 00178 return event.getContent; 00179 } 00180 }