Beispiel #1
0
func InitConfigFromFile(filename string) (*cfg.Cfg, error) {
	ret := cfg.NewCfg(filename)
	if err := ret.Load(); err != nil {
		return nil, errors.Trace(err)
	}
	return ret, nil
}
Beispiel #2
0
func (r *rollingFile) roll() error {
	if r.file != nil {
		// 还没有日期切换
		now := time.Now()
		if now.Before(r.nextStartOfDate) {
			return nil
		}
		r.file.Close()
		r.file = nil
		r.nextStartOfDate = nextStartOfDay(now)

		// 将logKeepDays之前几天的日志删除
		for i := r.logKeepDays; i < r.logKeepDays+4; i++ {

			filePath := fmt.Sprintf("%s-%s", r.basePath, now.AddDate(0, 0, -i).Format("20060102"))
			_, err := os.Stat(filePath)
			// 如果存在,则删除
			if !os.IsNotExist(err) {
				os.Remove(filePath)
			}
		}

	}

	// 例如: proxy.log-20140101
	r.filePath = fmt.Sprintf("%s-%s", r.basePath, time.Now().Format("20060102"))

	f, err := os.OpenFile(r.filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		return errors.Trace(err)
	} else {
		r.file = f
		return nil
	}
}
Beispiel #3
0
func Parse(s string) (int64, error) {
	if !BytesizeRegexp.MatchString(s) {
		return 0, errors.Trace(ErrBadBytesize)
	}

	subs := BytesizeRegexp.FindStringSubmatch(s)
	if len(subs) != 3 {
		return 0, errors.Trace(ErrBadBytesize)
	}

	size := int64(0)
	switch strings.ToUpper(string(subs[2])) {
	case "B", "":
		size = 1
	case "KB", "K":
		size = KB
	case "MB", "M":
		size = MB
	case "GB", "G":
		size = GB
	case "TB", "T":
		size = TB
	case "PB", "P":
		size = PB
	default:
		return 0, errors.Trace(ErrBadBytesizeUnit)
	}

	text := subs[1]
	if digitsRegexp.MatchString(text) {
		n, err := strconv.ParseInt(text, 10, 64)
		if err != nil {
			return 0, errors.Trace(ErrBadBytesize)
		}
		size *= n
	} else {
		n, err := strconv.ParseFloat(text, 64)
		if err != nil {
			return 0, errors.Trace(ErrBadBytesize)
		}
		size = int64(float64(size) * n)
	}
	return size, nil
}
Beispiel #4
0
func (top *Topology) WatchChildren(path string, evtbus chan interface{}) ([]string, error) {
	// 获取Children的信息
	content, _, evtch, err := top.ZkConn.ChildrenW(path)
	if err != nil {
		return nil, errors.Trace(err)
	}

	go top.doWatch(evtch, evtbus)
	return content, nil
}
Beispiel #5
0
func (r *rollingFile) Write(b []byte) (int, error) {
	r.mu.Lock()
	defer r.mu.Unlock()

	if r.closed {
		return 0, errors.Trace(ErrClosedRollingFile)
	}

	if err := r.roll(); err != nil {
		return 0, err
	}

	n, err := r.file.Write(b)
	if err != nil {
		return n, errors.Trace(err)
	} else {
		return n, nil
	}
}
Beispiel #6
0
func InitConfig() (*cfg.Cfg, error) {
	configFile := os.Getenv("CODIS_CONF")
	if len(configFile) == 0 {
		configFile = "config.ini"
	}
	ret := cfg.NewCfg(configFile)
	if err := ret.Load(); err != nil {
		return nil, errors.Trace(err)
	} else {
		return ret, nil
	}
}
Beispiel #7
0
// 读取当前path对应的数据,监听之后的事件
func (top *Topology) WatchNode(path string, evtbus chan interface{}) ([]byte, error) {
	content, _, evtch, err := top.ZkConn.GetW(path)
	if err != nil {
		return nil, errors.Trace(err)
	}

	// 从: evtch 读取数据,然后再传递到 evtbus
	// evtbus 是外部可控的 channel
	if evtbus != nil {
		go top.doWatch(evtch, evtbus)
	}
	return content, nil
}
Beispiel #8
0
func (r *rollingFile) Close() error {
	r.mu.Lock()
	defer r.mu.Unlock()

	if r.closed {
		return nil
	}

	r.closed = true
	if f := r.file; f != nil {
		r.file = nil
		return errors.Trace(f.Close())
	}
	return nil
}
Beispiel #9
0
func OpenFile(path string) (*os.File, error) {
	f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
	return f, errors.Trace(err)
}