slogging icon indicating copy to clipboard operation
slogging copied to clipboard

scala-native getTimestamp

Open Andrei-Pozolotin opened this issue 7 years ago • 2 comments

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?

Andrei-Pozolotin avatar Mar 22 '18 16:03 Andrei-Pozolotin

not really. this was just a quick hack that works on JVM

jokade avatar Mar 30 '18 10:03 jokade

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] "
      }

  }

}

Andrei-Pozolotin avatar Mar 30 '18 13:03 Andrei-Pozolotin