2018-02-19 17:01:00 -05:00
|
|
|
package log_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-07-13 08:36:09 -04:00
|
|
|
"strings"
|
2018-02-19 17:01:00 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
|
|
|
. "github.com/v2fly/v2ray-core/v5/common/log"
|
2018-02-19 17:01:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileLogger(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "vtest")
|
2018-07-13 08:36:09 -04:00
|
|
|
common.Must(err)
|
2018-02-19 17:01:00 -05:00
|
|
|
path := f.Name()
|
|
|
|
common.Must(f.Close())
|
|
|
|
|
|
|
|
creator, err := CreateFileLogWriter(path)
|
2018-07-13 08:36:09 -04:00
|
|
|
common.Must(err)
|
2018-02-19 17:01:00 -05:00
|
|
|
|
|
|
|
handler := NewLogger(creator)
|
|
|
|
handler.Handle(&GeneralMessage{Content: "Test Log"})
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
common.Must(common.Close(handler))
|
|
|
|
|
|
|
|
f, err = os.Open(path)
|
2018-07-13 08:36:09 -04:00
|
|
|
common.Must(err)
|
2020-10-11 07:22:46 -04:00
|
|
|
defer f.Close()
|
2018-02-19 17:01:00 -05:00
|
|
|
|
|
|
|
b, err := buf.ReadAllToBytes(f)
|
2018-07-13 08:36:09 -04:00
|
|
|
common.Must(err)
|
|
|
|
if !strings.Contains(string(b), "Test Log") {
|
|
|
|
t.Fatal("Expect log text contains 'Test Log', but actually: ", string(b))
|
|
|
|
}
|
2018-02-19 17:01:00 -05:00
|
|
|
}
|