Commit 1354bc97 by Amelin Konstantin

Add log to file, defined in application.conf

parent 10bfeca9
......@@ -10,17 +10,16 @@
Импортировать проект. <https://www.jetbrains.com/help/idea/sbt-support.html#import_sbt>
## Запуск проекта
## Запуск проекта dev mode
1) Создать в корне проекта папку **output**.
2) Запуск из терминала
```
sbt run
sbt "run input output"
или
sbt ~run
sbt "~run input output"
```
3) Запуск из IDE. <https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html#RunningDebugging>
4) Запуск тестов из терминала
......@@ -29,4 +28,7 @@ sbt test
или
sbt ~test
```
## Запуск проекта production mode
1) Для отключения детального логгирования установить **debug = false** в src/main/resources/application.conf
# Application settings
# Set debug mode
debug = true
\ No newline at end of file
debug = true
# Log file name
log = "log.txt"
\ No newline at end of file
import com.typesafe.config.{Config, ConfigFactory}
import scala.xml._
import java.nio.file.{Files, Path, Paths}
import java.nio.file.{Files, Path, Paths, StandardOpenOption}
import java.time.LocalDateTime
import scala.util.{Failure, Success, Try}
import xmlvalid._
import templates._
......@@ -8,14 +11,15 @@ import templates._
object Main {
val conf: Config = ConfigFactory.load
val debugMode: Boolean = conf.getBoolean("debug")
val logFileName: String = conf.getString("log")
implicit def templateFactory(source: Node): Template with Logger = {
implicit def templateFactory(source: Node): Template = {
(source \ "trade").head.child
.filter {
case v: Elem => true
case _ => false
}(1).label match {
case "repo" => new RepoTemplate(source) with Logger
case "repo" => if (debugMode) new RepoTemplate(source) with Logger else new RepoTemplate(source)
case v => throw new Exception("Undefined trade type")
}
}
......@@ -26,35 +30,41 @@ object Main {
else if (!Files.exists(Paths.get(args(1)))) println("Error: OUTPUT directory doesn`t exist")
else {
val p = new PrettyPrinter(140, 2)
val nsdXmlValid = XMLValid("src/main/resources/nsd/nsd-ext-merged-schema.xsd")
val rtsXmlValid = XMLValid("src/main/resources/rts/fpml-recordkeeping-merged-schema.xsd")
var logString = ""
for (v <- Files.walk(Paths.get(args(0))).toArray; f = v.asInstanceOf[Path]; if !Files.isDirectory(f)) {
val fSourceName= f.getFileName.toString
val fOutputName = s"Converted_$fSourceName"
val sourceXml = XML.loadFile(f.toString)
val template = Template(sourceXml)
val outputXml = XML.loadString(p.format(template.render))
val now = LocalDateTime.now().toString
for (v <- Files.walk(Paths.get(args(0))).toArray) {
val f = v.asInstanceOf[Path]
if (!Files.isDirectory(f)) {
val fName= f.getFileName.toString.split('.')
val sourceXml = XML.loadFile(f.toString)
val template = Template(sourceXml)
val outputXml = XML.loadString(p.format(template.render))
//val outputXml = template.template
//template.log()
Try {
XMLValid("src/main/resources/nsd/nsd-ext-merged-schema.xsd").loadString(sourceXml.mkString)
} match {
case Success(_) => println("Source xml success check against NSD schema")
case Failure(ex) => println(s"Source xml isn`t wellformed or invalid: ${ex.getMessage}")
}
Try {
XMLValid("src/main/resources/rts/fpml-recordkeeping-merged-schema.xsd").loadString(outputXml.mkString)
} match {
case Success(_) => println("Output xml success check against RTS schema")
case Failure(ex) => println(s"Output xml isn`t wellformed or invalid: ${ex.getMessage}")
}
XML.save(Paths.get(args(1), fName(0) + "_RTS." + fName(1)).toString, outputXml, xmlDecl = true)
val sourceXmlValid = if (template.isStraight) nsdXmlValid else rtsXmlValid
val outputXmlValid = if (template.isStraight) rtsXmlValid else nsdXmlValid
val msgCheckSource = Try {
sourceXmlValid.loadString(sourceXml.mkString)
} match {
case Success(_) => s"'$fSourceName' successfully checked against schema"
case Failure(ex) => s"'$fSourceName' isn`t wellformed or invalid: ${ex.getMessage}"
}
val msgCheckOutput = Try {
outputXmlValid.loadString(outputXml.mkString)
} match {
case Success(_) => s"'$fOutputName' successfully checked against schema"
case Failure(ex) => s"'$fOutputName' isn`t wellformed or invalid: ${ex.getMessage}"
}
XML.save(Paths.get(args(1), fOutputName).toString, outputXml, xmlDecl = true)
logString += template.info(s"$now\n$msgCheckSource\n$msgCheckOutput") + "\n\n"
}
Files.write(Paths.get(args(1), logFileName), logString.getBytes("utf-8"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)
}
}
}
......@@ -21,9 +21,9 @@ trait Logger {
m.get(idx) match {
case None =>
m(idx) = mutable.Queue((s"tag: ${i.label}", text, attr))
m(idx) = mutable.Queue((i.label, text, attr))
case _ =>
m(idx).enqueue((s"tag: ${i.label}", text, attr))
m(idx).enqueue((i.label, text, attr))
}
if (i.attributes.nonEmpty) {
......@@ -61,5 +61,25 @@ trait Logger {
case _ => Text(msgNotFound)
}
def log(): Unit = data.foreach(e => println(e._2))
override def info(msg: String): String = {
val additionalInfo = data.filter {
case (k, v) if v.nonEmpty => true
case _ => false
}
.toVector
.flatMap { case (k, v ) => v }
.filter {
case (t, v, m) if v != null || (m != null && m.nonEmpty) => true
case _ => false
}
.map {
case (t, v, m) => s"tag: $t, value: ${if (v != null) v else ""}" +
(if (m != null && m.nonEmpty) ", " + m.toVector.map { case (k, v1) => v1}.map {
case (k1, v2) => s"attr: $k1, value: $v2"
}.mkString(", ") else "")
}
.mkString("\n")
s"$msg\nNot used:\n$additionalInfo"
}
}
......@@ -83,4 +83,6 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) {
</fpml:bond>
</fpmlext:repo>
}
def isStraight = true
}
\ No newline at end of file
......@@ -19,6 +19,8 @@ abstract class Template(protected[this] val source: Node) {
def template: Node
def isStraight: Boolean
def render: Node = {
def attrIsEmpty(n: Node): Boolean = n match {
case v: Elem if v.child.isEmpty => v.attributes.isEmpty
......@@ -48,10 +50,12 @@ abstract class Template(protected[this] val source: Node) {
rt2.transform(rt1.transform(template)).head
}
def info(msg: String): String = msg
}
object Template {
def apply[T <: Template](source: Node)(implicit templateFactory: Node => T): T = templateFactory(source)
def apply(source: Node)(implicit templateFactory: Node => Template): Template = templateFactory(source)
}
......@@ -79,6 +79,8 @@ class TemplateSpec extends WordSpec {
</nonpublicExecutionReport>
</document>
}
def isStraight: Boolean = true
}
"Template" should {
......@@ -127,17 +129,6 @@ class TemplateSpec extends WordSpec {
assertThrows[Exception] {
val template = Template(sourceXml)
}
}
}
it should {
"generate valid RTS output xml using valid NSD source xml" ignore {
val sourceXml: Node = XML.loadFile("src/test/resources/templates/nsd/Example repo NSD.xml")
val outputXml = new RepoTemplate(sourceXml).template
val checkedXml = XMLValid("src/test/resources/templates/rts/fpml-recordkeeping-merged-schema.xsd").loadString(outputXml.mkString)
assert(checkedXml.isInstanceOf[Node])
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment