slogging
slogging copied to clipboard
scala-native getTimestamp
https://github.com/jokade/slogging/blob/master/shared/src/main/scala/slogging/loggerFactory.scala#L169
the following
private def getTimestamp = new Date().toString
produces output
Date(1521735026318)
was this the intent?
not really. this was just a quick hack that works on JVM
the following works in scala-native 0.3.7
# output from timeStamp():
2018-03-30 08:02:29
import slogging._
import slogging.MessageFormatter._
import scala.scalanative.native._
import scala.scalanative.posix.time._
object Logging {
class Formatter(
printLevel : Boolean = true,
printName : Boolean = true,
printStamp : Boolean = true,
stampFormat : CString = c"%Y-%m-%d %H:%M:%S",
stampSize : CInt = 64
) extends PrefixFormatter {
def timeStamp() = {
val secondsPtr = stackalloc[ time_t ]
!secondsPtr = System.currentTimeMillis / 1000
val calendarPtr = stackalloc[ tm ]
localtime_r( secondsPtr, calendarPtr )
val buffer = stackalloc[ CChar ]( stampSize )
strftime( buffer, stampSize, stampFormat, calendarPtr )
fromCString( buffer )
}
override def formatPrefix( level : MessageLevel, name : String ) : String =
if ( printStamp ) {
val stamp = timeStamp()
if ( printName ) s"$stamp [$level] $name " else s"$stamp [$level] "
} else {
if ( printName ) s"[$level] $name " else s"[$level] "
}
}
}