2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_FILELOGGER_JUCEHEADER__
27 #define __JUCE_FILELOGGER_JUCEHEADER__
29 #include "juce_Logger.h"
30 #include "../io/files/juce_File.h"
31 #include "../memory/juce_ScopedPointer.h"
34 //==============================================================================
36 A simple implemenation of a Logger that writes to a file.
40 class JUCE_API FileLogger
: public Logger
43 //==============================================================================
44 /** Creates a FileLogger for a given file.
46 @param fileToWriteTo the file that to use - new messages will be appended
47 to the file. If the file doesn't exist, it will be created,
48 along with any parent directories that are needed.
49 @param welcomeMessage when opened, the logger will write a header to the log, along
50 with the current date and time, and this welcome message
51 @param maxInitialFileSizeBytes if this is zero or greater, then if the file already exists
52 but is larger than this number of bytes, then the start of the
53 file will be truncated to keep the size down. This prevents a log
54 file getting ridiculously large over time. The file will be truncated
55 at a new-line boundary. If this value is less than zero, no size limit
56 will be imposed; if it's zero, the file will always be deleted. Note that
57 the size is only checked once when this object is created - any logging
58 that is done later will be appended without any checking
60 FileLogger (const File
& fileToWriteTo
,
61 const String
& welcomeMessage
,
62 const int maxInitialFileSizeBytes
= 128 * 1024);
67 //==============================================================================
68 void logMessage (const String
& message
);
70 File
getLogFile() const { return logFile
; }
72 //==============================================================================
73 /** Helper function to create a log file in the correct place for this platform.
75 On Windows this will return a logger with a path such as:
76 c:\\Documents and Settings\\username\\Application Data\\[logFileSubDirectoryName]\\[logFileName]
78 On the Mac it'll create something like:
79 ~/Library/Logs/[logFileName]
81 The method might return 0 if the file can't be created for some reason.
83 @param logFileSubDirectoryName if a subdirectory is needed, this is what it will be called -
84 it's best to use the something like the name of your application here.
85 @param logFileName the name of the file to create, e.g. "MyAppLog.txt". Don't just
86 call it "log.txt" because if it goes in a directory with logs
87 from other applications (as it will do on the Mac) then no-one
88 will know which one is yours!
89 @param welcomeMessage a message that will be written to the log when it's opened.
90 @param maxInitialFileSizeBytes (see the FileLogger constructor for more info on this)
92 static FileLogger
* createDefaultAppLogger (const String
& logFileSubDirectoryName
,
93 const String
& logFileName
,
94 const String
& welcomeMessage
,
95 const int maxInitialFileSizeBytes
= 128 * 1024);
98 //==============================================================================
100 CriticalSection logLock
;
102 void trimFileSize (int maxFileSizeBytes
) const;
104 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileLogger
);
108 #endif // __JUCE_FILELOGGER_JUCEHEADER__