Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

- Add woodchip; a sample program using lines from stdin as input to

  cabin. Supports stdout and "syslog" at this point.

  Aims to replace logger(1)
  • Loading branch information...
commit 09ba93398ed9891905abe019936698c7ddd8c84c 1 parent d79931b
Jordan Sissel authored

Showing 1 changed file with 62 additions and 0 deletions. Show diff stats Hide diff stats

  1. +62 0 example/woodchip.go
62 example/woodchip.go
... ... @@ -0,0 +1,62 @@
  1 +package main
  2 +
  3 +import (
  4 + "io"
  5 + "bufio"
  6 + "os"
  7 + "flag"
  8 + "strconv"
  9 + "net" /* for SplitHostPort */
  10 + "cabin" // "github.com/jordansissel/go-cabin/src/cabin"
  11 + //"fmt"
  12 +)
  13 +
  14 +var syslog_host = flag.String("syslog-host", "", "the host:port to send events to")
  15 +
  16 +/**
  17 + * Take stdin as a cabin input
  18 + */
  19 +func main() {
  20 + logger := &cabin.Cabin{}
  21 + stdin := bufio.NewReaderSize(os.Stdin, 16834)
  22 +
  23 + /* TODO(sissel): Make this tunable */
  24 + logger.Subscribe(cabin.Stdout{})
  25 +
  26 + flag.Parse()
  27 +
  28 + /* TODO(sissel): Improve the API for this */
  29 + if len(*syslog_host) > 0 {
  30 + logger.Logf("Subscribing to syslog %s", *syslog_host)
  31 + host, port_str, _ := net.SplitHostPort(*syslog_host)
  32 + port, _ := strconv.ParseUint(port_str, 10, 16)
  33 + syslog := cabin.Syslog{Host: host, Port: uint(port)}
  34 + logger.Subscribe(syslog)
  35 + }
  36 +
  37 + for {
  38 + /* Read lines from stdin */
  39 + line, err := stdin.ReadString('\n')
  40 +
  41 + if err != nil {
  42 + exitCode := 0
  43 + switch err {
  44 + case io.EOF: /* Expected if stdin closes, don't complain. */
  45 + default:
  46 + exitCode = 1
  47 + logger.Logf("Unexpected error: %v", err)
  48 + }
  49 +
  50 + /* On any error, close up the logger and shutdown */
  51 + logger.Close()
  52 +
  53 + /* This will terminate us. */
  54 + os.Exit(exitCode)
  55 + } /* end error checking */
  56 +
  57 + /* log the line received */
  58 + logger.Log(line[0:len(line) - 1])
  59 + } /* loop forever */
  60 +
  61 + panic("should not get here")
  62 +} /* main */

0 comments on commit 09ba933

Please sign in to comment.
Something went wrong with that request. Please try again.