-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
airspec (feature): Scala Native 0.5 support (Scala 3 only) (#3494)
- Loading branch information
Showing
21 changed files
with
255 additions
and
62 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
20 changes: 20 additions & 0 deletions
20
airframe-log/.native/src/main/scala-3/java/util/logging/Level.scala
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,20 @@ | ||
package java.util.logging | ||
|
||
|
||
case class Level(name: String, value: Int) extends Ordered[Level] { | ||
override def compare(other: Level): Int = value.compare(other.value) | ||
def intValue(): Int = value | ||
override def toString: String = name | ||
} | ||
|
||
object Level: | ||
val OFF = Level("OFF", 0) | ||
val SEVERE = Level("SEVERE", 1000) | ||
val WARNING = Level("WARNING", 900) | ||
val INFO = Level("INFO", 800) | ||
val CONFIG = Level("CONFIG", 700) | ||
val FINE = Level("FINE", 500) | ||
val FINER = Level("FINER", 400) | ||
val FINEST = Level("FINEST", 300) | ||
val ALL = Level("ALL", Integer.MIN_VALUE) | ||
|
120 changes: 120 additions & 0 deletions
120
airframe-log/.native/src/main/scala-3/java/util/logging/Logger.scala
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,120 @@ | ||
package java.util.logging | ||
|
||
|
||
abstract class Handler extends AutoCloseable: | ||
def publish(record: LogRecord): Unit | ||
def flush(): Unit | ||
|
||
/** | ||
* Implements java.util.logging.Logger interface, which is not avaialble | ||
* in Scala Native | ||
* @param name | ||
*/ | ||
class Logger(parent: Option[Logger], name: String) { | ||
private var handlers = List.empty[Handler] | ||
private var useParentHandlers = true | ||
private var level: Option[Level] = None | ||
|
||
def getName(): String = name | ||
|
||
def log(level: Level, msg: String): Unit = { | ||
log(LogRecord(level, msg)) | ||
} | ||
|
||
def log(record: LogRecord): Unit = { | ||
if(isLoggable(record.getLevel())) { | ||
if(record.getLoggerName() == null) { | ||
record.setLoggerName(name) | ||
} | ||
if(parent.nonEmpty && useParentHandlers) then | ||
getParent().log(record) | ||
else | ||
handlers.foreach { h => h.publish(record) } | ||
} | ||
} | ||
|
||
def isLoggable(level: Level): Boolean = { | ||
val l = getLevel() | ||
if(level.intValue() < l.intValue()) then false else true | ||
} | ||
|
||
def getParent(): Logger = { | ||
parent.getOrElse(null) | ||
} | ||
|
||
def getLevel(): Level = { | ||
level.orElse(parent.map(_.getLevel())).getOrElse(Level.INFO) | ||
} | ||
|
||
def setLevel(newLevel: Level): Unit = { | ||
level = Some(newLevel) | ||
} | ||
|
||
def resetLogLevel(): Unit = { | ||
level = None | ||
} | ||
|
||
def setUseParentHandlers(useParentHandlers: Boolean): Unit = { | ||
this.useParentHandlers = useParentHandlers | ||
} | ||
|
||
def addHandler(h: Handler): Unit = { | ||
handlers = h :: handlers | ||
} | ||
|
||
def removeHandler(h: Handler): Unit = { | ||
handlers = handlers.filter(_ != h) | ||
} | ||
|
||
def getHandlers: Array[Handler] = handlers.toArray | ||
} | ||
|
||
object Logger: | ||
import scala.jdk.CollectionConverters.* | ||
private val loggerTable = new java.util.concurrent.ConcurrentHashMap[String, Logger]().asScala | ||
private val rootLogger = Logger(None, "") | ||
|
||
def getLogger(name: String): Logger = { | ||
loggerTable.get(name) match { | ||
case Some(logger) => logger | ||
case None => | ||
val logger = newLogger(name) | ||
synchronized { | ||
loggerTable.put(name, logger) | ||
} | ||
logger | ||
} | ||
} | ||
|
||
private def newLogger(name: String): Logger = { | ||
name match { | ||
case null | "" => rootLogger | ||
case other => | ||
val parentName = name.substring(0, name.lastIndexOf('.').max(0)) | ||
val parentLogger = getLogger(parentName) | ||
Logger(Some(parentLogger), name) | ||
} | ||
} | ||
|
||
|
||
abstract class Formatter: | ||
def format(record: LogRecord): String | ||
|
||
|
||
class LogRecord(_level: Level, msg: String) extends Serializable: | ||
private val millis = System.currentTimeMillis() | ||
private var loggerName = "" | ||
private var thrown: Throwable = null | ||
|
||
def getMessage(): String = msg | ||
def getMillis(): Long = millis | ||
def getLoggerName(): String = loggerName | ||
def getLevel(): Level = _level | ||
def getThrown(): Throwable = thrown | ||
|
||
def setLoggerName(name: String): Unit = { | ||
this.loggerName = name | ||
} | ||
def setThrown(e: Throwable): Unit = { | ||
thrown = e | ||
} |
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
airframe-log/.native/src/main/scala-3/wvlet/log/LogTimestampFormatter.scala
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,43 @@ | ||
package wvlet.log | ||
|
||
import scalanative.posix.time.* | ||
import scalanative.unsafe.* | ||
import scalanative.unsigned.* | ||
import scalanative.libc.stdio.* | ||
import scalanative.libc.string.* | ||
|
||
/** | ||
* Use strftime to format timestamps in Scala Native | ||
*/ | ||
object LogTimestampFormatter { | ||
|
||
private def format(pattern: CString, timeMillis: Long): String = { | ||
Zone { | ||
val ttPtr = alloc[time_t]() | ||
!ttPtr = (timeMillis / 1000).toSize | ||
val tmPtr = alloc[tm]() | ||
localtime_r(ttPtr, tmPtr) | ||
val bufSize = 26.toUSize | ||
val buf: Ptr[Byte] = alloc[Byte](bufSize) | ||
strftime(buf, bufSize, pattern, tmPtr) | ||
val ms = timeMillis % 1000 | ||
|
||
val msBuf: Ptr[Byte] = alloc[Byte](3) | ||
sprintf(msBuf, c"%03d", ms) | ||
strcat(buf, msBuf) | ||
|
||
val tzBuf: Ptr[Byte] = alloc[Byte](5) | ||
strftime(tzBuf, 5.toUSize, c"%z", tmPtr) | ||
strcat(buf, tzBuf) | ||
fromCString(buf) | ||
} | ||
} | ||
|
||
def formatTimestamp(timeMillis: Long): String = { | ||
format(c"%Y-%m-%d %H:%M:%S.", timeMillis) | ||
} | ||
|
||
def formatTimestampWithNoSpaace(timeMillis: Long): String = { | ||
format(c"%Y-%m-%dT%H:%M:%S.", timeMillis) | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
airframe-log/.native/src/main/scala/wvlet/log/LogTimestampFormatter.scala
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.