-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hexacraft.main | ||
|
||
import hexacraft.infra.fs.FileSystem | ||
|
||
import java.io.{ByteArrayOutputStream, File, PrintStream} | ||
import java.time.OffsetDateTime | ||
|
||
class MainErrorLogger(saveToFile: Boolean, saveFolder: File, fs: FileSystem) { | ||
def log(e: Throwable): Unit = if saveToFile then logToFile(e) else logToConsole(e) | ||
|
||
private def logToConsole(e: Throwable): Unit = | ||
e.printStackTrace() | ||
|
||
private def logToFile(e: Throwable): Unit = | ||
val byteStream = new ByteArrayOutputStream() | ||
e.printStackTrace(new PrintStream(byteStream)) | ||
|
||
val now = OffsetDateTime.now() | ||
val logFile = new File(saveFolder, s"logs/error_$now.log".replace(':', '.')) | ||
|
||
fs.writeBytes(logFile.toPath, byteStream.toByteArray) | ||
|
||
System.err.println( | ||
s"The program has crashed. The crash report can be found in: ${logFile.getAbsolutePath}" | ||
) | ||
} | ||
|
||
object MainErrorLogger { | ||
def create(saveToFile: Boolean, saveFolder: File): MainErrorLogger = | ||
MainErrorLogger(saveToFile, saveFolder, FileSystem.create()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
game/src/test/scala/hexacraft/main/MainErrorLoggerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package hexacraft.main | ||
|
||
import hexacraft.infra.fs.FileSystem | ||
|
||
import munit.FunSuite | ||
|
||
import java.io.{ByteArrayOutputStream, File, PrintStream} | ||
import java.nio.file.Path | ||
|
||
class MainErrorLoggerTest extends FunSuite { | ||
test("does not write log files in debug mode") { | ||
val fs = FileSystem.createNull() | ||
val tracker = fs.trackWrites() | ||
|
||
val logger = MainErrorLogger(false, null, fs) | ||
captureStdErr(logger.log(new Exception("something happened"))) | ||
|
||
assertEquals(tracker.events, Seq()) | ||
} | ||
|
||
test("writes stacktrace to stderr in debug mode") { | ||
val fs = FileSystem.createNull() | ||
val logger = MainErrorLogger(false, null, fs) | ||
|
||
val output = captureStdErr(logger.log(new Exception("something happened"))) | ||
|
||
val lines = output.lines().toList | ||
assert(lines.size() > 1) | ||
assertEquals(lines.get(0), "java.lang.Exception: something happened") | ||
} | ||
|
||
test("write a log file in release mode") { | ||
val fs = FileSystem.createNull() | ||
val tracker = fs.trackWrites() | ||
|
||
val saveFolder = File("some/path") | ||
val logger = MainErrorLogger(true, saveFolder, fs) | ||
captureStdErr(logger.log(new Exception("something happened"))) | ||
|
||
assertEquals(tracker.events.size, 1) | ||
val writeEvent = tracker.events(0) | ||
|
||
val logsFolder = writeEvent.path.getParent | ||
assertEquals(logsFolder, Path.of("some/path/logs")) | ||
|
||
val logFileName = writeEvent.path.getFileName.toString | ||
assert(logFileName.startsWith("error_")) | ||
assert(logFileName.endsWith(".log")) | ||
|
||
val logLines = String(writeEvent.bytes.toArray).lines().toList | ||
assert(logLines.size() > 1) | ||
assertEquals(logLines.get(0), "java.lang.Exception: something happened") | ||
} | ||
|
||
// TODO: the check below should probably live in FileSystem instead | ||
// On Windows colons may not be used in file names | ||
test("does not use colon in the log file name") { | ||
val fs = FileSystem.createNull() | ||
val tracker = fs.trackWrites() | ||
|
||
val saveFolder = File("some/path") | ||
val logger = MainErrorLogger(true, saveFolder, fs) | ||
captureStdErr(logger.log(new Exception("something happened"))) | ||
|
||
assertEquals(tracker.events.size, 1) | ||
|
||
val logFile = tracker.events(0).path | ||
val logFileName = logFile.getFileName.toString | ||
assert(!logFileName.contains(':'), s"'$logFileName' contains a colon") | ||
} | ||
|
||
test("writes to stderr to mention the log file in release mode") { | ||
val fs = FileSystem.createNull() | ||
val tracker = fs.trackWrites() | ||
|
||
val saveFolder = File("some/path") | ||
val logger = MainErrorLogger(true, saveFolder, fs) | ||
|
||
val output = captureStdErr(logger.log(new Exception("something happened"))) | ||
|
||
assertEquals(tracker.events.size, 1) | ||
val writeEvent = tracker.events(0) | ||
val logFileAbsolutePath = writeEvent.path.toAbsolutePath.toString | ||
|
||
assert(output.contains(logFileAbsolutePath)) | ||
} | ||
|
||
def captureStdErr(code: => Unit): String = { | ||
val prevErr = System.err | ||
try | ||
val err = ByteArrayOutputStream() | ||
System.setErr(PrintStream(err)) | ||
|
||
code | ||
|
||
String(err.toByteArray) | ||
finally System.setErr(prevErr) | ||
} | ||
} |