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.

138 lines
7.2KB

  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. #pragma once
  24. //==============================================================================
  25. /**
  26. A simple implementation of a Logger that writes to a file.
  27. @see Logger
  28. */
  29. class JUCE_API FileLogger : public Logger
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a FileLogger for a given file.
  34. @param fileToWriteTo the file that to use - new messages will be appended
  35. to the file. If the file doesn't exist, it will be created,
  36. along with any parent directories that are needed.
  37. @param welcomeMessage when opened, the logger will write a header to the log, along
  38. with the current date and time, and this welcome message
  39. @param maxInitialFileSizeBytes if this is zero or greater, then if the file already exists
  40. but is larger than this number of bytes, then the start of the
  41. file will be truncated to keep the size down. This prevents a log
  42. file getting ridiculously large over time. The file will be truncated
  43. at a new-line boundary. If this value is less than zero, no size limit
  44. will be imposed; if it's zero, the file will always be deleted. Note that
  45. the size is only checked once when this object is created - any logging
  46. that is done later will be appended without any checking
  47. */
  48. FileLogger (const File& fileToWriteTo,
  49. const String& welcomeMessage,
  50. const int64 maxInitialFileSizeBytes = 128 * 1024);
  51. /** Destructor. */
  52. ~FileLogger();
  53. //==============================================================================
  54. /** Returns the file that this logger is writing to. */
  55. const File& getLogFile() const noexcept { return logFile; }
  56. //==============================================================================
  57. /** Helper function to create a log file in the correct place for this platform.
  58. The method might return nullptr if the file can't be created for some reason.
  59. @param logFileSubDirectoryName the name of the subdirectory to create inside the logs folder (as
  60. returned by getSystemLogFileFolder). It's best to use something
  61. like the name of your application here.
  62. @param logFileName the name of the file to create, e.g. "MyAppLog.txt".
  63. @param welcomeMessage a message that will be written to the log when it's opened.
  64. @param maxInitialFileSizeBytes (see the FileLogger constructor for more info on this)
  65. */
  66. static FileLogger* createDefaultAppLogger (const String& logFileSubDirectoryName,
  67. const String& logFileName,
  68. const String& welcomeMessage,
  69. const int64 maxInitialFileSizeBytes = 128 * 1024);
  70. /** Helper function to create a log file in the correct place for this platform.
  71. The filename used is based on the root and suffix strings provided, along with a
  72. time and date string, meaning that a new, empty log file will be always be created
  73. rather than appending to an exising one.
  74. The method might return nullptr if the file can't be created for some reason.
  75. @param logFileSubDirectoryName the name of the subdirectory to create inside the logs folder (as
  76. returned by getSystemLogFileFolder). It's best to use something
  77. like the name of your application here.
  78. @param logFileNameRoot the start of the filename to use, e.g. "MyAppLog_". This will have
  79. a timestamp and the logFileNameSuffix appended to it
  80. @param logFileNameSuffix the file suffix to use, e.g. ".txt"
  81. @param welcomeMessage a message that will be written to the log when it's opened.
  82. */
  83. static FileLogger* createDateStampedLogger (const String& logFileSubDirectoryName,
  84. const String& logFileNameRoot,
  85. const String& logFileNameSuffix,
  86. const String& welcomeMessage);
  87. //==============================================================================
  88. /** Returns an OS-specific folder where log-files should be stored.
  89. On Windows this will return a logger with a path such as:
  90. c:\\Documents and Settings\\username\\Application Data\\[logFileSubDirectoryName]\\[logFileName]
  91. On the Mac it'll create something like:
  92. ~/Library/Logs/[logFileSubDirectoryName]/[logFileName]
  93. @see createDefaultAppLogger
  94. */
  95. static File getSystemLogFileFolder();
  96. // (implementation of the Logger virtual method)
  97. void logMessage (const String&);
  98. //==============================================================================
  99. /** This is a utility function which removes lines from the start of a text
  100. file to make sure that its total size is below the given size.
  101. */
  102. static void trimFileSize (const File& file, int64 maxFileSize);
  103. private:
  104. //==============================================================================
  105. File logFile;
  106. CriticalSection logLock;
  107. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileLogger)
  108. };