OpenShot Library | OpenShotAudio  0.2.1
juce_FileLogger.h
1 
2 /** @weakgroup juce_core-logging
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  A simple implementation of a Logger that writes to a file.
33 
34  @see Logger
35 
36  @tags{Core}
37 */
38 class JUCE_API FileLogger : public Logger
39 {
40 public:
41  //==============================================================================
42  /** Creates a FileLogger for a given file.
43 
44  @param fileToWriteTo the file that to use - new messages will be appended
45  to the file. If the file doesn't exist, it will be created,
46  along with any parent directories that are needed.
47  @param welcomeMessage when opened, the logger will write a header to the log, along
48  with the current date and time, and this welcome message
49  @param maxInitialFileSizeBytes if this is zero or greater, then if the file already exists
50  but is larger than this number of bytes, then the start of the
51  file will be truncated to keep the size down. This prevents a log
52  file getting ridiculously large over time. The file will be truncated
53  at a new-line boundary. If this value is less than zero, no size limit
54  will be imposed; if it's zero, the file will always be deleted. Note that
55  the size is only checked once when this object is created - any logging
56  that is done later will be appended without any checking
57  */
58  FileLogger (const File& fileToWriteTo,
59  const String& welcomeMessage,
60  const int64 maxInitialFileSizeBytes = 128 * 1024);
61 
62  /** Destructor. */
63  ~FileLogger() override;
64 
65  //==============================================================================
66  /** Returns the file that this logger is writing to. */
67  const File& getLogFile() const noexcept { return logFile; }
68 
69  //==============================================================================
70  /** Helper function to create a log file in the correct place for this platform.
71 
72  The method might return nullptr if the file can't be created for some reason.
73 
74  @param logFileSubDirectoryName the name of the subdirectory to create inside the logs folder (as
75  returned by getSystemLogFileFolder). It's best to use something
76  like the name of your application here.
77  @param logFileName the name of the file to create, e.g. "MyAppLog.txt".
78  @param welcomeMessage a message that will be written to the log when it's opened.
79  @param maxInitialFileSizeBytes (see the FileLogger constructor for more info on this)
80  */
81  static FileLogger* createDefaultAppLogger (const String& logFileSubDirectoryName,
82  const String& logFileName,
83  const String& welcomeMessage,
84  const int64 maxInitialFileSizeBytes = 128 * 1024);
85 
86  /** Helper function to create a log file in the correct place for this platform.
87 
88  The filename used is based on the root and suffix strings provided, along with a
89  time and date string, meaning that a new, empty log file will be always be created
90  rather than appending to an existing one.
91 
92  The method might return nullptr if the file can't be created for some reason.
93 
94  @param logFileSubDirectoryName the name of the subdirectory to create inside the logs folder (as
95  returned by getSystemLogFileFolder). It's best to use something
96  like the name of your application here.
97  @param logFileNameRoot the start of the filename to use, e.g. "MyAppLog_". This will have
98  a timestamp and the logFileNameSuffix appended to it
99  @param logFileNameSuffix the file suffix to use, e.g. ".txt"
100  @param welcomeMessage a message that will be written to the log when it's opened.
101  */
102  static FileLogger* createDateStampedLogger (const String& logFileSubDirectoryName,
103  const String& logFileNameRoot,
104  const String& logFileNameSuffix,
105  const String& welcomeMessage);
106 
107  //==============================================================================
108  /** Returns an OS-specific folder where log-files should be stored.
109 
110  On Windows this will return a logger with a path such as:
111  c:\\Documents and Settings\\username\\Application Data\\[logFileSubDirectoryName]\\[logFileName]
112 
113  On the Mac it'll create something like:
114  ~/Library/Logs/[logFileSubDirectoryName]/[logFileName]
115 
116  @see createDefaultAppLogger
117  */
118  static File getSystemLogFileFolder();
119 
120  // (implementation of the Logger virtual method)
121  void logMessage (const String&) override;
122 
123  //==============================================================================
124  /** This is a utility function which removes lines from the start of a text
125  file to make sure that its total size is below the given size.
126  */
127  static void trimFileSize (const File& file, int64 maxFileSize);
128 
129 private:
130  //==============================================================================
131  File logFile;
132  CriticalSection logLock;
133 
134  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileLogger)
135 };
136 
137 } // namespace juce
138 
139 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
The JUCE String class!
Definition: juce_String.h:42
const File & getLogFile() const noexcept
Returns the file that this logger is writing to.
A simple implementation of a Logger that writes to a file.
Acts as an application-wide logging class.
Definition: juce_Logger.h:44
Represents a local file or directory.
Definition: juce_File.h:44
A re-entrant mutex.