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