JUCE  v6.1.6 (6.0.8-1114)
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::FileLogger Class Reference

A simple implementation of a Logger that writes to a file. More...

#include <juce_FileLogger.h>

Inheritance diagram for juce::FileLogger:
Collaboration diagram for juce::FileLogger:

Public Member Functions

 FileLogger (const File &fileToWriteTo, const String &welcomeMessage, const int64 maxInitialFileSizeBytes=128 *1024)
 Creates a FileLogger for a given file. More...
 
 ~FileLogger () override
 Destructor. More...
 
const FilegetLogFile () const noexcept
 Returns the file that this logger is writing to. More...
 
void logMessage (const String &) override
 This is overloaded by subclasses to implement custom logging behaviour. More...
 

Static Public Member Functions

static FileLoggercreateDateStampedLogger (const String &logFileSubDirectoryName, const String &logFileNameRoot, const String &logFileNameSuffix, const String &welcomeMessage)
 Helper function to create a log file in the correct place for this platform. More...
 
static FileLoggercreateDefaultAppLogger (const String &logFileSubDirectoryName, const String &logFileName, const String &welcomeMessage, const int64 maxInitialFileSizeBytes=128 *1024)
 Helper function to create a log file in the correct place for this platform. More...
 
static LoggergetCurrentLogger () noexcept
 Returns the current logger, or nullptr if no custom logger has been set. More...
 
static File getSystemLogFileFolder ()
 Returns an OS-specific folder where log-files should be stored. More...
 
static void outputDebugString (const String &text)
 Writes a message to the standard error stream. More...
 
static void setCurrentLogger (Logger *newLogger) noexcept
 Sets the current logging class to use. More...
 
static void trimFileSize (const File &file, int64 maxFileSize)
 This is a utility function which removes lines from the start of a text file to make sure that its total size is below the given size. More...
 
static void writeToLog (const String &message)
 Writes a string to the current logger. More...
 

Private Attributes

File logFile
 
CriticalSection logLock
 

Static Private Attributes

static LoggercurrentLogger
 

Detailed Description

A simple implementation of a Logger that writes to a file.

See also
Logger

@tags{Core}

Constructor & Destructor Documentation

◆ FileLogger()

juce::FileLogger::FileLogger ( const File fileToWriteTo,
const String welcomeMessage,
const int64  maxInitialFileSizeBytes = 128 *1024 
)

Creates a FileLogger for a given file.

Parameters
fileToWriteTothe file that to use - new messages will be appended to the file. If the file doesn't exist, it will be created, along with any parent directories that are needed.
welcomeMessagewhen opened, the logger will write a header to the log, along with the current date and time, and this welcome message
maxInitialFileSizeBytesif this is zero or greater, then if the file already exists but is larger than this number of bytes, then the start of the file will be truncated to keep the size down. This prevents a log file getting ridiculously large over time. The file will be truncated at a new-line boundary. If this value is less than zero, no size limit will be imposed; if it's zero, the file will always be deleted. Note that the size is only checked once when this object is created - any logging that is done later will be appended without any checking

◆ ~FileLogger()

juce::FileLogger::~FileLogger ( )
override

Destructor.

Member Function Documentation

◆ createDateStampedLogger()

static FileLogger* juce::FileLogger::createDateStampedLogger ( const String logFileSubDirectoryName,
const String logFileNameRoot,
const String logFileNameSuffix,
const String welcomeMessage 
)
static

Helper function to create a log file in the correct place for this platform.

The filename used is based on the root and suffix strings provided, along with a time and date string, meaning that a new, empty log file will be always be created rather than appending to an existing one.

The method might return nullptr if the file can't be created for some reason.

Parameters
logFileSubDirectoryNamethe name of the subdirectory to create inside the logs folder (as returned by getSystemLogFileFolder). It's best to use something like the name of your application here.
logFileNameRootthe start of the filename to use, e.g. "MyAppLog_". This will have a timestamp and the logFileNameSuffix appended to it
logFileNameSuffixthe file suffix to use, e.g. ".txt"
welcomeMessagea message that will be written to the log when it's opened.

◆ createDefaultAppLogger()

static FileLogger* juce::FileLogger::createDefaultAppLogger ( const String logFileSubDirectoryName,
const String logFileName,
const String welcomeMessage,
const int64  maxInitialFileSizeBytes = 128 *1024 
)
static

Helper function to create a log file in the correct place for this platform.

The method might return nullptr if the file can't be created for some reason.

Parameters
logFileSubDirectoryNamethe name of the subdirectory to create inside the logs folder (as returned by getSystemLogFileFolder). It's best to use something like the name of your application here.
logFileNamethe name of the file to create, e.g. "MyAppLog.txt".
welcomeMessagea message that will be written to the log when it's opened.
maxInitialFileSizeBytes(see the FileLogger constructor for more info on this)

◆ getCurrentLogger()

static Logger* juce::Logger::getCurrentLogger ( )
staticnoexceptinherited

Returns the current logger, or nullptr if no custom logger has been set.

◆ getLogFile()

const File& juce::FileLogger::getLogFile ( ) const
inlinenoexcept

Returns the file that this logger is writing to.

◆ getSystemLogFileFolder()

static File juce::FileLogger::getSystemLogFileFolder ( )
static

Returns an OS-specific folder where log-files should be stored.

On Windows this will return a logger with a path such as: c:\Documents and Settings\username\Application Data\[logFileSubDirectoryName]\[logFileName]

On the Mac it'll create something like: ~/Library/Logs/[logFileSubDirectoryName]/[logFileName]

See also
createDefaultAppLogger

◆ logMessage()

void juce::FileLogger::logMessage ( const String message)
overridevirtual

This is overloaded by subclasses to implement custom logging behaviour.

See also
setCurrentLogger

Implements juce::Logger.

◆ outputDebugString()

static void juce::Logger::outputDebugString ( const String text)
staticinherited

Writes a message to the standard error stream.

This can be called directly, or by using the DBG() macro in juce_PlatformDefs.h (which will avoid calling the method in non-debug builds).

◆ setCurrentLogger()

static void juce::Logger::setCurrentLogger ( Logger newLogger)
staticnoexceptinherited

Sets the current logging class to use.

Note that the object passed in will not be owned or deleted by the logger, so the caller must make sure that it is not deleted while still being used. A null pointer can be passed-in to reset the system to the default logger.

◆ trimFileSize()

static void juce::FileLogger::trimFileSize ( const File file,
int64  maxFileSize 
)
static

This is a utility function which removes lines from the start of a text file to make sure that its total size is below the given size.

◆ writeToLog()

static void juce::Logger::writeToLog ( const String message)
staticinherited

Writes a string to the current logger.

This will pass the string to the logger's logMessage() method if a logger has been set.

See also
logMessage

Member Data Documentation

◆ currentLogger

Logger* juce::Logger::currentLogger
staticprivateinherited

◆ logFile

File juce::FileLogger::logFile
private

◆ logLock

CriticalSection juce::FileLogger::logLock
private

The documentation for this class was generated from the following file: