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.

134 lines
6.7KB

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