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 #include "juce_StandardHeader.h"
30 #include "juce_FileLogger.h"
31 #include "../io/files/juce_FileOutputStream.h"
32 #include "../io/files/juce_FileInputStream.h"
33 #include "../memory/juce_ScopedPointer.h"
34 #include "juce_SystemStats.h"
37 //==============================================================================
38 FileLogger::FileLogger (const File
& logFile_
,
39 const String
& welcomeMessage
,
40 const int maxInitialFileSizeBytes
)
43 if (maxInitialFileSizeBytes
>= 0)
44 trimFileSize (maxInitialFileSizeBytes
);
46 if (! logFile_
.exists())
48 // do this so that the parent directories get created..
54 << "**********************************************************" << newLine
55 << welcomeMessage
<< newLine
56 << "Log started: " << Time::getCurrentTime().toString (true, true) << newLine
;
58 FileLogger::logMessage (welcome
);
61 FileLogger::~FileLogger()
65 //==============================================================================
66 void FileLogger::logMessage (const String
& message
)
70 const ScopedLock
sl (logLock
);
72 FileOutputStream
out (logFile
, 256);
73 out
<< message
<< newLine
;
77 void FileLogger::trimFileSize (int maxFileSizeBytes
) const
79 if (maxFileSizeBytes
<= 0)
85 const int64 fileSize
= logFile
.getSize();
87 if (fileSize
> maxFileSizeBytes
)
89 ScopedPointer
<FileInputStream
> in (logFile
.createInputStream());
90 jassert (in
!= nullptr);
94 in
->setPosition (fileSize
- maxFileSizeBytes
);
98 MemoryBlock contentToSave
;
99 contentToSave
.setSize (maxFileSizeBytes
+ 4);
100 contentToSave
.fillWith (0);
102 in
->read (contentToSave
.getData(), maxFileSizeBytes
);
105 content
= contentToSave
.toString();
110 while (newStart
< fileSize
111 && content
[newStart
] != '\n'
112 && content
[newStart
] != '\r')
115 logFile
.deleteFile();
116 logFile
.appendText (content
.substring (newStart
), false, false);
122 //==============================================================================
123 FileLogger
* FileLogger::createDefaultAppLogger (const String
& logFileSubDirectoryName
,
124 const String
& logFileName
,
125 const String
& welcomeMessage
,
126 const int maxInitialFileSizeBytes
)
129 File
logFile ("~/Library/Logs");
130 logFile
= logFile
.getChildFile (logFileSubDirectoryName
)
131 .getChildFile (logFileName
);
134 File
logFile (File::getSpecialLocation (File::userApplicationDataDirectory
));
136 if (logFile
.isDirectory())
138 logFile
= logFile
.getChildFile (logFileSubDirectoryName
)
139 .getChildFile (logFileName
);
143 return new FileLogger (logFile
, welcomeMessage
, maxInitialFileSizeBytes
);