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.

154 lines
4.8KB

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