Loading...
Searching...
No Matches
juce_FileLogger.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
FileLogger::FileLogger (const File& file,
const String& welcomeMessage,
const int64 maxInitialFileSizeBytes)
: logFile (file)
{
if (maxInitialFileSizeBytes >= 0)
trimFileSize (logFile, maxInitialFileSizeBytes);
if (! file.exists())
file.create(); // (to create the parent directories)
String welcome;
welcome << newLine
<< "**********************************************************" << newLine
<< welcomeMessage << newLine
<< "Log started: " << Time::getCurrentTime().toString (true, true) << newLine;
FileLogger::logMessage (welcome);
}
FileLogger::~FileLogger() {}
//==============================================================================
void FileLogger::logMessage (const String& message)
{
const ScopedLock sl (logLock);
DBG (message);
FileOutputStream out (logFile, 256);
out << message << newLine;
}
void FileLogger::trimFileSize (const File& file, int64 maxFileSizeBytes)
{
if (maxFileSizeBytes <= 0)
{
file.deleteFile();
}
else
{
const int64 fileSize = file.getSize();
if (fileSize > maxFileSizeBytes)
{
TemporaryFile tempFile (file);
{
FileOutputStream out (tempFile.getFile());
FileInputStream in (file);
if (! (out.openedOk() && in.openedOk()))
return;
in.setPosition (fileSize - maxFileSizeBytes);
for (;;)
{
const char c = in.readByte();
if (c == 0)
return;
if (c == '\n' || c == '\r')
{
out << c;
break;
}
}
out.writeFromInputStream (in, -1);
}
tempFile.overwriteTargetFileWithTemporary();
}
}
}
//==============================================================================
File FileLogger::getSystemLogFileFolder()
{
#if JUCE_MAC
return File ("~/Library/Logs");
#else
return File::getSpecialLocation (File::userApplicationDataDirectory);
#endif
}
FileLogger* FileLogger::createDefaultAppLogger (const String& logFileSubDirectoryName,
const String& logFileName,
const String& welcomeMessage,
const int64 maxInitialFileSizeBytes)
{
return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
.getChildFile (logFileName),
welcomeMessage, maxInitialFileSizeBytes);
}
FileLogger* FileLogger::createDateStampedLogger (const String& logFileSubDirectoryName,
const String& logFileNameRoot,
const String& logFileNameSuffix,
const String& welcomeMessage)
{
return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
.getChildFile (logFileNameRoot + Time::getCurrentTime().formatted ("%Y-%m-%d_%H-%M-%S"))
.withFileExtension (logFileNameSuffix)
.getNonexistentSibling(),
welcomeMessage, 0);
}
} // 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::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::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