summaryrefslogtreecommitdiff
path: root/nexus/src/main/kotlin/tech/libeufin/nexus/Log.kt
blob: fa74c79152981dfeb2676dcb97b4429a3ae952d9 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2024 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

package tech.libeufin.nexus

import tech.libeufin.common.*
import java.io.*
import java.nio.file.*
import java.time.*
import kotlin.io.*
import kotlin.io.path.*

/** 
 * Log EBICS files for debugging
 * 
 * Only log if [path] is not null
 */
class FileLogger(path: String?) {
    private val dir = if (path != null) Path(path) else null

    init {
        if (dir != null) {
            try {
                // Create logging directory if missing
                dir.createDirectories()
            } catch (e: Exception) {
                throw Exception("Failed to init EBICS debug logging directory", e)
            }
            logger.info("Logging to '$dir'")
        }
    }

    /**
     * Logs EBICS fetch content if EBICS debug logging is enabled
     *
     * @param stream EBICS fetch content
     * @param hac only true when downloading via HAC (EBICS 2)
     */
    fun logFetch(stream: InputStream, hac: Boolean = false): InputStream {
        if (dir == null) return stream
        val content = stream.readBytes()
        // Subdir based on current day.
        val now = Instant.now()
        val asUtcDate = LocalDate.ofInstant(now, ZoneId.of("UTC"))
        val nowMs = now.micros()
        // Creating the combined dir.
        val subDir = dir.resolve("${asUtcDate.year}-${asUtcDate.monthValue}-${asUtcDate.dayOfMonth}").resolve("fetch")
        subDir.createDirectories()
        if (hac) {
            subDir.resolve("${nowMs}_HAC_response.pain.002.xml").writeBytes(content)
        } else {
            // Write each ZIP entry in the combined dir.
            content.inputStream().unzipEach { fileName, xmlContent ->
                xmlContent.use {
                    Files.copy(it, subDir.resolve("${nowMs}_$fileName"))
                }
            }
        }
        return content.inputStream()
    }

    /**
     * Logs EBICS submit content if EBICS debug logging is enabled
     *
     * @param content EBICS submit content
     */
    fun logSubmit(content: ByteArray) {
        if (dir == null) return

        // Subdir based on current day.
        val now = Instant.now()
        val asUtcDate = LocalDate.ofInstant(now, ZoneId.of("UTC"))
        val nowMs = now.micros()
        // Creating the combined dir.
        val subDir = dir.resolve("${asUtcDate.year}-${asUtcDate.monthValue}-${asUtcDate.dayOfMonth}").resolve("submit")
        subDir.createDirectories()
        subDir.resolve("${nowMs}_pain.001.xml").writeBytes(content)
    }
}