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.

137 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. FileLogger::FileLogger (const File& file,
  24. const String& welcomeMessage,
  25. const int64 maxInitialFileSizeBytes)
  26. : logFile (file)
  27. {
  28. if (maxInitialFileSizeBytes >= 0)
  29. trimFileSize (logFile, maxInitialFileSizeBytes);
  30. if (! file.exists())
  31. file.create(); // (to create the parent directories)
  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. void FileLogger::logMessage (const String& message)
  42. {
  43. const ScopedLock sl (logLock);
  44. DBG (message);
  45. FileOutputStream out (logFile, 256);
  46. out << message << newLine;
  47. }
  48. void FileLogger::trimFileSize (const File& file, int64 maxFileSizeBytes)
  49. {
  50. if (maxFileSizeBytes <= 0)
  51. {
  52. file.deleteFile();
  53. }
  54. else
  55. {
  56. const int64 fileSize = file.getSize();
  57. if (fileSize > maxFileSizeBytes)
  58. {
  59. TemporaryFile tempFile (file);
  60. {
  61. FileOutputStream out (tempFile.getFile());
  62. FileInputStream in (file);
  63. if (! (out.openedOk() && in.openedOk()))
  64. return;
  65. in.setPosition (fileSize - maxFileSizeBytes);
  66. for (;;)
  67. {
  68. const char c = in.readByte();
  69. if (c == 0)
  70. return;
  71. if (c == '\n' || c == '\r')
  72. {
  73. out << c;
  74. break;
  75. }
  76. }
  77. out.writeFromInputStream (in, -1);
  78. }
  79. tempFile.overwriteTargetFileWithTemporary();
  80. }
  81. }
  82. }
  83. //==============================================================================
  84. File FileLogger::getSystemLogFileFolder()
  85. {
  86. #if JUCE_MAC
  87. return File ("~/Library/Logs");
  88. #else
  89. return File::getSpecialLocation (File::userApplicationDataDirectory);
  90. #endif
  91. }
  92. FileLogger* FileLogger::createDefaultAppLogger (const String& logFileSubDirectoryName,
  93. const String& logFileName,
  94. const String& welcomeMessage,
  95. const int64 maxInitialFileSizeBytes)
  96. {
  97. return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
  98. .getChildFile (logFileName),
  99. welcomeMessage, maxInitialFileSizeBytes);
  100. }
  101. FileLogger* FileLogger::createDateStampedLogger (const String& logFileSubDirectoryName,
  102. const String& logFileNameRoot,
  103. const String& logFileNameSuffix,
  104. const String& welcomeMessage)
  105. {
  106. return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
  107. .getChildFile (logFileNameRoot + Time::getCurrentTime().formatted ("%Y-%m-%d_%H-%M-%S"))
  108. .withFileExtension (logFileNameSuffix)
  109. .getNonexistentSibling(),
  110. welcomeMessage, 0);
  111. }