-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversionLog.R
53 lines (47 loc) · 1.7 KB
/
conversionLog.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
initConversionLog <- function(studentName, studentDirectory) {
options(
conversionLog = list(
StudentName = studentName,
Messages = list(paste0("Reading from: ", studentDirectory))
)
)
}
messageConversionLog <- function(...) {
message <- paste0(...)
conversionLog <- getOption("conversionLog", list())
if (length(conversionLog) == 0) {
warning("Conversion log was not initiated. Trashing the message: ", message)
}
conversionLog$Messages <- append(conversionLog$Messages, message)
options(conversionLog = conversionLog)
}
.writeConversionLog <- function(logFilePath) {
conversionLog <- getOption("conversionLog", list())
if (length(conversionLog) == 0) {
warning("Conversion log was not initiated. Can not write out.")
}
tryCatch({
logFile <- file(description = logFilePath, open = "w")
writeLines(text = unlist(conversionLog$Messages), con = logFile)
},
error = function(e) {
warning(paste0("Unable to write log file: ", logFilePath))
},
finally = close(logFile))
}
writeSuccessfullConversionLog <- function(outputDirectory) {
conversionLog <- getOption("conversionLog", list())
if (length(conversionLog) == 0) {
warning("Conversion log was not initiated. Can not write out.")
}
logFilePath <- file.path(outputDirectory, paste0(conversionLog$StudentName, ".txt"))
.writeConversionLog(logFilePath)
}
writeFailedConversionLog <- function(outputDirectory) {
conversionLog <- getOption("conversionLog", list())
if (length(conversionLog) == 0) {
warning("Conversion log was not initiated. Can not write out.")
}
logFilePath <- file.path(outputDirectory, paste0(conversionLog$StudentName, ".failed.txt"))
.writeConversionLog(logFilePath)
}