Пример #1
0
// FormatTime formats a given time structure accroding to the settings.
func (e *Environment) FormatTime(timestamp *time.Time) (formatted string) {
	if e.HistTimeFormat != "" {
		formatted = strftime.Format(e.HistTimeFormat, *timestamp)
	}

	return
}
Пример #2
0
// Generate implements i3.Generator
func (c Clock) Generate() ([]i3.Output, error) {
	if c.Location == "" {
		c.Location = "Local"
	}

	l, err := time.LoadLocation(c.Location)
	if err != nil {
		return nil, err
	}

	t := time.Now()
	st := timeFormat.Format(c.Format, t.In(l))

	color := c.Color
	if color == "" {
		color = i3.DefaultColors.General
	}

	o := i3.Output{
		Name:     c.Name,
		Instance: c.Instance,

		FullText:  st,
		Color:     color,
		Separator: true,
	}

	return []i3.Output{o}, nil
}
Пример #3
0
// Generate implements i3.Generator
func (c Clock) Generate() ([]i3.Output, error) {
	st := timeFormat.Format(c.format, time.Now())

	o := i3.Output{
		FullText:  st,
		Color:     "#FFFFFF",
		Separator: true,
	}

	return []i3.Output{o}, nil
}
func Now() string {
	format := "%Y.%m.%d.%H.%M.%S.%z"
	now := time.Now()
	return strftime.Format(format, now)
}
Пример #5
0
func main() {
	params := ParseArgs()
	if !ValidateParams(params) {
		os.Exit(1)
	}
	logWriter := (io.Writer)(nil)
	if params.LogFile != "" {
		logWriter = ioextras.NewStaticRotatingWriter(
			func(_ interface{}) (string, error) {
				path := strftime.Format(params.LogFile, time.Now())
				return path, nil
			},
			func(path string, _ interface{}) (io.Writer, error) {
				dir, _ := filepath.Split(path)
				err := os.MkdirAll(dir, os.FileMode(0777))
				if err != nil {
					return nil, err
				}
				return os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.FileMode(0666))
			},
			nil,
		)
	} else {
		logWriter = os.Stderr
	}
	logBackend := logging.NewLogBackend(logWriter, "[fluentd-forwarder] ", log.Ldate|log.Ltime|log.Lmicroseconds)
	logging.SetBackend(logBackend)
	logger := logging.MustGetLogger("fluentd-forwarder")
	logging.SetLevel(params.LogLevel, "fluentd-forwarder")
	if progVersion != "" {
		logger.Info("Version %s starting...", progVersion)
	}

	workerSet := fluentd_forwarder.NewWorkerSet()

	if params.CPUProfileFile != "" {
		f, err := os.Create(params.CPUProfileFile)
		if err != nil {
			Error(err.Error())
			os.Exit(1)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	output := (PortWorker)(nil)
	err := (error)(nil)
	switch params.OutputType {
	case "fluent":
		output, err = fluentd_forwarder.NewForwardOutput(
			logger,
			params.ForwardTo,
			params.RetryInterval,
			params.ConnectionTimeout,
			params.WriteTimeout,
			params.FlushInterval,
			params.JournalGroupPath,
			params.MaxJournalChunkSize,
			params.Metadata,
		)
	case "td":
		rootCAs := (*x509.CertPool)(nil)
		if params.SslCACertBundleFile != "" {
			b, err := ioutil.ReadFile(params.SslCACertBundleFile)
			if err != nil {
				Error("Failed to read CA bundle file: %s", err.Error())
				os.Exit(1)
			}
			rootCAs = x509.NewCertPool()
			if !rootCAs.AppendCertsFromPEM(b) {
				Error("No valid certificate found in %s", params.SslCACertBundleFile)
				os.Exit(1)
			}
		}
		output, err = fluentd_forwarder.NewTDOutput(
			logger,
			params.ForwardTo,
			params.ConnectionTimeout,
			params.WriteTimeout,
			params.FlushInterval,
			params.Parallelism,
			params.JournalGroupPath,
			params.MaxJournalChunkSize,
			params.ApiKey,
			params.DatabaseName,
			params.TableName,
			"",
			params.Ssl,
			rootCAs,
			"", // TODO:http-proxy
			params.Metadata,
		)
	}
	if err != nil {
		Error("%s", err.Error())
		return
	}
	workerSet.Add(output)
	input, err := fluentd_forwarder.NewForwardInput(logger, params.ListenOn, output)
	if err != nil {
		Error(err.Error())
		return
	}
	workerSet.Add(input)

	signalHandler := NewSignalHandler(workerSet)
	input.Start()
	output.Start()
	signalHandler.Start()

	for _, worker := range workerSet.Slice() {
		worker.WaitForShutdown()
	}
	logger.Notice("Shutting down...")
}
Пример #6
0
func TimeStamp() (TimeStamp string) {
	TimeStamp = strftime.Format("%Y.%m.%d.%H.%M.%S.%z", time.Now())
	return
}
Пример #7
0
func main() {
	flagAppend := flag.BoolP("append", "a", false, "append to file")
	timeFormat := flag.String("format", "%Y-%m-%d %H:%M:%S", "time format, compatible with strftime(3)")
	flag.Parse()

	var outStreams []*os.File = []*os.File{os.Stdout}
	for i := 0; i < len(flag.Args()); i++ {
		if *flagAppend {
			f, _ := os.OpenFile(flag.Args()[i], os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0644)
			outStreams = append(outStreams, f)
		} else {
			f, _ := os.OpenFile(flag.Args()[i], os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0644)
			outStreams = append(outStreams, f)
		}
	}

	const kBufferSize = 1
	inputBuffer := make([]byte, kBufferSize)
	outputBuffer := make([]byte, kBufferSize)
	var outputBufferSize int = 0

	kSeparator := []byte{' '}
	const kLF byte = '\x0a'

	AppendBuffer := func(buffer []byte) {
		var remaining int = len(buffer)
		var offset int = 0
		for remaining > 0 {
			amount := remaining
			if outputBufferSize + amount > kBufferSize {
				amount = kBufferSize - outputBufferSize
			}
			for i := 0; i < amount; i++ {
				outputBuffer[outputBufferSize] = buffer[i + offset]
				outputBufferSize++
			}
			if outputBufferSize == kBufferSize {
				for _, stream := range outStreams {
					stream.Write(outputBuffer)
				}
				outputBufferSize = 0
			}
			remaining -= amount
			offset += amount
		}
	}

	PrintTime := func() {
		AppendBuffer([]byte(strftime.Format(*timeFormat, time.Now())))
		AppendBuffer(kSeparator)
	}

	var printTime bool = true

	for true {
		size, _ := os.Stdin.Read(inputBuffer)

		if size == 0 {
			break;
		}

		for i := 0; i < size; i++ {
			if printTime {
				PrintTime()
				printTime = false
			}

			if inputBuffer[i] == kLF {
				printTime = true
			}
			AppendBuffer(inputBuffer[i:i + 1])
		}
	}

	for _, stream := range outStreams {
		stream.Write(outputBuffer[0:outputBufferSize])
		stream.Close()
	}
}