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.

134 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. FileLogger::FileLogger (const File& file,
  20. const String& welcomeMessage,
  21. const int64 maxInitialFileSizeBytes)
  22. : logFile (file)
  23. {
  24. if (maxInitialFileSizeBytes >= 0)
  25. trimFileSize (logFile, maxInitialFileSizeBytes);
  26. if (! file.exists())
  27. file.create(); // (to create the parent directories)
  28. String welcome;
  29. welcome << newLine
  30. << "**********************************************************" << newLine
  31. << welcomeMessage << newLine
  32. << "Log started: " << Time::getCurrentTime().toString (true, true) << newLine;
  33. FileLogger::logMessage (welcome);
  34. }
  35. FileLogger::~FileLogger() {}
  36. //==============================================================================
  37. void FileLogger::logMessage (const String& message)
  38. {
  39. const ScopedLock sl (logLock);
  40. DBG (message);
  41. FileOutputStream out (logFile, 256);
  42. out << message << newLine;
  43. }
  44. void FileLogger::trimFileSize (const File& file, int64 maxFileSizeBytes)
  45. {
  46. if (maxFileSizeBytes <= 0)
  47. {
  48. file.deleteFile();
  49. }
  50. else
  51. {
  52. const int64 fileSize = file.getSize();
  53. if (fileSize > maxFileSizeBytes)
  54. {
  55. TemporaryFile tempFile (file);
  56. {
  57. FileOutputStream out (tempFile.getFile());
  58. FileInputStream in (file);
  59. if (! (out.openedOk() && in.openedOk()))
  60. return;
  61. in.setPosition (fileSize - maxFileSizeBytes);
  62. for (;;)
  63. {
  64. const char c = in.readByte();
  65. if (c == 0)
  66. return;
  67. if (c == '\n' || c == '\r')
  68. {
  69. out << c;
  70. break;
  71. }
  72. }
  73. out.writeFromInputStream (in, -1);
  74. }
  75. tempFile.overwriteTargetFileWithTemporary();
  76. }
  77. }
  78. }
  79. //==============================================================================
  80. File FileLogger::getSystemLogFileFolder()
  81. {
  82. #if JUCE_MAC
  83. return File ("~/Library/Logs");
  84. #else
  85. return File::getSpecialLocation (File::userApplicationDataDirectory);
  86. #endif
  87. }
  88. FileLogger* FileLogger::createDefaultAppLogger (const String& logFileSubDirectoryName,
  89. const String& logFileName,
  90. const String& welcomeMessage,
  91. const int64 maxInitialFileSizeBytes)
  92. {
  93. return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
  94. .getChildFile (logFileName),
  95. welcomeMessage, maxInitialFileSizeBytes);
  96. }
  97. FileLogger* FileLogger::createDateStampedLogger (const String& logFileSubDirectoryName,
  98. const String& logFileNameRoot,
  99. const String& logFileNameSuffix,
  100. const String& welcomeMessage)
  101. {
  102. return new FileLogger (getSystemLogFileFolder().getChildFile (logFileSubDirectoryName)
  103. .getChildFile (logFileNameRoot + Time::getCurrentTime().formatted ("%Y-%m-%d_%H-%M-%S"))
  104. .withFileExtension (logFileNameSuffix)
  105. .getNonexistentSibling(),
  106. welcomeMessage, 0);
  107. }
  108. } // namespace juce