diff --git a/README.md b/README.md index 2ce15e43..d2813e7b 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Repository [ipp-samples](https://github.com/gmuth/ipp-samples) contains examples ```kotlin // initialize printer connection and show printer attributes val ippPrinter = IppPrinter(URI.create("ipp://colorjet.local/ipp/printer")) -ippPrinter.attributes.logDetails() +ippPrinter.attributes.log(logger) // marker levels ippPrinter.markers.forEach { println(it) } @@ -47,7 +47,6 @@ val job = ippPrinter.printJob( mediaColSource("tray-1"), notifyEvents = listOf("job-state-changed", "job-stopped", "job-completed") // CUPS ) -job.logDetails() job.subscription?.processEvents { println(it) } // print remote file, make printer pull document from remote server diff --git a/src/main/kotlin/de/gmuth/ipp/client/CupsPrinterType.kt b/src/main/kotlin/de/gmuth/ipp/client/CupsPrinterType.kt index 4ea4a2e5..b8f27dcd 100644 --- a/src/main/kotlin/de/gmuth/ipp/client/CupsPrinterType.kt +++ b/src/main/kotlin/de/gmuth/ipp/client/CupsPrinterType.kt @@ -4,6 +4,9 @@ package de.gmuth.ipp.client * Copyright (c) 2020-2023 Gerhard Muth */ +import java.util.logging.Level +import java.util.logging.Level.INFO +import java.util.logging.Logger import java.util.logging.Logger.getLogger // https://www.cups.org/doc/spec-ipp.html @@ -51,10 +54,10 @@ class CupsPrinterType(val value: Int) { override fun toString() = "$value (${toSet().joinToString(",")})" - fun logDetails() { - log.info { "PRINTER-TYPE 0x%08X capabilities:".format(value) } + fun log(logger: Logger, level: Level = INFO) = logger.run { + log(level) { "PRINTER-TYPE 0x%08X capabilities:".format(value) } for (capability in toSet()) { - log.info { "* ${capability.description}" } + log(level) { "* ${capability.description}" } } } diff --git a/src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt b/src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt index f2bf6c54..9a21c9c5 100644 --- a/src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt +++ b/src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt @@ -8,6 +8,9 @@ import de.gmuth.ipp.core.IppTag.* import de.gmuth.ipp.iana.IppRegistrationsSection2.attributeIs1setOf import de.gmuth.ipp.iana.IppRegistrationsSection6.getEnumName import java.nio.charset.Charset +import java.util.logging.Level +import java.util.logging.Level.INFO +import java.util.logging.Logger import java.util.logging.Logger.getLogger data class IppAttribute constructor(val name: String, val tag: IppTag) : IppAttributeBuilder { @@ -69,17 +72,17 @@ data class IppAttribute constructor(val name: String, val tag: IppTag) : IppA fun enumNameOrValue(value: Any) = if (tag == IppTag.Enum) getEnumName(name, value) else value - fun logDetails(prefix: String = "") { + fun log(logger: Logger, level: Level = INFO, prefix: String = "") = logger.run { val string = toString() if (string.length < 160) { - log.info { "$prefix$string" } + log(level) { "$prefix$string" } } else { - log.info { "$prefix$name ($tag) =" } + log(level) { "$prefix$name ($tag) =" } for (value in values) { if (value is IppCollection) { - value.logDetails("$prefix ") + (value as IppCollection).log(logger, level, "$prefix ") } else { - log.info { "$prefix ${enumNameOrValue(value as Any)}" } + log(level) { "$prefix ${enumNameOrValue(value as Any)}" } } } } diff --git a/src/main/kotlin/de/gmuth/ipp/core/IppCollection.kt b/src/main/kotlin/de/gmuth/ipp/core/IppCollection.kt index b663a864..b6b1f191 100644 --- a/src/main/kotlin/de/gmuth/ipp/core/IppCollection.kt +++ b/src/main/kotlin/de/gmuth/ipp/core/IppCollection.kt @@ -1,5 +1,8 @@ package de.gmuth.ipp.core +import java.util.logging.Level +import java.util.logging.Level.INFO +import java.util.logging.Logger import java.util.logging.Logger.getLogger /** @@ -30,10 +33,10 @@ data class IppCollection(val members: MutableCollection> = mutab "${it.name}=${it.values.joinToString(",")}" } - fun logDetails(prefix: String = "") { + fun log(logger: Logger, level: Level = INFO, prefix: String = "") { val string = toString() - if (string.length < 160) log.info { "$prefix$string" } - else members.forEach { member -> member.logDetails(prefix) } + if (string.length < 160) logger.log(level) { "$prefix$string" } + else members.forEach { member -> member.log(logger, level, prefix) } } } \ No newline at end of file diff --git a/src/main/resources/ippclient-logging.properties b/src/main/resources/ippclient-logging.properties deleted file mode 100644 index aab57712..00000000 --- a/src/main/resources/ippclient-logging.properties +++ /dev/null @@ -1,27 +0,0 @@ -# activate: JulAdapter.configure("/ipp-client-logging.conf") -# https://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html -# https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax -# %1 format - the java.util.Formatter format string specified in the java.util.logging.SimpleFormatter.format property or the default format. -# %2 date - a Date object representing event time of the log record. -# %3 source - a string representing the caller, if available; otherwise, the logger's name. -# %3 logger - the logger's name. -# %4 level - the log level. -# %5 message - the formatted log message -# %6 thrown - -handlers=java.util.logging.ConsoleHandler -java.util.logging.ConsoleHandler.level=ALL -java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter -java.util.logging.SimpleFormatter.format=%1$tT.%1$tL %3$-50s%4$-15s%5$s%6$s%n - -sun.net.www.protocol.http.level=FINE - -# verbose -#de.gmuth.ipp.client.IppClient.level=FINE -#de.gmuth.ipp.core.IppOutputStream.level=FINE -#de.gmuth.ipp.core.IppInputStream.level=FINE -#sun.net.www.protocol.level=FINEST - -# very verbose -#.level=ALL -#de.gmuth.level=ALL diff --git a/src/test/kotlin/de/gmuth/ipp/client/IppPrinterTests.kt b/src/test/kotlin/de/gmuth/ipp/client/IppPrinterTests.kt index afe75bc1..a435cad0 100644 --- a/src/test/kotlin/de/gmuth/ipp/client/IppPrinterTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/client/IppPrinterTests.kt @@ -33,7 +33,7 @@ import kotlin.test.assertTrue class IppPrinterTests { - val log = getLogger(javaClass.name) + val tlog = getLogger(javaClass.name) val blankPdf = File("tool/A4-blank.pdf") val httpClient = HttpClientMock() val ippConfig = IppConfig() @@ -50,8 +50,8 @@ class IppPrinterTests { @Test fun printerAttributes() { - printer.apply { - log.info { toString() } + printer.run { + tlog.info { toString() } assertTrue(isAcceptingJobs) assertTrue(documentFormatSupported.contains("application/pdf")) assertTrue(supportsOperations(GetPrinterAttributes)) @@ -81,13 +81,13 @@ class IppPrinterTests { assertFalse(isMediaNeeded()) assertFalse(isCups()) printerType.apply { - log.info { toString() } - logDetails() + tlog.info { toString() } + log(tlog) } communicationChannelsSupported.forEach { - log.info { "${it.uri}, ${it.security}, ${it.authentication}, $it" } + tlog.info { "${it.uri}, ${it.security}, ${it.authentication}, $it" } } - ippConfig.log(log) + ippConfig.log(tlog) } } @@ -102,7 +102,7 @@ class IppPrinterTests { httpClient.mockResponse("Simulated_Laser_Printer/Get-Printer-Attributes.ipp") printer.apply { updateAttributes() - log(log) + log(tlog) assertEquals(122, attributes.size) } } diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt index 3a128d28..d8cae28a 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt @@ -12,7 +12,7 @@ import kotlin.test.* class IppAttributeTests { private val ippAttribute = IppAttribute("printer-state-reasons", Keyword, "none") - val log = getLogger(javaClass.name) + val tlog = getLogger(javaClass.name) @Test fun constructorFailsDueToDelimiterTag() { @@ -103,9 +103,9 @@ class IppAttributeTests { } @Test - fun logDetails() { + fun log() { // cover an output with more than 160 characters and a collection value - IppAttribute("media-col".padEnd(160, '-'), BegCollection, IppCollection()).logDetails() + IppAttribute("media-col".padEnd(160, '-'), BegCollection, IppCollection()).log(tlog) } @Test diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt index 1d520adc..ca74eda1 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt @@ -103,7 +103,7 @@ class IppAttributesGroupTests { } @Test - fun logDetails() { + fun log() { group.attribute("Commodore PET", Integer, 2001) group.log(log, prefix = "|", title = "title") } diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt index 75e55e4e..8887410e 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt @@ -5,12 +5,14 @@ package de.gmuth.ipp.core */ import java.util.NoSuchElementException +import java.util.logging.Logger.getLogger import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith class IppCollectionTests { + val tlog = getLogger(javaClass.name) private val collection = IppCollection(IppAttribute("foo", IppTag.Keyword, "a", "b")) @Test @@ -38,14 +40,14 @@ class IppCollectionTests { } @Test - fun logDetailsNarrow() { - collection.logDetails() + fun logNarrow() { + collection.log(tlog) } @Test - fun logDetailsWide() { + fun logWide() { collection.addAll(listOf(IppAttribute("bar", IppTag.Keyword, "c".repeat(160)))) - collection.logDetails() + collection.log(tlog) } } \ No newline at end of file diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt index 8148e126..d9e7077f 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt @@ -57,7 +57,7 @@ class IppMessageTests { assertEquals(38, rawBytes!!.size) write(ByteArrayOutputStream()) // cover warning toString() // cover toString - log(log) // cover logDetails + log(log) // cover log } }