Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

RollingFileAppender.d

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

Generated on Sat Apr 9 20:11:28 2005 for Mango by doxygen 1.3.6