Skip to content

Commit

Permalink
Add exec example
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansissel committed Apr 16, 2012
1 parent be4062b commit 6217d8c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 134 deletions.
7 changes: 7 additions & 0 deletions asm/test.c
@@ -0,0 +1,7 @@
#include <stdio.h>

char *foo = "Hello world";
int main() {
printf("%s\n", foo);
return 42;
}
40 changes: 0 additions & 40 deletions go/cabin/cabin.go

This file was deleted.

92 changes: 1 addition & 91 deletions go/cabin/httpexample.go
@@ -1,7 +1,7 @@
package main

import (
"./cabin"
"cabin"
"http"
"bytes"
"reflect"
Expand All @@ -28,96 +28,6 @@ func main() {
//logger.Log(&cabin.Event{Message: "Hello world", Object: response})
}

func StdoutLogger(channel chan *cabin.Event) {
for {
event := <- channel
emit(event)
}
}

func emit(event *cabin.Event) {
/* TODO(sissel): Improve map support */
type_ := reflect.TypeOf(event.Object)
value := reflect.ValueOf(event.Object)

/* Dereference reflected pointer values */
if type_.Kind() == reflect.Ptr {
type_ = type_.Elem()
}
if value.Kind() == reflect.Ptr {
value = value.Elem()
}

switch type_.Kind() {
case reflect.Map:
log_map(event, type_, value)
case reflect.String:
log_string(event, type_, value)
case reflect.Struct:
/* Do special handling for the cabin.Event struct */
fmt.Println(type_.Name())
if type_.Name() == "Event" {
e := event.Object.(*cabin.Event)
e.Timestamp = event.Timestamp
log_event(e)
} else {
log_struct(event, type_, value)
}
default:
fmt.Printf("Unsupported type: %s\n", type_.Kind())
}
}

func log_string(event *cabin.Event, type_ reflect.Type, value reflect.Value) {
event.Message = value.String()

fmt.Printf("%s: %s\n", event.Timestamp, value.String())
}

func log_map(event *cabin.Event, type_ reflect.Type, value reflect.Value) {
fmt.Printf("%s: ", event.Timestamp)
for _, key := range value.MapKeys() {
fmt.Printf("%s=%s", key, value.MapIndex(key))
}
fmt.Printf("\n")
}

func log_struct(event *cabin.Event, type_ reflect.Type, value reflect.Value) {
fmt.Printf("%s: ", event.Timestamp)
/* For every field in this object, emit the name, type_, and current value */
for i := 0; i < type_.NumField(); i++ {
field := type_.Field(i)
if field.Anonymous {
continue;
}

switch field.Type.Kind() {
case reflect.Int:
fmt.Printf("%s(%s)=%d, ", field.Name, field.Type, value.Field(i).Int())
case reflect.Float32, reflect.Float64:
fmt.Printf("%s(%s)=%f, ", field.Name, field.Type, value.Field(i).Float())
case reflect.String:
fmt.Printf("%s(%s)=%s, ", field.Name, field.Type, value.Field(i).String())
case reflect.Interface:
fmt.Printf("%s(%s)=%s, ", field.Name, field.Type, value.Field(i).Interface())
case reflect.Bool:
fmt.Printf("%s(%s)=%s, ", field.Name, field.Type, value.Field(i).Bool())
}
}
fmt.Printf("\n")
}

func log_event(event *cabin.Event) {
fmt.Printf("%s: ", event.Timestamp)
if len(event.Message) > 0 {
fmt.Printf("%s", event.Message)
}

if event.Object != nil {

}
fmt.Printf("\n")
}

func JSONLogger(channel chan *cabin.Event) {
for {
Expand Down
11 changes: 8 additions & 3 deletions go/devlog/foo.go
@@ -1,15 +1,20 @@
package main

import "fmt"
import "net"
import (
"fmt"
"os"
"net"
)

func main() {
addr, err := net.ResolveUnixAddr("unixgram", "/dev/log")
fmt.Println(err)
//conn, err := net.DialUnix("unixgram", nil, addr)
conn, err := net.ListenUnixgram("unixgram", addr)
fmt.Println(err)

// Ensure world writable access
os.Chmod("/dev/log", 0666)

data := make([]byte, 4096)
len, _, err := conn.ReadFrom(data)
fmt.Printf("Got: %d bytes\n", len)
Expand Down
14 changes: 14 additions & 0 deletions go/run/get-exit-code.go
@@ -0,0 +1,14 @@
package main
import (
"syscall"
"os/exec"
"fmt"
)

func main() {
cmd := exec.Command("/bin/sh", "-c", "exit 42")
cmd.Start()
result := cmd.Wait()
status := result.(*exec.ExitError).ProcessState.Sys().(syscall.WaitStatus)
fmt.Printf("%v\n", status.ExitStatus())
}
16 changes: 16 additions & 0 deletions go/x11/Makefile
@@ -0,0 +1,16 @@
build: test

test: test.go
GOPATH=$(PWD) go build $<

deps: src/cabin

# 'go get' doesn't support repos with dashes in the name, and breaking my pattern
# of calling repos '<language>-<repo>' is silly, especially when calling it "gocabin"
# requires all my code to use 'go...' as a namespace prefix. Not worth it.
# So just download master and unpack it into src.
.PHONY: src/cabin
src/cabin:
curl -Lso - https://github.com/jordansissel/go-cabin/tarball/master \
| tar -zvxf - --strip-components=1 '*'/src/cabin

24 changes: 24 additions & 0 deletions go/x11/test.go
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"os"
"code.google.com/p/x-go-binding/xgb"
"cabin"
)

func main() {
logger := cabin.New()
output := make(chan *cabin.Event)
logger.Subscribe(output)
go cabin.StdoutLogger(output)

conn, err := xgb.Dial(os.Getenv("DISPLAY"))
if err != nil {
fmt.Printf("Failed to connect to the X Server (%s)\n", os.Getenv("DISPLAY"))
os.Exit(1)
}

logger.Log("OK")
logger.Log(conn)
}

0 comments on commit 6217d8c

Please sign in to comment.