Skip to content

Commit 8d20a66

Browse files
committed
调度ok
1 parent e0d048e commit 8d20a66

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Binaries for programs and plugins
2+
*~
23
*.exe
34
*.exe~
45
*.dll

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#tee
2+
3+
#### 简介
4+
tee 在完成gnu tee 功能命令基础上,并增强tee命令。
5+
6+
#### install
7+
```bash
8+
env GOPATH=`pwd` go get -u github.com/guonaihong/tee
9+
```
10+
11+
#### 命令行选项
12+
```console
13+
Usage of tee:
14+
-A, --max-archive int
15+
How many archive files are saved
16+
-a, --append
17+
append to the given FILEs, do not overwrite
18+
-g, --gzip
19+
compressed archived log files
20+
-s, --max-size string
21+
current file maximum write size
22+
```
23+
24+
#### 提供对日志文件的自动归档和裁减
25+
* bash 内容
26+
```bash
27+
while :;
28+
do
29+
date
30+
done
31+
```
32+
33+
* tee命令保证始终只有10个归档文件(.gz)
34+
```bash
35+
bash loop.sh | tee -A 10 -g -a my.log
36+
```

tee.go

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,51 @@ import (
77
"github.com/guonaihong/log"
88
"io"
99
"os"
10+
"os/signal"
11+
"syscall"
1012
)
1113

14+
func ignore() {
15+
signal.Ignore(syscall.SIGABRT)
16+
signal.Ignore(syscall.SIGALRM)
17+
signal.Ignore(syscall.SIGBUS)
18+
signal.Ignore(syscall.SIGCHLD)
19+
signal.Ignore(syscall.SIGCLD)
20+
signal.Ignore(syscall.SIGCONT)
21+
signal.Ignore(syscall.SIGFPE)
22+
signal.Ignore(syscall.SIGHUP)
23+
signal.Ignore(syscall.SIGILL)
24+
signal.Ignore(syscall.SIGINT)
25+
signal.Ignore(syscall.SIGIO)
26+
signal.Ignore(syscall.SIGIOT)
27+
signal.Ignore(syscall.SIGKILL)
28+
signal.Ignore(syscall.SIGPIPE)
29+
signal.Ignore(syscall.SIGPOLL)
30+
signal.Ignore(syscall.SIGPROF)
31+
signal.Ignore(syscall.SIGPWR)
32+
signal.Ignore(syscall.SIGQUIT)
33+
signal.Ignore(syscall.SIGSEGV)
34+
signal.Ignore(syscall.SIGSTKFLT)
35+
signal.Ignore(syscall.SIGSTOP)
36+
signal.Ignore(syscall.SIGSYS)
37+
signal.Ignore(syscall.SIGTERM)
38+
signal.Ignore(syscall.SIGTRAP)
39+
//signal.Ignore(syscall.SIGTSTP) //ctrl-z
40+
signal.Ignore(syscall.SIGTTIN)
41+
signal.Ignore(syscall.SIGTTOU)
42+
signal.Ignore(syscall.SIGUNUSED)
43+
signal.Ignore(syscall.SIGURG)
44+
signal.Ignore(syscall.SIGUSR1)
45+
signal.Ignore(syscall.SIGUSR2)
46+
signal.Ignore(syscall.SIGVTALRM)
47+
signal.Ignore(syscall.SIGWINCH)
48+
signal.Ignore(syscall.SIGXCPU)
49+
signal.Ignore(syscall.SIGXFSZ)
50+
}
51+
1252
func main() {
1353
append := flag.Bool("a, append", false, "append to the given FILEs, do not overwrite")
14-
//ignoreInterrupts := flag.Bool("i, ignore-interrupts", false, "ignore interrupt signals")//todo
54+
ignoreInterrupts := flag.Bool("i, ignore-interrupts", false, "ignore interrupt signals") //todo
1555
gzip := flag.Bool("g, gzip", false, "compressed archived log files")
1656
maxSize := flag.String("s, max-size", "0", "current file maximum write size")
1757
maxArchive := flag.Int("A, max-archive", 0, "How many archive files are saved")
@@ -20,11 +60,20 @@ func main() {
2060

2161
var fileArch *log.File
2262
var buffer [4096]byte
23-
63+
var fileName string
2464
var w io.Writer
2565

66+
if *ignoreInterrupts {
67+
ignore()
68+
}
69+
2670
args := flag.Args()
27-
fileName := args[0]
71+
if len(args) == 0 {
72+
w = os.Stdout
73+
goto end
74+
}
75+
76+
fileName = args[0]
2877
if *maxSize != "" {
2978
size, err := file.ParseSize(*maxSize)
3079
if err != nil {
@@ -55,11 +104,15 @@ func main() {
55104
w = file
56105
}
57106

107+
end:
58108
for {
59109
n, err := os.Stdin.Read(buffer[:])
60110
if err != nil {
61111
break
62112
}
63113
w.Write(buffer[:n])
114+
if len(args) != 0 {
115+
os.Stdout.Write(buffer[:n])
116+
}
64117
}
65118
}

0 commit comments

Comments
 (0)