-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
128 lines (107 loc) · 3.06 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"flag"
"fmt"
"github.com/andrewdjackson/rosco"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/andrewdjackson/memsfcr/fcr"
log "github.com/sirupsen/logrus"
)
//
// This file is the main function within the main package. It sets up the logging
// and extracts the application information (e.g version, location etc.)
// The fault code reader functionality is managed in the memsreader.go within
// the main package
//
var (
// Version of the application
Version string
// Build date
Build string
)
func init() {
// if the version is not written into the binary
// then read the version from the version file and set the build date to Now
if strings.Compare(Version, "") == 0 {
version, err := ioutil.ReadFile("version")
if err != nil {
Version = "0.0.0"
} else {
Version = string(version)
Version = strings.TrimSuffix(Version, "\n")
}
}
currentTime := time.Now()
Build = currentTime.Format("2006-01-02")
}
func setupLogging(debug bool) {
if debug {
// create a log file using the current date and time
// this saves trying to roll logs
currentTime := time.Now()
dateTime := currentTime.Format("2006-01-02 15:04:05")
dateTime = strings.ReplaceAll(dateTime, ":", "")
dateTime = strings.ReplaceAll(dateTime, " ", "-")
filename := fmt.Sprintf("%s/debug-%s.log", rosco.GetDebugFolder(), dateTime)
filename = filepath.FromSlash(filename)
// write logs to file and console
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
log.WithFields(log.Fields{"error": err}).Warn("error opening log file")
}
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
TimestampFormat: "15:04:05.000",
})
multilogwriter := io.MultiWriter(os.Stdout, f)
log.SetOutput(multilogwriter)
log.Infof("debug logging to %s", filename)
} else {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
ForceColors: false,
DisableColors: false,
FullTimestamp: true,
TimestampFormat: "15:04:05.000",
})
}
// disable function logging
log.SetReportCaller(false)
}
func main() {
var debug bool
var headless bool
flag.BoolVar(&debug, "debug", true, "output to a debug file")
flag.BoolVar(&headless, "headless", false, "headless server mode")
flag.Parse()
// initialise the logging
fcr.CreateFolders()
setupLogging(debug)
log.Infof("MemsFCR Version %s, Build %s", Version, Build)
log.Infof("MemsFCR Home Folder %s", rosco.GetHomeFolder())
log.Infof("MemsFCR App Folder %s", rosco.GetAppFolder())
log.Infof("MemsFCR Log Folder %s", rosco.GetLogFolder())
log.Infof("MemsFCR Debug Folder %s", rosco.GetDebugFolder())
// create a channel to notify app to exit
exit := make(chan int)
// set up and initialise the fault code reader
reader := fcr.NewMemsReader(Version, Build, headless)
// start the web server
reader.StartWebServer()
if !headless {
// open the browser view
reader.OpenBrowser()
} else {
log.Infof("MemsFCR started in headless mode")
}
// wait for exit on the channel
for {
<-exit
}
}