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 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029       
00030         @version        Initial version, May 2004
00031         @author         Kris
00032 
00033 
00034 *******************************************************************************/
00035 
00036 module mango.log.RollingFileAppender;
00037 
00038 private import  mango.log.Appender,
00039                 mango.log.FileAppender;
00040 
00041 private import  mango.io.FilePath,
00042                 mango.io.FileSystem,
00043                 mango.io.FileConduit,
00044                 mango.io.DisplayWriter;
00045 
00046 /*******************************************************************************
00047 
00048         Append log messages to a file set. 
00049 
00050 *******************************************************************************/
00051 
00052 public class RollingFileAppender : FileAppender
00053 {
00054         private static uint     mask;
00055         private FilePath[]      paths;
00056         private uint            index;
00057         private IWriter         writer;
00058         private ulong           maxSize,
00059                                 fileSize;
00060 
00061         /***********************************************************************
00062                 
00063                 Get a unique fingerprint for this class
00064 
00065         ***********************************************************************/
00066 
00067         static this()
00068         {
00069                 mask = nextMask();
00070         }
00071 
00072         /***********************************************************************
00073                 
00074                 Create a basic RollingFileAppender to a file-set with the 
00075                 specified path.
00076 
00077         ***********************************************************************/
00078 
00079         this (char[] path, int count, ulong maxSize)
00080         in {
00081            assert (count > 1 && count < 10);
00082            assert (path.length);
00083            }
00084         body
00085         {
00086                 FilePath p = new FilePath (path);
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 (char[] path, int count, ulong maxSize, Layout layout)
00110         {
00111                 this (path, 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 }

Generated on Sun Oct 24 22:31:16 2004 for Mango by doxygen 1.3.6