-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First version of logging in USVM (#38)
* First version of logging for usvm * Implementation of the JacoDB interpreter (#18) Co-authored-by: Alexey Menshutin <[email protected]> * New path selectors infrastructure (#29) * Add general framework for weighted searchers * Add tests for AA-tree and discrete PDF * Little typo fix in tests * Add shortest distance to targets weighter * Add random tree path selector * Add fork depth path selector * Fix interleaved path selector * Add comments and some new java tests, infrastructure fixes * PR comments fixes, add solver type and timeout options * First version of logging for usvm * synchronize with master --------- Co-authored-by: Sergey Pospelov <[email protected]> Co-authored-by: Alexey Menshutin <[email protected]> Co-authored-by: Maksim Parshin <[email protected]>
- Loading branch information
1 parent
34e6503
commit c09f086
Showing
15 changed files
with
191 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<!-- Refer to this to choose your pattern: https://logback.qos.ch/manual/layouts.html --> | ||
<pattern>%d{HH:mm:ss.SSS} |%.-1level| %replace(%c{0}){'(\$Companion)?\$logger\$1',''} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="debug"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<!-- Refer to this to choose your pattern: https://logback.qos.ch/manual/layouts.html --> | ||
<pattern>%d{HH:mm:ss.SSS} |%.-1level| %replace(%c{0}){'(\$Companion)?\$logger\$1',''} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="debug"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.usvm.util | ||
|
||
import mu.KLogger | ||
|
||
class LoggerWithLogMethod(val logger: KLogger, val logMethod: (() -> Any?) -> Unit) | ||
|
||
fun KLogger.info(): LoggerWithLogMethod = LoggerWithLogMethod(this, this::info) | ||
fun KLogger.debug(): LoggerWithLogMethod = LoggerWithLogMethod(this, this::debug) | ||
fun KLogger.trace(): LoggerWithLogMethod = LoggerWithLogMethod(this, this::trace) | ||
|
||
fun elapsedSecFrom(startNano: Long): String { | ||
val elapsedNano = System.nanoTime() - startNano | ||
val elapsedS = elapsedNano.toDouble() / 1_000_000_000 | ||
return String.format("%.3f", elapsedS) + " sec" | ||
} | ||
|
||
/** | ||
* Structured logging. | ||
* | ||
* Usage: `logger.info().bracket("message") { ... block_of_code ...}` | ||
* | ||
* Results in | ||
* | ||
* ``` | ||
* Started: message | ||
* -- other messages inside block of code | ||
* Finished (in xxx.xxx sec): message | ||
* ``` | ||
* | ||
* Method can handle exceptions and nonlocal returns from inline lambda | ||
* You can use [closingComment] to add some result-depending comment to "Finished:" message. Special "<Nothing>" comment | ||
* is added if nonlocal return happened in [block] | ||
*/ | ||
inline fun <T> LoggerWithLogMethod.bracket( | ||
msg: String, | ||
crossinline closingComment: (Result<T>) -> Any? = { "" }, | ||
block: () -> T | ||
): T { | ||
logMethod { "Started: $msg" } | ||
val startNano = System.nanoTime() | ||
var alreadyLogged = false | ||
|
||
var res : Maybe<T> = Maybe.empty() | ||
try { | ||
// Note: don't replace this one with runCatching, otherwise return from lambda breaks "finished" logging. | ||
res = Maybe(block()) | ||
return res.getOrThrow() | ||
} catch (t: Throwable) { | ||
logMethod { "Finished (in ${elapsedSecFrom(startNano)}): $msg :: EXCEPTION :: ${closingComment(Result.failure(t))}" } | ||
alreadyLogged = true | ||
throw t | ||
} finally { | ||
if (!alreadyLogged) { | ||
if (res.hasValue) | ||
logMethod { "Finished (in ${elapsedSecFrom(startNano)}): $msg ${closingComment(Result.success(res.getOrThrow()))}" } | ||
else | ||
logMethod { "Finished (in ${elapsedSecFrom(startNano)}): $msg <Nothing>" } | ||
} | ||
} | ||
} | ||
|
||
inline fun <T> KLogger.catchException(block: () -> T): T? { | ||
return try { | ||
block() | ||
} catch (e: Throwable) { | ||
this.error(e) { "Isolated" } | ||
null | ||
} | ||
} | ||
|
||
inline fun <T> KLogger.logException(block: () -> T): T { | ||
return try { | ||
block() | ||
} catch (e: Throwable) { | ||
this.error("Exception occurred", e) | ||
throw e | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.usvm.util | ||
|
||
/** | ||
* Analogue of java's [java.util.Optional] | ||
*/ | ||
class Maybe<out T> private constructor(val hasValue: Boolean, val value:T? ) { | ||
constructor(v: T) : this(true, v) | ||
companion object { | ||
fun empty() = Maybe(false, null) | ||
} | ||
|
||
/** | ||
* Returns [value] if [hasValue]. Otherwise, throws exception | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
fun getOrThrow() : T = if (!hasValue) { | ||
error("Maybe hasn't value") | ||
} else { | ||
value as T | ||
} | ||
|
||
override fun toString(): String = if (hasValue) "Maybe($value)" else "<empty>" | ||
} |