Browse Source

добавил логгер для веб-браузера

теперь можно отлаживать поэлементно
kpmy 10 năm trước cách đây
mục cha
commit
1aa78f574f
9 tập tin đã thay đổi với 114 bổ sung18 xóa
  1. 2 1
      .gitignore
  2. 39 0
      api/view.go
  3. 3 1
      ncl/std/elems.go
  4. 1 1
      ncl/std/pins.go
  5. 2 3
      sim.go
  6. 7 7
      tri/tri.go
  7. 2 0
      web/index.html
  8. 57 2
      web/main.dart
  9. 1 3
      web/styles/main.css

+ 2 - 1
.gitignore

@@ -29,4 +29,5 @@ _testmain.go
 .pub/
 build/
 packages
-pubspec.lock 
+pubspec.lock 
+/pubspec.lock

+ 39 - 0
api/view.go

@@ -0,0 +1,39 @@
+package api
+
+import (
+	"github.com/ivpusic/neo"
+	"sim3/tri"
+	"time"
+)
+
+type Item struct {
+	Timestamp int64
+	Name      string
+	Type      string
+	Meta      bool
+	Signal    tri.Trit
+}
+
+var LogChannel chan *Item = make(chan *Item, 1024)
+
+func Log(i *Item) {
+	i.Timestamp = time.Now().Unix()
+	LogChannel <- i
+}
+
+func Tri(ctx *neo.Ctx) {
+	var il []*Item
+	for ok := true; ok; {
+		select {
+		case i := <-LogChannel:
+			il = append(il, i)
+		default:
+			ok = false
+		}
+	}
+	if il != nil {
+		ctx.Res.Json(il, 200)
+	} else {
+		ctx.Res.Text("[]", 200)
+	}
+}

+ 3 - 1
ncl/std/elems.go

@@ -2,6 +2,7 @@ package std
 
 import (
 	"fmt"
+	"sim3/api"
 	"sim3/ncl"
 	"sim3/tri"
 	"ypk/assert"
@@ -37,7 +38,8 @@ func NewProbe(n string) (ret ncl.Element) {
 	go func(p *probe) {
 		ncl.Step(p, func() {
 			meta, signal := p.I.Select()
-			fmt.Println("probe", p.name, meta, signal)
+			api.Log(&api.Item{Name: p.name, Type: "probe", Meta: meta, Signal: signal})
+			fmt.Println(p.name, meta, signal)
 		})
 	}(ret.(*probe))
 	return

+ 1 - 1
ncl/std/pins.go

@@ -64,7 +64,7 @@ func (p *point) sel() (meta tri.Trit, signal tri.Trit) {
 		switch x := _x.(type) {
 		case *out:
 			assert.For(meta == tri.FALSE, 100)
-			meta := <-x.meta
+			meta = <-x.meta
 			if meta == tri.TRUE {
 				signal = <-x.signal
 			}

+ 2 - 3
sim.go

@@ -4,6 +4,7 @@ import (
 	"github.com/ivpusic/neo"
 	"github.com/ivpusic/neo-cors"
 	"runtime"
+	"sim3/api"
 	"sim3/ncl"
 	"sim3/ncl/std"
 	"sync"
@@ -15,9 +16,7 @@ func main() {
 	nw := func() {
 		app := neo.App()
 		app.Use(cors.Init())
-		app.Get("/tri.json", func(ctx *neo.Ctx) {
-			ctx.Res.Text("[]", 200)
-		})
+		app.Get("/tri.json", api.Tri)
 		app.Start()
 	}
 	go nw()

+ 7 - 7
tri/tri.go

@@ -1,19 +1,19 @@
 package tri
 
 /* троичная логика */
-var TRUE Trit = Trit{n: false, t: true}
-var FALSE Trit = Trit{n: false, t: false}
-var NIL Trit = Trit{n: true, t: false}
+var TRUE Trit = Trit{N: false, T: true}
+var FALSE Trit = Trit{N: false, T: false}
+var NIL Trit = Trit{N: true, T: false}
 
 type Trit struct {
-	n bool
-	t bool
+	N bool
+	T bool
 }
 
 func (t Trit) String() string {
-	if t.n {
+	if t.N {
 		return "%nil"
-	} else if t.t {
+	} else if t.T {
 		return "%true"
 	} else {
 		return "%false"

+ 2 - 0
web/index.html

@@ -9,6 +9,8 @@
     <link rel="stylesheet" href="styles/main.css">
 </head>
 <body>
+  <div id="data">
+  </div>
 <script type="application/dart" src="main.dart"></script>
 <script data-pub-inline src="packages/browser/dart.js"></script>
 </body>

+ 57 - 2
web/main.dart

@@ -7,15 +7,70 @@ import 'dart:convert';
 
 var Q = querySelector;
 
-void main() {
+class Item implements Comparable{
+  int timestamp;
+  String name;
+  String type;
+  bool meta;
+  bool signal;
+
+  @override
+  int compareTo(Item that){
+    DateTime a = new DateTime.fromMillisecondsSinceEpoch(this.timestamp);
+    DateTime b = new DateTime.fromMillisecondsSinceEpoch(that.timestamp);
+    if(a.isBefore(b))
+      return -1;
+    else if(a.isAfter(b))
+      return 1;
+    else return 0;
+  }
+
+  Item(this.timestamp, this.name, this.type, this.meta, this.signal);
+}
+
+List<Item> cache = new List();
+
+update() async{
+  (Q("#data") as DivElement).children.clear();
+  Map<String, List<Item>> map = new Map();
+  cache.forEach((i){
+    if(!map.containsKey(i.name)){
+      map[i.name] = new List<Item>();
+    }
+    map[i.name].add(i);
+  });
+  map.keys.forEach((s){
+    var id = "p"+s.hashCode.toString();
+    (Q("#data") as DivElement).appendHtml("<div id='p$id' style='white-space: nowrap;'>$s: </div>");
+    String dump = "";
+    map[s].forEach((i){
+      var sig = i.signal == null ? "0" : i.signal ? "+" : "-";
+      if(i.meta)
+        dump = "$sig $dump";
+      else
+        dump = "_  $dump";
+    });
+    (Q("#p$id") as DivElement).appendHtml(dump);
+  });
+  return 0;
+}
+
+main() async{
   var get;
   get = (){
     HttpRequest.getString("http://localhost:3000/tri.json")
     ..then((s){
       var data = JSON.decode(s);
+      List il = (data as List);
+      il.forEach((i){
+        bool sig = i["Signal"]["N"] ? null : i["Signal"]["T"];
+        cache.add(new Item(i["Timestamp"], i["Name"], i["Type"], i["Meta"], sig));
+      });
+      cache.sort();
+      update();
     })
     ..catchError((e){});
-    new Future.delayed(new Duration(milliseconds: 100), get);
+    new Future.delayed(new Duration(milliseconds: 300), get);
   };
   get();
 }

+ 1 - 3
web/styles/main.css

@@ -1,9 +1,7 @@
-@import url(https://fonts.googleapis.com/css?family=Roboto);
-
 html, body {
     width: 100%;
     height: 100%;
     margin: 0;
     padding: 0;
-    font-family: 'Roboto', sans-serif;
+    font-family: monospace;
 }