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.

109 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_FILELOGGER_JUCEHEADER__
  19. #define __JUCE_FILELOGGER_JUCEHEADER__
  20. #include "juce_Logger.h"
  21. #include "../files/juce_File.h"
  22. #include "../memory/juce_ScopedPointer.h"
  23. //==============================================================================
  24. /**
  25. A simple implemenation 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 int maxInitialFileSizeBytes = 128 * 1024);
  50. /** Destructor. */
  51. ~FileLogger();
  52. //==============================================================================
  53. void logMessage (const String& message);
  54. File getLogFile() const { return logFile; }
  55. //==============================================================================
  56. /** Helper function to create a log file in the correct place for this platform.
  57. On Windows this will return a logger with a path such as:
  58. c:\\Documents and Settings\\username\\Application Data\\[logFileSubDirectoryName]\\[logFileName]
  59. On the Mac it'll create something like:
  60. ~/Library/Logs/[logFileName]
  61. The method might return 0 if the file can't be created for some reason.
  62. @param logFileSubDirectoryName if a subdirectory is needed, this is what it will be called -
  63. it's best to use the something like the name of your application here.
  64. @param logFileName the name of the file to create, e.g. "MyAppLog.txt". Don't just
  65. call it "log.txt" because if it goes in a directory with logs
  66. from other applications (as it will do on the Mac) then no-one
  67. will know which one is yours!
  68. @param welcomeMessage a message that will be written to the log when it's opened.
  69. @param maxInitialFileSizeBytes (see the FileLogger constructor for more info on this)
  70. */
  71. static FileLogger* createDefaultAppLogger (const String& logFileSubDirectoryName,
  72. const String& logFileName,
  73. const String& welcomeMessage,
  74. const int maxInitialFileSizeBytes = 128 * 1024);
  75. private:
  76. //==============================================================================
  77. File logFile;
  78. CriticalSection logLock;
  79. void trimFileSize (int maxFileSizeBytes) const;
  80. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileLogger);
  81. };
  82. #endif // __JUCE_FILELOGGER_JUCEHEADER__