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.

110 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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 "../io/files/juce_File.h"
  22. //==============================================================================
  23. /**
  24. A simple implemenation of a Logger that writes to a file.
  25. @see Logger
  26. */
  27. class JUCE_API FileLogger : public Logger
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates a FileLogger for a given file.
  32. @param fileToWriteTo the file that to use - new messages will be appended
  33. to the file. If the file doesn't exist, it will be created,
  34. along with any parent directories that are needed.
  35. @param welcomeMessage when opened, the logger will write a header to the log, along
  36. with the current date and time, and this welcome message
  37. @param maxInitialFileSizeBytes if this is zero or greater, then if the file already exists
  38. but is larger than this number of bytes, then the start of the
  39. file will be truncated to keep the size down. This prevents a log
  40. file getting ridiculously large over time. The file will be truncated
  41. at a new-line boundary. If this value is less than zero, no size limit
  42. will be imposed; if it's zero, the file will always be deleted. Note that
  43. the size is only checked once when this object is created - any logging
  44. that is done later will be appended without any checking
  45. */
  46. FileLogger (const File& fileToWriteTo,
  47. const String& welcomeMessage,
  48. const int maxInitialFileSizeBytes = 128 * 1024);
  49. /** Destructor. */
  50. ~FileLogger();
  51. //==============================================================================
  52. void logMessage (const String& message);
  53. //==============================================================================
  54. /** Helper function to create a log file in the correct place for this platform.
  55. On Windows this will return a logger with a path such as:
  56. c:\\Documents and Settings\\username\\Application Data\\[logFileSubDirectoryName]\\[logFileName]
  57. On the Mac it'll create something like:
  58. ~/Library/Logs/[logFileName]
  59. The method might return 0 if the file can't be created for some reason.
  60. @param logFileSubDirectoryName if a subdirectory is needed, this is what it will be called -
  61. it's best to use the something like the name of your application here.
  62. @param logFileName the name of the file to create, e.g. "MyAppLog.txt". Don't just
  63. call it "log.txt" because if it goes in a directory with logs
  64. from other applications (as it will do on the Mac) then no-one
  65. will know which one is yours!
  66. @param welcomeMessage a message that will be written to the log when it's opened.
  67. @param maxInitialFileSizeBytes (see the FileLogger constructor for more info on this)
  68. */
  69. static FileLogger* createDefaultAppLogger (const String& logFileSubDirectoryName,
  70. const String& logFileName,
  71. const String& welcomeMessage,
  72. const int maxInitialFileSizeBytes = 128 * 1024);
  73. //==============================================================================
  74. juce_UseDebuggingNewOperator
  75. private:
  76. File logFile;
  77. CriticalSection logLock;
  78. FileOutputStream* logStream;
  79. void trimFileSize (int maxFileSizeBytes) const;
  80. FileLogger (const FileLogger&);
  81. const FileLogger& operator= (const FileLogger&);
  82. };
  83. #endif // __JUCE_FILELOGGER_JUCEHEADER__