-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
126 lines (110 loc) · 2.49 KB
/
model.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
package parser
import (
"errors"
"fmt"
"github.com/hellgate75/general_utils/common"
errs "github.com/hellgate75/general_utils/errors"
"runtime"
"time"
)
type LogStreamType int
var testFlag bool = false
var testChan chan interface{}
const (
StdOutStreamType LogStreamType = 101
FileStreamType LogStreamType = 102
UrlStreamType LogStreamType = 103
)
func WriterTypeToLogStreamType(wTp common.WriterType) (LogStreamType, error) {
switch wTp {
case common.StdOutWriter:
return StdOutStreamType, nil
case common.FileWriter:
return FileStreamType, nil
case common.UrlWriter:
return UrlStreamType, nil
default:
return 0, errors.New("Unkown Writer Type")
}
}
type LogStream interface {
Open() error
Close() error
Write(interface{}) error
}
type RotatePolicy struct {
size int
unit common.StorageUnit
frequency int
timeUnit time.Duration
history int
}
type _logStdOutStruct struct {
_logFormat common.StreamInOutFormat
converter ConverterFunc
open bool
}
type _logFileStreamStruct struct {
file string
format common.StreamInOutFormat
policy RotatePolicy
converter ConverterFunc
open bool
}
type _logUrlStreamStruct struct {
url string
format common.StreamInOutFormat
converter ConverterFunc
open bool
}
func (l *_logStdOutStruct) Open() error {
var err error
defer func() {
itf := recover()
if errs.IsError(itf) {
err = itf.(error)
} else {
err = errors.New(fmt.Sprintf("Unknown Error Occured : %v", itf))
}
}()
l.converter = getConverterByStreamInOutFormat(l._logFormat)
l.open = (err == nil)
return err
}
func (l *_logStdOutStruct) Close() error {
if !l.open {
return nil
}
l.open = false
return nil
}
func (l *_logStdOutStruct) Write(data interface{}) error {
if l.converter == nil {
return errors.New("Start Log Stream before write content")
}
body, err := l.converter(data)
if err != nil {
return err
}
if testFlag && testChan != nil {
testChan <- string(body)
} else {
fmt.Println(string(body))
}
return nil
}
func New(logType LogStreamType, format common.StreamInOutFormat) (LogStream, error) {
//TODO: Complete File and Url Stream Implementations and place into this method
switch logType {
case StdOutStreamType:
var los _logStdOutStruct = _logStdOutStruct{
_logFormat: format,
}
runtime.SetFinalizer(&los, func(los *_logStdOutStruct) {
los.Close()
})
return &los, nil
default:
return nil, errors.New("log::parser::error : Unknown or Not Implemented Log Stream Type")
}
}