00001 /******************************************************************************* 00002 00003 @file RollingFileAppender.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.RollingFileAppender; 00040 00041 version (Mango) 00042 { 00043 private import mango.log.Appender, 00044 mango.log.FileAppender; 00045 00046 private import mango.io.FilePath, 00047 mango.io.FileSystem, 00048 mango.io.FileConduit, 00049 mango.io.DisplayWriter; 00050 00051 /******************************************************************************* 00052 00053 Append log messages to a file set. 00054 00055 *******************************************************************************/ 00056 00057 public class RollingFileAppender : FileAppender 00058 { 00059 private static uint mask; 00060 private FilePath[] paths; 00061 private int index; 00062 private IBuffer buffer; 00063 private ulong maxSize, 00064 fileSize; 00065 00066 /*********************************************************************** 00067 00068 Get a unique fingerprint for this class 00069 00070 ***********************************************************************/ 00071 00072 static this() 00073 { 00074 mask = nextMask(); 00075 } 00076 00077 /*********************************************************************** 00078 00079 Create a basic RollingFileAppender to a file-set with the 00080 specified path. 00081 00082 ***********************************************************************/ 00083 00084 this (FilePath p, int count, ulong maxSize) 00085 in { 00086 assert (count > 1 && count < 10); 00087 assert (p); 00088 } 00089 body 00090 { 00091 char[1] x; 00092 for (int i=0; i < count; ++i) 00093 { 00094 x[0] = '0' + i; 00095 00096 MutableFilePath clone = new MutableFilePath (p); 00097 clone.setName (clone.getName ~ x); 00098 paths ~= clone; 00099 } 00100 00101 this.maxSize = maxSize; 00102 index = -1; 00103 nextFile (); 00104 } 00105 00106 /*********************************************************************** 00107 00108 Create a basic RollingFileAppender to a file-set with the 00109 specified path, and with the given Layout 00110 00111 ***********************************************************************/ 00112 00113 this (FilePath p, int count, ulong maxSize, Layout layout) 00114 { 00115 this (p, count, maxSize); 00116 setLayout (layout); 00117 } 00118 00119 /*********************************************************************** 00120 00121 Return the fingerprint for this class 00122 00123 ***********************************************************************/ 00124 00125 uint getMask () 00126 { 00127 return mask; 00128 } 00129 00130 /*********************************************************************** 00131 00132 Return the name of this class 00133 00134 ***********************************************************************/ 00135 00136 char[] getName () 00137 { 00138 return this.classinfo.name; 00139 } 00140 00141 /*********************************************************************** 00142 00143 Append an event to the output. 00144 00145 ***********************************************************************/ 00146 00147 synchronized void append (Event event) 00148 { 00149 char[] msg; 00150 00151 // file already full? 00152 if (fileSize >= maxSize) 00153 nextFile (); 00154 00155 // bump file size 00156 fileSize += FileSystem.NewlineString.length; 00157 00158 // writer log message and flush it 00159 Layout layout = getLayout; 00160 msg = layout.header (event); 00161 fileSize += msg.length; 00162 buffer.append (msg); 00163 00164 msg = layout.content (event); 00165 fileSize += msg.length; 00166 buffer.append (msg); 00167 00168 msg = layout.footer (event); 00169 fileSize += msg.length; 00170 buffer.append (msg); 00171 00172 buffer.append(FileSystem.NewlineString).flush(); 00173 } 00174 00175 /*********************************************************************** 00176 00177 Switch to the next file within the set 00178 00179 ***********************************************************************/ 00180 00181 private void nextFile () 00182 { 00183 // select next file in the set 00184 if (++index >= paths.length) 00185 index = 0; 00186 00187 // reset file size 00188 fileSize = 0; 00189 00190 // close any existing conduit 00191 close (); 00192 00193 // open file; get writer 00194 buffer = setConduit (new FileConduit (paths[index], FileStyle.WriteAppending)); 00195 } 00196 } 00197 }