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.

138 lines
4.5KB

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