juce_FileLogger.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_FILELOGGER_H_INCLUDED
  22. #define JUCE_FILELOGGER_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. A simple implementation of a Logger that writes to a file.
  26. @see Logger
  27. */
  28. class JUCE_API FileLogger : public Logger
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a FileLogger for a given file.
  33. @param fileToWriteTo the file that to use - new messages will be appended
  34. to the file. If the file doesn't exist, it will be created,
  35. along with any parent directories that are needed.
  36. @param welcomeMessage when opened, the logger will write a header to the log, along
  37. with the current date and time, and this welcome message
  38. @param maxInitialFileSizeBytes if this is zero or greater, then if the file already exists
  39. but is larger than this number of bytes, then the start of the
  40. file will be truncated to keep the size down. This prevents a log
  41. file getting ridiculously large over time. The file will be truncated
  42. at a new-line boundary. If this value is less than zero, no size limit
  43. will be imposed; if it's zero, the file will always be deleted. Note that
  44. the size is only checked once when this object is created - any logging
  45. that is done later will be appended without any checking
  46. */
  47. FileLogger (const File& fileToWriteTo,
  48. const String& welcomeMessage,
  49. const int64 maxInitialFileSizeBytes = 128 * 1024);
  50. /** Destructor. */
  51. ~FileLogger();
  52. //==============================================================================
  53. /** Returns the file that this logger is writing to. */
  54. const File& getLogFile() const noexcept { return logFile; }
  55. //==============================================================================
  56. /** Helper function to create a log file in the correct place for this platform.
  57. The method might return nullptr if the file can't be created for some reason.
  58. @param logFileSubDirectoryName the name of the subdirectory to create inside the logs folder (as
  59. returned by getSystemLogFileFolder). It's best to use something
  60. like the name of your application here.
  61. @param logFileName the name of the file to create, e.g. "MyAppLog.txt".
  62. @param welcomeMessage a message that will be written to the log when it's opened.
  63. @param maxInitialFileSizeBytes (see the FileLogger constructor for more info on this)
  64. */
  65. static FileLogger* createDefaultAppLogger (const String& logFileSubDirectoryName,
  66. const String& logFileName,
  67. const String& welcomeMessage,
  68. const int64 maxInitialFileSizeBytes = 128 * 1024);
  69. /** Helper function to create a log file in the correct place for this platform.
  70. The filename used is based on the root and suffix strings provided, along with a
  71. time and date string, meaning that a new, empty log file will be always be created
  72. rather than appending to an exising one.
  73. The method might return nullptr if the file can't be created for some reason.
  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 logFileNameRoot the start of the filename to use, e.g. "MyAppLog_". This will have
  78. a timestamp and the logFileNameSuffix appended to it
  79. @param logFileNameSuffix the file suffix to use, e.g. ".txt"
  80. @param welcomeMessage a message that will be written to the log when it's opened.
  81. */
  82. static FileLogger* createDateStampedLogger (const String& logFileSubDirectoryName,
  83. const String& logFileNameRoot,
  84. const String& logFileNameSuffix,
  85. const String& welcomeMessage);
  86. //==============================================================================
  87. /** Returns an OS-specific folder where log-files should be stored.
  88. On Windows this will return a logger with a path such as:
  89. c:\\Documents and Settings\\username\\Application Data\\[logFileSubDirectoryName]\\[logFileName]
  90. On the Mac it'll create something like:
  91. ~/Library/Logs/[logFileSubDirectoryName]/[logFileName]
  92. @see createDefaultAppLogger
  93. */
  94. static File getSystemLogFileFolder();
  95. // (implementation of the Logger virtual method)
  96. void logMessage (const String&);
  97. private:
  98. //==============================================================================
  99. File logFile;
  100. CriticalSection logLock;
  101. void trimFileSize (int64 maxFileSizeBytes) const;
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileLogger)
  103. };
  104. #endif // JUCE_FILELOGGER_H_INCLUDED