Skip to content

Commit

Permalink
refactored log details
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Sep 13, 2023
1 parent 222f655 commit dd9588d
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 57 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/de/gmuth/ipp/client/CupsPrinterType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}" }
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> constructor(val name: String, val tag: IppTag) : IppAttributeBuilder {
Expand Down Expand Up @@ -69,17 +72,17 @@ data class IppAttribute<T> 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)}" }
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/de/gmuth/ipp/core/IppCollection.kt
Original file line number Diff line number Diff line change
@@ -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

/**
Expand Down Expand Up @@ -30,10 +33,10 @@ data class IppCollection(val members: MutableCollection<IppAttribute<*>> = 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) }
}

}
27 changes: 0 additions & 27 deletions src/main/resources/ippclient-logging.properties

This file was deleted.

16 changes: 8 additions & 8 deletions src/test/kotlin/de/gmuth/ipp/client/IppPrinterTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class IppAttributesGroupTests {
}

@Test
fun logDetails() {
fun log() {
group.attribute("Commodore PET", Integer, 2001)
group.log(log, prefix = "|", title = "title")
}
Expand Down
10 changes: 6 additions & 4 deletions src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

}
2 changes: 1 addition & 1 deletion src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit dd9588d

Please sign in to comment.