-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteabox_app.go
99 lines (82 loc) · 2.55 KB
/
teabox_app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package teabox
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/isbm/crtview"
"gitlab.com/isbm/teabox/teaboxlib"
)
// Global reference of the Application. It is accessed via GetTeaboxApp() function.
var __APP_REF *TeaboxApplication
var __MSG_REF string
// TeaboxApplication singleton
type TeaboxApplication struct {
callback *teaboxlib.TeaboxSocketServer
config *teaboxlib.TeaConf
session *teaboxlib.TeaboxRuntimeSession
*crtview.Application
}
// GetCallbackServer, responsible to the Unix socket and modules callbacks
func (ta *TeaboxApplication) GetCallbackServer() *teaboxlib.TeaboxSocketServer {
return ta.callback
}
// SetGlobalConfig file to the application
func (ta *TeaboxApplication) SetGlobalConfig(conf *teaboxlib.TeaConf) *TeaboxApplication {
ta.config = conf
return ta
}
// GetSession instance
func (ta *TeaboxApplication) GetSession() *teaboxlib.TeaboxRuntimeSession {
return ta.session
}
// GetGlobalConfig of the application
func (ta *TeaboxApplication) GetGlobalConfig() *teaboxlib.TeaConf {
return ta.config
}
// Stop application
func (ta *TeaboxApplication) Stop(message string) {
__MSG_REF = message
ta.Application.Stop()
}
// GetTeaboxApp is a factory that returns a singleton of an Application reference
// if it wasn't creted before, it gets created.
func GetTeaboxApp() *TeaboxApplication {
if __APP_REF == nil {
__APP_REF = &TeaboxApplication{
Application: crtview.NewApplication(),
callback: teaboxlib.NewTeaboxSocketServer(),
session: teaboxlib.NewTeaboxRuntimeSession(),
}
}
return __APP_REF
}
func GetTeaboxQuitMessage() string {
return __MSG_REF
}
func DumpToFile(fn string, obj ...interface{}) {
ioutil.WriteFile(fn, []byte(spew.Sdump(obj...)), 0644)
}
// AddToFile a text. Used for quick debug purposes where using a real debugger
// is too tedious and time consuming. :-)
func AddToFile(fn string, data string) error {
f, err := os.OpenFile(fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
for _, chunk := range []string{"--------------", time.Now().Format("2006-01-02 15:04:05"), data} {
if _, err := f.WriteString(fmt.Sprintf("%v\n", strings.TrimSpace(chunk))); err != nil {
return err
}
}
return nil
}
// DebugToFile writes a data to a teabox-trace.log in the current directory.
// Since screen is locked, printing to the STDOUT is not an option, so
// this prints to a file, which one can watch with "tail -f" elsewhere.
func DebugToFile(data string) error {
return AddToFile("teabox-trace.log", data)
}