The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
7.3KB

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