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.

133 lines
4.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. FileLogger::FileLogger (const File& logFile_,
  19. const String& welcomeMessage,
  20. const int maxInitialFileSizeBytes)
  21. : logFile (logFile_)
  22. {
  23. if (maxInitialFileSizeBytes >= 0)
  24. trimFileSize (maxInitialFileSizeBytes);
  25. if (! logFile_.exists())
  26. {
  27. // do this so that the parent directories get created..
  28. logFile_.create();
  29. }
  30. String welcome;
  31. welcome << newLine
  32. << "**********************************************************" << newLine
  33. << welcomeMessage << newLine
  34. << "Log started: " << Time::getCurrentTime().toString (true, true) << newLine;
  35. FileLogger::logMessage (welcome);
  36. }
  37. FileLogger::~FileLogger()
  38. {
  39. }
  40. //==============================================================================
  41. void FileLogger::logMessage (const String& message)
  42. {
  43. DBG (message);
  44. const ScopedLock sl (logLock);
  45. FileOutputStream out (logFile, 256);
  46. out << message << newLine;
  47. }
  48. void FileLogger::trimFileSize (int maxFileSizeBytes) const
  49. {
  50. if (maxFileSizeBytes <= 0)
  51. {
  52. logFile.deleteFile();
  53. }
  54. else
  55. {
  56. const int64 fileSize = logFile.getSize();
  57. if (fileSize > maxFileSizeBytes)
  58. {
  59. ScopedPointer <FileInputStream> in (logFile.createInputStream());
  60. jassert (in != nullptr);
  61. if (in != nullptr)
  62. {
  63. in->setPosition (fileSize - maxFileSizeBytes);
  64. String content;
  65. {
  66. MemoryBlock contentToSave;
  67. contentToSave.setSize ((size_t) maxFileSizeBytes + 4);
  68. contentToSave.fillWith (0);
  69. in->read (contentToSave.getData(), maxFileSizeBytes);
  70. in = nullptr;
  71. content = contentToSave.toString();
  72. }
  73. int newStart = 0;
  74. while (newStart < fileSize
  75. && content[newStart] != '\n'
  76. && content[newStart] != '\r')
  77. ++newStart;
  78. logFile.deleteFile();
  79. logFile.appendText (content.substring (newStart), false, false);
  80. }
  81. }
  82. }
  83. }
  84. //==============================================================================
  85. FileLogger* FileLogger::createDefaultAppLogger (const String& logFileSubDirectoryName,
  86. const String& logFileName,
  87. const String& welcomeMessage,
  88. const int maxInitialFileSizeBytes)
  89. {
  90. #if JUCE_MAC
  91. File logFile ("~/Library/Logs");
  92. logFile = logFile.getChildFile (logFileSubDirectoryName)
  93. .getChildFile (logFileName);
  94. #else
  95. File logFile (File::getSpecialLocation (File::userApplicationDataDirectory));
  96. if (logFile.isDirectory())
  97. {
  98. logFile = logFile.getChildFile (logFileSubDirectoryName)
  99. .getChildFile (logFileName);
  100. }
  101. #endif
  102. return new FileLogger (logFile, welcomeMessage, maxInitialFileSizeBytes);
  103. }