OpenShot Audio Library | OpenShotAudio: juce_FileLogger.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_FileLogger.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26FileLogger::FileLogger (const File& file,
27 const String& welcomeMessage,
28 const int64 maxInitialFileSizeBytes)
29 : logFile (file)
30{
31 if (maxInitialFileSizeBytes >= 0)
32 trimFileSize (logFile, maxInitialFileSizeBytes);
33
34 if (! file.exists())
35 file.create(); // (to create the parent directories)
36
37 String welcome;
38 welcome << newLine
39 << "**********************************************************" << newLine
40 << welcomeMessage << newLine
41 << "Log started: " << Time::getCurrentTime().toString (true, true) << newLine;
42
43 FileLogger::logMessage (welcome);
44}
45
46FileLogger::~FileLogger() {}
47
48//==============================================================================
49 void FileLogger::logMessage (const String& message)
50{
51 const ScopedLock sl (logLock);
52 DBG (message);
53 FileOutputStream out (logFile, 256);
54 out << message << newLine;
55}
56
57 void FileLogger::trimFileSize (const File& file, int64 maxFileSizeBytes)
58{
59 if (maxFileSizeBytes <= 0)
60 {
61 file.deleteFile();
62 }
63 else
64 {
65 const int64 fileSize = file.getSize();
66
67 if (fileSize > maxFileSizeBytes)
68 {
69 TemporaryFile tempFile (file);
70
71 {
72 FileOutputStream out (tempFile.getFile());
73 FileInputStream in (file);
74
75 if (! (out.openedOk() && in.openedOk()))
76 return;
77
78 in.setPosition (fileSize - maxFileSizeBytes);
79
80 for (;;)
81 {
82 const char c = in.readByte();
83 if (c == 0)
84 return;
85
86 if (c == '\n' || c == '\r')
87 {
88 out << c;
89 break;
90 }
91 }
92
93 out.writeFromInputStream (in, -1);
94 }
95
96 tempFile.overwriteTargetFileWithTemporary();
97 }
98 }
99}
100
101//==============================================================================
102 File FileLogger::getSystemLogFileFolder()
103{
104 #if JUCE_MAC
105 return File ("~/Library/Logs");
106 #else
107 return File::getSpecialLocation (File::userApplicationDataDirectory);
108 #endif
109}
110
111 FileLogger* FileLogger::createDefaultAppLogger (const String& logFileSubDirectoryName,
112 const String& logFileName,
113 const String& welcomeMessage,
114 const int64 maxInitialFileSizeBytes)
115{
116 return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
117 .getChildFile (logFileName),
118 welcomeMessage, maxInitialFileSizeBytes);
119}
120
121 FileLogger* FileLogger::createDateStampedLogger (const String& logFileSubDirectoryName,
122 const String& logFileNameRoot,
123 const String& logFileNameSuffix,
124 const String& welcomeMessage)
125{
126 return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
127 .getChildFile (logFileNameRoot + Time::getCurrentTime().formatted ("%Y-%m-%d_%H-%M-%S"))
128 .withFileExtension (logFileNameSuffix)
129 .getNonexistentSibling(),
130 welcomeMessage, 0);
131}
132
133} // namespace juce
juce::FileInputStream
Definition juce_FileInputStream.h:35
juce::FileInputStream::setPosition
bool setPosition(int64) override
Definition juce_FileInputStream.cpp:68
juce::FileInputStream::openedOk
bool openedOk() const noexcept
Definition juce_FileInputStream.h:67
juce::FileLogger
Definition juce_FileLogger.h:35
juce::FileLogger::FileLogger
FileLogger(const File &fileToWriteTo, const String &welcomeMessage, const int64 maxInitialFileSizeBytes=128 *1024)
Definition juce_FileLogger.cpp:26
juce::FileLogger::createDefaultAppLogger
static FileLogger * createDefaultAppLogger(const String &logFileSubDirectoryName, const String &logFileName, const String &welcomeMessage, const int64 maxInitialFileSizeBytes=128 *1024)
Definition juce_FileLogger.cpp:111
juce::FileLogger::trimFileSize
static void trimFileSize(const File &file, int64 maxFileSize)
Definition juce_FileLogger.cpp:57
juce::FileLogger::getSystemLogFileFolder
static File getSystemLogFileFolder()
Definition juce_FileLogger.cpp:102
juce::FileLogger::~FileLogger
~FileLogger() override
Definition juce_FileLogger.cpp:46
juce::FileLogger::createDateStampedLogger
static FileLogger * createDateStampedLogger(const String &logFileSubDirectoryName, const String &logFileNameRoot, const String &logFileNameSuffix, const String &welcomeMessage)
Definition juce_FileLogger.cpp:121
juce::FileLogger::logMessage
void logMessage(const String &) override
Definition juce_FileLogger.cpp:49
juce::FileOutputStream
Definition juce_FileOutputStream.h:35
juce::FileOutputStream::openedOk
bool openedOk() const noexcept
Definition juce_FileOutputStream.h:91
juce::File
Definition juce_File.h:45
juce::File::getSize
int64 getSize() const
juce::File::getSpecialLocation
static File JUCE_CALLTYPE getSpecialLocation(const SpecialLocationType type)
juce::File::userApplicationDataDirectory
@ userApplicationDataDirectory
Definition juce_File.h:895
juce::File::create
Result create() const
Definition juce_File.cpp:498
juce::File::deleteFile
bool deleteFile() const
juce::File::exists
bool exists() const
juce::GenericScopedLock
Definition juce_ScopedLock.h:55
juce::InputStream::readByte
virtual char readByte()
Definition juce_InputStream.cpp:56
juce::OutputStream::writeFromInputStream
virtual int64 writeFromInputStream(InputStream &source, int64 maxNumBytesToWrite)
Definition juce_OutputStream.cpp:296
juce::String
Definition juce_String.h:53
juce::TemporaryFile
Definition juce_TemporaryFile.h:65
juce::TemporaryFile::overwriteTargetFileWithTemporary
bool overwriteTargetFileWithTemporary() const
Definition juce_TemporaryFile.cpp:93
juce::TemporaryFile::getFile
const File & getFile() const noexcept
Definition juce_TemporaryFile.h:126
juce::Time::getCurrentTime
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Definition juce_Time.cpp:233
juce::Time::toString
String toString(bool includeDate, bool includeTime, bool includeSeconds=true, bool use24HourClock=false) const
Definition juce_Time.cpp:304