juce_FileLogger.cpp 5.0 KB

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