com.mchange.sysadmin.core.scala Maven / Gradle / Ivy
package com.mchange.sysadmin
import scala.collection.*
import com.mchange.codegenutil.*
import java.time.{Instant,ZoneId}
import java.time.temporal.ChronoUnit
import java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME
import scala.util.control.NonFatal
class SysadminException( message : String, cause : Throwable = null ) extends Exception(message, cause)
class UnexpectedPriorState( message : String, cause : Throwable = null ) extends Exception(message, cause)
def abortUnexpectedPriorState( message : String ) : Nothing = throw new UnexpectedPriorState(message)
def extractFullStackTrace(t:Throwable) : String =
val sw = new java.io.StringWriter()
t.printStackTrace(new java.io.PrintWriter(sw))
sw.toString()
extension (t : Throwable)
def fullStackTrace : String = extractFullStackTrace(t)
lazy val hostname : Option[String] =
try
Some(os.proc("hostname").call( check=false, stdout = os.Pipe, stderr = os.Pipe ).out.trim())
catch
case NonFatal(t) => None
def timestamp =
val now = Instant.now.truncatedTo( ChronoUnit.SECONDS ).atZone(ZoneId.systemDefault())
ISO_OFFSET_DATE_TIME.format(now)
val UnitKiB = 1024L
val UnitMiB = 1024L * 1024L
val UnitGiB = 1024L * 1024L * 1024L
val UnitTiB = 1024L * 1024L * 1024L * 1024L
def formatSize(num : Double, unit : String) = f"$num%1.3f $unit%s"
def friendlyFileSize( bytes : Long ) : String =
if (bytes < UnitKiB) then
s"${bytes} bytes"
else if (bytes < UnitMiB) then
formatSize(bytes.toDouble / UnitKiB, "KiB")
else if (bytes < UnitGiB) then
formatSize(bytes.toDouble / UnitMiB, "MiB")
else
formatSize(bytes.toDouble / UnitGiB, "TiB")
def yn( boolean : Boolean ) = if (boolean) then "Yes" else "No"
/*
def colorClass( boolean : Boolean ) = if boolean then "success" else "failure"
def mbLabeledText( mlt : Option[Tuple2[String,String]]) : String =
mlt match
case Some( tup ) => labeledText( tup ).text
case None => ""
def labeledTextOrNA( label : String, mbText : String ) : String =
if mbText.nonEmpty then
labeledText( Tuple2(label,mbText) ).text
else
s"""${label}: N/A"""
*/ © 2015 - 2025 Weber Informatics LLC | Privacy Policy