00001 /******************************************************************************* 00002 00003 @file FileAppender.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 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, May 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.log.FileAppender; 00037 00038 private import mango.log.Appender; 00039 00040 private import mango.io.FileConduit, 00041 mango.io.DisplayWriter; 00042 00043 /******************************************************************************* 00044 00045 Append log messages to a file. This basic version has no rollover 00046 support, so it just keeps on adding to the file. There's also a 00047 RollingFileAppender that may suit your needs. 00048 00049 *******************************************************************************/ 00050 00051 public class FileAppender : Appender 00052 { 00053 private static uint mask; 00054 private DisplayWriter writer; 00055 private IConduit conduit; 00056 00057 /*********************************************************************** 00058 00059 Get a unique fingerprint for this class 00060 00061 ***********************************************************************/ 00062 00063 static this() 00064 { 00065 mask = nextMask(); 00066 } 00067 00068 /*********************************************************************** 00069 00070 ***********************************************************************/ 00071 00072 protected this (){} 00073 00074 /*********************************************************************** 00075 00076 Create a basic FileAppender to a file with the specified 00077 path. 00078 00079 ***********************************************************************/ 00080 00081 this (char[] path) 00082 { 00083 setConduit (new FileConduit (path, FileStyle.WriteAppending)); 00084 } 00085 00086 /*********************************************************************** 00087 00088 Create a basic FileAppender to a file with the specified 00089 path, and with the given Layout 00090 00091 ***********************************************************************/ 00092 00093 this (char[] path, Layout layout) 00094 { 00095 this (path); 00096 setLayout (layout); 00097 } 00098 00099 /*********************************************************************** 00100 00101 Make sure the file is closed when we're GC'd 00102 00103 ***********************************************************************/ 00104 00105 ~this () 00106 { 00107 close(); 00108 } 00109 00110 /*********************************************************************** 00111 00112 Return the conduit 00113 00114 ***********************************************************************/ 00115 00116 IConduit getConduit () 00117 { 00118 return conduit; 00119 } 00120 00121 /*********************************************************************** 00122 00123 Set the conduit 00124 00125 ***********************************************************************/ 00126 00127 IWriter setConduit (IConduit conduit) 00128 { 00129 // close any existing conduit 00130 close (); 00131 00132 // create a new writer upon this conduit 00133 this.conduit = conduit; 00134 return (writer = new DisplayWriter (conduit)); 00135 } 00136 00137 /*********************************************************************** 00138 00139 Return the fingerprint for this class 00140 00141 ***********************************************************************/ 00142 00143 uint getMask () 00144 { 00145 return mask; 00146 } 00147 00148 /*********************************************************************** 00149 00150 Return the name of this class 00151 00152 ***********************************************************************/ 00153 00154 char[] getName () 00155 { 00156 return this.classinfo.name; 00157 } 00158 00159 /*********************************************************************** 00160 00161 Append an event to the output. 00162 00163 ***********************************************************************/ 00164 00165 synchronized void append (Event event) 00166 { 00167 writer.put (getLayout.format(event)).cr().flush(); 00168 } 00169 00170 /*********************************************************************** 00171 00172 Close the file associated with this Appender 00173 00174 ***********************************************************************/ 00175 00176 synchronized void close () 00177 { 00178 if (conduit) 00179 { 00180 conduit.close(); 00181 conduit = null; 00182 } 00183 } 00184 }