-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplefile.go
89 lines (80 loc) · 1.9 KB
/
simplefile.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
package goolog2
import (
"os"
"sync"
"sync/atomic"
)
type simpleFile struct {
writer FileWriter
mutex sync.Mutex
sync bool
refcount int32
}
// Create new simple file holder
//
// Parameters:
// filepath: name of the logging file
// sync: if true, the stream is flushed after every message
// Returns:
// the new file holder
// Note: the reference counter is set to 1. You have to invoke Unref()
// to clean up the holder.
func NewSimpleFile(
filepath string,
sync bool,
) FileHolder {
holder := &simpleFile{
sync: sync,
refcount: 1,
}
// I ignore the error here - if the file cannot be opened, the logging
// just simply doesn't work. However, if I cannot open the logging
// file, I cannot report the error.
file, _ := os.OpenFile(
filepath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
holder.writer = newSimpleFileWriter(file, true)
return holder
}
// Create new simple file holder working over an opened file handle
//
// Parameters:
// file: the opened file
// sync: if true, the stream is flushed after every message
// Returns:
// the new file holder
// Note: the reference counter is set to 1. You have to invoke Unref()
// to clean up the holder.
func NewSimpleFileHandle(
file *os.File,
sync bool,
) FileHolder {
return &simpleFile{
writer: newSimpleFileWriter(file, false),
sync: sync,
refcount: 1,
}
}
func (this *simpleFile) AccessWriter(
functor func(writer FileWriter),
) {
if this.writer != nil {
/* -- The lock avoids inter-mixing of logging lines */
this.mutex.Lock()
defer this.mutex.Unlock()
functor(this.writer)
if this.sync {
this.writer.Sync()
}
}
}
func (this *simpleFile) Ref() FileHolder {
atomic.AddInt32(&this.refcount, 1)
return this
}
func (this *simpleFile) Unref() {
refcount := atomic.AddInt32(&this.refcount, -1)
if refcount == 0 && this.writer != nil {
this.writer.Close()
this.writer = nil
}
}