Keep on seeing a buffer overflow type effect occur...
# kotlin-native
n
Keep on seeing a buffer overflow type effect occurring when writing messages to a log file. Some of the written messages have the timestamp included twice. What could cause a buffer overflow to occur?
Below is the function that writes a message to the log file:
Copy code
fun logMessage(tag: String, logLevel: LogLevel, content: String) = memScoped {
        val msg = "[${createTimestampString()}] ${logLevel.name} ${if (includeTag) "- $tag: " else ""}$content\n"
        val msgCstr = msg.cstr
        if (maxFileSizeExceeded()) clearFile()
        if (logFile != null) {
            fwrite(__s = logFile, __n = 1uL, __ptr = msgCstr, __size = msg.length.toULong())
        }
        Unit
    }
Some sample output from the log file:
Copy code
[21-09-2020 09:02:31] DEBUG - [21-09-2020 09:02:31] Publishing AMQP message...
[21-09-2020 09:02:31] DEBUG - [21-09-2020 09:02:31] AMQP message published.
[21-09-2020 09:03:01] DEBUG - Processing Modbus registers...
[21-09-2020 09:03:01] DEBUG - [21-09-2020 09:03:01] Publishing AMQP message...
[21-09-2020 09:03:01] DEBUG - [21-09-2020 09:03:01] AMQP message published.
Made two silly mistakes 🤦‍♂️. I logged two messages that include the timestamp when it wasn't necessary 🙃. The problems were hidden in plain sight. This is what stress/timelines can do especially when hunting for an elusive 🐛.