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.

130 lines
6.7KB

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