FileLogHandler.kt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.logger
  21. import java.io.File
  22. import java.io.FileNotFoundException
  23. import java.io.FileOutputStream
  24. import java.io.IOException
  25. import java.nio.charset.Charset
  26. /**
  27. * Very simple log writer with file rotations.
  28. *
  29. * Files are rotated when writing entry causes log file to exceed it's maximum size.
  30. * Last entry is not truncated and final log file can exceed max file size, but
  31. * no further entries will be written to it.
  32. */
  33. internal class FileLogHandler(private val logDir: File, private val logFilename: String, private val maxSize: Long) {
  34. data class RawLogs(val lines: List<String>, val logSize: Long)
  35. companion object {
  36. const val ROTATED_LOGS_COUNT = 3
  37. }
  38. private var writer: FileOutputStream? = null
  39. private var size: Long = 0
  40. private val rotationList = listOf(
  41. "$logFilename.2",
  42. "$logFilename.1",
  43. "$logFilename.0",
  44. logFilename
  45. )
  46. val logFile: File
  47. get() {
  48. return File(logDir, logFilename)
  49. }
  50. val isOpened: Boolean
  51. get() {
  52. return writer != null
  53. }
  54. val maxLogFilesCount get() = rotationList.size
  55. fun open() {
  56. try {
  57. writer = FileOutputStream(logFile, true)
  58. size = logFile.length()
  59. } catch (ex: FileNotFoundException) {
  60. logFile.parentFile.mkdirs()
  61. writer = FileOutputStream(logFile, true)
  62. size = logFile.length()
  63. }
  64. }
  65. fun write(logEntry: String) {
  66. val rawLogEntry = logEntry.toByteArray(Charset.forName("UTF-8"))
  67. writer?.write(rawLogEntry)
  68. size += rawLogEntry.size
  69. if (size > maxSize) {
  70. rotateLogs()
  71. }
  72. }
  73. fun close() {
  74. writer?.close()
  75. writer = null
  76. size = 0L
  77. }
  78. fun deleteAll() {
  79. rotationList
  80. .map { File(logDir, it) }
  81. .forEach { it.delete() }
  82. }
  83. fun rotateLogs() {
  84. val rotatatingOpenedLog = isOpened
  85. if (rotatatingOpenedLog) {
  86. close()
  87. }
  88. val existingLogFiles = logDir.listFiles().associate { it.name to it }
  89. existingLogFiles[rotationList.first()]?.delete()
  90. for (i in 0 until rotationList.size - 1) {
  91. val nextFile = File(logDir, rotationList[i])
  92. val previousFile = existingLogFiles[rotationList[i + 1]]
  93. previousFile?.renameTo(nextFile)
  94. }
  95. if (rotatatingOpenedLog) {
  96. open()
  97. }
  98. }
  99. fun loadLogFiles(rotated: Int = ROTATED_LOGS_COUNT): RawLogs {
  100. if (rotated < 0) {
  101. throw IllegalArgumentException("Negative index")
  102. }
  103. val allLines = mutableListOf<String>()
  104. var size = 0L
  105. for (i in 0..Math.min(rotated, rotationList.size - 1)) {
  106. val file = File(logDir, rotationList[i])
  107. if (!file.exists()) continue
  108. try {
  109. val lines = file.readLines(Charsets.UTF_8)
  110. allLines.addAll(lines)
  111. size += file.length()
  112. } catch (ex: IOException) {
  113. // ignore failing file
  114. }
  115. }
  116. return RawLogs(lines = allLines, logSize = size)
  117. }
  118. }