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.

132 lines
4.8KB

  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. FileLogger::FileLogger (const File& file,
  19. const String& welcomeMessage,
  20. const int64 maxInitialFileSizeBytes)
  21. : logFile (file)
  22. {
  23. if (maxInitialFileSizeBytes >= 0)
  24. trimFileSize (maxInitialFileSizeBytes);
  25. if (! file.exists())
  26. file.create(); // (to create the parent directories)
  27. String welcome;
  28. welcome << newLine
  29. << "**********************************************************" << newLine
  30. << welcomeMessage << newLine
  31. << "Log started: " << Time::getCurrentTime().toString (true, true) << newLine;
  32. FileLogger::logMessage (welcome);
  33. }
  34. FileLogger::~FileLogger() {}
  35. //==============================================================================
  36. void FileLogger::logMessage (const String& message)
  37. {
  38. const ScopedLock sl (logLock);
  39. DBG (message);
  40. FileOutputStream out (logFile, 256);
  41. out << message << newLine;
  42. }
  43. void FileLogger::trimFileSize (int64 maxFileSizeBytes) const
  44. {
  45. if (maxFileSizeBytes <= 0)
  46. {
  47. logFile.deleteFile();
  48. }
  49. else
  50. {
  51. const int64 fileSize = logFile.getSize();
  52. if (fileSize > maxFileSizeBytes)
  53. {
  54. TemporaryFile tempFile (logFile);
  55. {
  56. FileOutputStream out (tempFile.getFile());
  57. FileInputStream in (logFile);
  58. if (! (out.openedOk() && in.openedOk()))
  59. return;
  60. in.setPosition (fileSize - maxFileSizeBytes);
  61. for (;;)
  62. {
  63. const char c = in.readByte();
  64. if (c == 0)
  65. return;
  66. if (c == '\n' || c == '\r')
  67. {
  68. out << c;
  69. break;
  70. }
  71. }
  72. out.writeFromInputStream (in, -1);
  73. }
  74. tempFile.overwriteTargetFileWithTemporary();
  75. }
  76. }
  77. }
  78. //==============================================================================
  79. File FileLogger::getSystemLogFileFolder()
  80. {
  81. #if JUCE_MAC
  82. return File ("~/Library/Logs");
  83. #else
  84. return File::getSpecialLocation (File::userApplicationDataDirectory);
  85. #endif
  86. }
  87. FileLogger* FileLogger::createDefaultAppLogger (const String& logFileSubDirectoryName,
  88. const String& logFileName,
  89. const String& welcomeMessage,
  90. const int64 maxInitialFileSizeBytes)
  91. {
  92. return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
  93. .getChildFile (logFileName),
  94. welcomeMessage, maxInitialFileSizeBytes);
  95. }
  96. FileLogger* FileLogger::createDateStampedLogger (const String& logFileSubDirectoryName,
  97. const String& logFileNameRoot,
  98. const String& logFileNameSuffix,
  99. const String& welcomeMessage)
  100. {
  101. return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
  102. .getChildFile (logFileNameRoot + Time::getCurrentTime().formatted ("%Y-%m-%d_%H-%M-%S"))
  103. .withFileExtension (logFileNameSuffix)
  104. .getNonexistentSibling(),
  105. welcomeMessage, 0);
  106. }