Skip to content

Commit 28b207b

Browse files
committed
Improve logging
1 parent 31372f1 commit 28b207b

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

src/main/kotlin/client/CarpoolCLIMain.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fun main(args: Array<String>) {
9898
}
9999

100100
private fun connectToSupervisor(args: ArgumentContainer, started: Boolean) {
101-
LOGGER.fine("Connecting to supervisor (reconnection?: $started)")
101+
LOGGER.fine { "Connecting to supervisor (reconnection?: $started)" }
102102

103103
val port = args.getRequired<Int>("port")
104104
val trySupervisor: Result<Supervisor> = runCatching {
@@ -114,7 +114,10 @@ private fun connectToSupervisor(args: ArgumentContainer, started: Boolean) {
114114

115115
trySupervisor.onSuccess {
116116
val supervisorPid = it.status()
117-
LOGGER.severe("You have asked to start the supervisor daemon, but it appears to already be running (PID $supervisorPid).")
117+
LOGGER.severe{
118+
"You have asked to start the supervisor daemon," +
119+
" but it appears to already be running (PID $supervisorPid)."
120+
}
118121
exitProcess(1)
119122
}
120123

@@ -129,7 +132,9 @@ private fun connectToSupervisor(args: ArgumentContainer, started: Boolean) {
129132
trySupervisor.onSuccess { supervisor ->
130133
handleCommands(args, supervisor)
131134
}.onFailure { e ->
132-
LOGGER.log(Level.SEVERE, e) { "The supervisor daemon does not appear to be running. Please start it with -start." }
135+
LOGGER.log(Level.SEVERE, e) {
136+
"The supervisor daemon does not appear to be running. Please start it with -start."
137+
}
133138
exitProcess(1)
134139
}
135140
}
@@ -151,26 +156,25 @@ private fun handleCommands(args: ArgumentContainer, supervisor: Supervisor) {
151156
}
152157
"list_applications" -> if (args.getRequired<Boolean>("list_applications")) {
153158
val applications = supervisor.listApplications()
154-
LOGGER.info("Currently ${applications.size} application(s)")
159+
LOGGER.info { "Currently ${applications.size} application(s)" }
155160
for (app in applications)
156-
LOGGER.info("${app.commandString}\n -PID: ${app.pid}\n -Alive?: ${app.isRunning}")
161+
LOGGER.info { "${app.commandString}\n -PID: ${app.pid}\n -Alive?: ${app.isRunning}" }
157162
}
158163
"add_application" -> for (app in args.getsRequired<String>("add_application")) {
159164
if (app.isEmpty()) continue
160165

161166
val commandString = app.split(" ").toTypedArray()
162167
val appPid = supervisor.addApplication(commandString)
163-
LOGGER.info("Started application [$app] -- PID $appPid")
168+
LOGGER.info { "Started application [$app] -- PID $appPid" }
164169
}
165170
"remove_application" -> for (appPid in args.getsRequired<Long>("remove_application")) {
166171
if (appPid == -1L) continue
167172

168173
try {
169174
supervisor.removeApplication(appPid)
170-
LOGGER.info("Removed application with PID $appPid")
175+
LOGGER.info { "Removed application with PID $appPid" }
171176
} catch (anfe: ApplicationNotFoundException) {
172-
LOGGER.warning("There is no application with PID $appPid")
173-
LOGGER.log(Level.FINE, anfe) { "Exception info:" }
177+
LOGGER.log(Level.WARNING, anfe) { "There is no application with PID $appPid" }
174178
}
175179
}
176180
}
@@ -190,5 +194,5 @@ private fun spawnSupervisor(logLevel: Level, port: Int, logDir: Path) {
190194
logDir.absolutePathString()
191195
))
192196

193-
LOGGER.info("Supervisor daemon started - PID ${supervisor.pid()}.")
197+
LOGGER.info { "Supervisor daemon started - PID ${supervisor.pid()}." }
194198
}

src/main/kotlin/supervisor/CarpoolSupervisor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CarpoolSupervisor(val pid: Long, val logger: Logger) : UnicastRemoteObject
2727

2828
override fun addApplication(commandArray: Array<String>): Long {
2929
val commandString = commandArray.joinToString(" ")
30-
logger.info("Starting application $commandString")
30+
logger.info { "Starting application $commandString" }
3131

3232
val app = Runtime.getRuntime().exec(commandArray)
3333
applications[app.pid()] = ApplicationEntry(app, commandString)
@@ -36,14 +36,14 @@ class CarpoolSupervisor(val pid: Long, val logger: Logger) : UnicastRemoteObject
3636

3737
override fun removeApplication(pid: Long) {
3838
if (!applications.containsKey(pid)) {
39-
logger.warning("Could not find application with PID $pid")
39+
logger.warning { "Could not find application with PID $pid" }
4040
throw ApplicationNotFoundException(pid)
4141
}
4242

4343
val app = applications[pid]!!
4444
app.handle.destroy()
4545
applications.remove(pid)
46-
logger.info("Removed application with PID $pid (${app.commandString})")
46+
logger.info { "Removed application with PID $pid (${app.commandString})" }
4747
}
4848

4949
data class ApplicationEntry(val handle: Process, val commandString: String)

src/main/kotlin/supervisor/CarpoolSupervisorMain.kt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
package org.bread_experts_group.application_carpool.supervisor
22

3-
import java.io.PrintStream
43
import java.lang.Integer.parseInt
5-
import java.nio.file.StandardOpenOption
64
import java.rmi.registry.LocateRegistry
5+
import java.util.logging.FileHandler
76
import java.util.logging.Level
87
import java.util.logging.Logger
98
import kotlin.io.path.Path
10-
import kotlin.io.path.createFile
11-
import kotlin.io.path.notExists
12-
import kotlin.io.path.outputStream
139
import kotlin.system.exitProcess
1410

1511
private val LOGGER = Logger.getLogger("Application Carpool Supervisor")
1612

1713
fun main(args: Array<String>) {
1814
val pid = ProcessHandle.current().pid()
1915
val port = parseInt(args[1])
20-
val logDir = Path(args[2])
21-
22-
val logFile = logDir.resolve("supervisor-log.txt")
23-
if (logFile.notExists())
24-
logFile.createFile()
25-
26-
System.setOut(PrintStream(logFile
27-
.outputStream(StandardOpenOption.APPEND, StandardOpenOption.WRITE)))
2816

17+
LOGGER.useParentHandlers = false
18+
LOGGER.addHandler(FileHandler(Path(args[2]).resolve("supervisor-log.txt").toString()))
2919
LOGGER.level = Level.parse(args[0])
30-
LOGGER.info("Starting supervisor daemon -- PID $pid")
20+
LOGGER.info { "Starting supervisor daemon -- PID $pid" }
3121

3222
var registry = LocateRegistry.getRegistry(port)
3323
val supervisorStub = CarpoolSupervisor(pid, LOGGER)
@@ -37,12 +27,17 @@ fun main(args: Array<String>) {
3727
LOGGER.fine("Connected to the local RMI registry")
3828
} catch (e: Exception) {
3929
try {
40-
LOGGER.log(Level.FINE, e) { "Could not connect to the local RMI registry on port $port, attempting to create our own" }
30+
LOGGER.log(Level.FINE, e) {
31+
"Could not connect to the local RMI registry on port $port, attempting to create our own"
32+
}
4133
registry = LocateRegistry.createRegistry(port)
4234
registry.bind("CarpoolSupervisor", supervisorStub)
43-
LOGGER.fine("Connected to the self-made RMI registry on port $port")
35+
LOGGER.fine { "Connected to the self-made RMI registry on port $port" }
4436
} catch (e: Exception) {
45-
LOGGER.log(Level.SEVERE, e) { "Could not connect to an RMI registry. If you have your own local registry, please ensure you run it with -J-Djava.rmi.server.codebase=file:<YOUR CLASSPATH>/" }
37+
LOGGER.log(Level.SEVERE, e) {
38+
"Could not connect to an RMI registry. If you have your own local registry, " +
39+
"please ensure you run it with -J-Djava.rmi.server.codebase=file:<YOUR CLASSPATH>/"
40+
}
4641
exitProcess(1)
4742
}
4843
}

0 commit comments

Comments
 (0)