@@ -282,6 +282,63 @@ func (hub *hub) checkCmd(m []byte) {
282
282
}
283
283
}
284
284
285
+ type logWriter struct {
286
+ onWrite func ([]byte )
287
+ }
288
+
289
+ func (hub * hub ) logAction (sl string ) {
290
+ if strings .HasPrefix (sl , "log on" ) {
291
+ * logDump = "on"
292
+
293
+ logWriter := logWriter {}
294
+ logWriter .onWrite = func (p []byte ) {
295
+ hub .broadcastSys <- p
296
+ }
297
+
298
+ multiWriter := io .MultiWriter (& logWriter , os .Stderr )
299
+ log .SetOutput (multiWriter )
300
+ } else if strings .HasPrefix (sl , "log off" ) {
301
+ * logDump = "off"
302
+ log .SetOutput (os .Stderr )
303
+ // } else if strings.HasPrefix(sl, "log show") {
304
+ // TODO: send all the saved log to websocket
305
+ //hub.broadcastSys <- []byte("{\"BufFlowDebug\" : \"" + *logDump + "\"}")
306
+ }
307
+ }
308
+
309
+ func (u * logWriter ) Write (p []byte ) (n int , err error ) {
310
+ u .onWrite (p )
311
+ return len (p ), nil
312
+ }
313
+
314
+ func (hub * hub ) memoryStats () {
315
+ var memStats runtime.MemStats
316
+ runtime .ReadMemStats (& memStats )
317
+ json , _ := json .Marshal (memStats )
318
+ log .Printf ("memStats:%v\n " , string (json ))
319
+ hub .broadcastSys <- json
320
+ }
321
+
322
+ func (hub * hub ) getHostname () {
323
+ hub .broadcastSys <- []byte ("{\" Hostname\" : \" " + * hostname + "\" }" )
324
+ }
325
+
326
+ func (hub * hub ) getVersion () {
327
+ hub .broadcastSys <- []byte ("{\" Version\" : \" " + version + "\" }" )
328
+ }
329
+
330
+ func (hub * hub ) garbageCollection () {
331
+ log .Printf ("Starting garbageCollection()\n " )
332
+ hub .broadcastSys <- []byte ("{\" gc\" :\" starting\" }" )
333
+ hub .memoryStats ()
334
+ debug .SetGCPercent (100 )
335
+ debug .FreeOSMemory ()
336
+ debug .SetGCPercent (- 1 )
337
+ log .Printf ("Done with garbageCollection()\n " )
338
+ hub .broadcastSys <- []byte ("{\" gc\" :\" done\" }" )
339
+ hub .memoryStats ()
340
+ }
341
+
285
342
func (hub * hub ) spHandlerOpen (portname string , baud int , buftype string ) {
286
343
287
344
log .Print ("Inside spHandler" )
@@ -415,63 +472,6 @@ func (hub *hub) spWrite(arg string) {
415
472
port .Write (data , bufferingMode )
416
473
}
417
474
418
- type logWriter struct {
419
- onWrite func ([]byte )
420
- }
421
-
422
- func (hub * hub ) logAction (sl string ) {
423
- if strings .HasPrefix (sl , "log on" ) {
424
- * logDump = "on"
425
-
426
- logWriter := logWriter {}
427
- logWriter .onWrite = func (p []byte ) {
428
- hub .broadcastSys <- p
429
- }
430
-
431
- multiWriter := io .MultiWriter (& logWriter , os .Stderr )
432
- log .SetOutput (multiWriter )
433
- } else if strings .HasPrefix (sl , "log off" ) {
434
- * logDump = "off"
435
- log .SetOutput (os .Stderr )
436
- // } else if strings.HasPrefix(sl, "log show") {
437
- // TODO: send all the saved log to websocket
438
- //hub.broadcastSys <- []byte("{\"BufFlowDebug\" : \"" + *logDump + "\"}")
439
- }
440
- }
441
-
442
- func (u * logWriter ) Write (p []byte ) (n int , err error ) {
443
- u .onWrite (p )
444
- return len (p ), nil
445
- }
446
-
447
- func (hub * hub ) memoryStats () {
448
- var memStats runtime.MemStats
449
- runtime .ReadMemStats (& memStats )
450
- json , _ := json .Marshal (memStats )
451
- log .Printf ("memStats:%v\n " , string (json ))
452
- hub .broadcastSys <- json
453
- }
454
-
455
- func (hub * hub ) getHostname () {
456
- hub .broadcastSys <- []byte ("{\" Hostname\" : \" " + * hostname + "\" }" )
457
- }
458
-
459
- func (hub * hub ) getVersion () {
460
- hub .broadcastSys <- []byte ("{\" Version\" : \" " + version + "\" }" )
461
- }
462
-
463
- func (hub * hub ) garbageCollection () {
464
- log .Printf ("Starting garbageCollection()\n " )
465
- hub .broadcastSys <- []byte ("{\" gc\" :\" starting\" }" )
466
- hub .memoryStats ()
467
- debug .SetGCPercent (100 )
468
- debug .FreeOSMemory ()
469
- debug .SetGCPercent (- 1 )
470
- log .Printf ("Done with garbageCollection()\n " )
471
- hub .broadcastSys <- []byte ("{\" gc\" :\" done\" }" )
472
- hub .memoryStats ()
473
- }
474
-
475
475
func (hub * hub ) spErr (err string ) {
476
476
//log.Println("Sending err back: ", err)
477
477
hub .broadcastSys <- []byte ("{\" Error\" : \" " + err + "\" }" )
0 commit comments