package main import ( "fmt" "io" "os" "regexp" "runtime" "strings" "sync" "syscall" "testing" "time" "unsafe" ) const lineMax = 1023 var iolock = &sync.Mutex{} var benchmarks = []testing.InternalBenchmark{} var examples = []testing.InternalExample{} var matchPat string var matchRe *regexp.Regexp func Getrlimit(resource int) (rlim Rlimit, err error) { _, _, e1 := syscall.Syscall(syscall.SYS_GETRLIMIT, uintptr(resource), uintptr(unsafe.Pointer(&rlim)), 0) if e1 != 0 { err = e1 } return } type Rlimit struct { Cur uint64 Max uint64 } const ( RLIM_INFINITY = -0x1 RLIMIT_AS = 0x9 RLIMIT_CORE = 0x4 RLIMIT_CPU = 0x0 RLIMIT_DATA = 0x2 RLIMIT_FSIZE = 0x1 RLIMIT_MEMLOCK = 0x8 RLIMIT_MSGQUEUE = 0xc RLIMIT_NICE = 0xd RLIMIT_NOFILE = 0x7 RLIMIT_NPROC = 0x6 RLIMIT_RSS = 0x5 RLIMIT_RTPRIO = 0xe RLIMIT_SIGPENDING = 0xb RLIMIT_STACK = 0x3 ) const DefaultTimeFormat = "2006-01-02T15:04:05Z" type Record struct { Priority int Timestamp int64 Filename string Linenumber int Function string Prefix string Message string Parameters []interface{} } type ioWriter struct { writer io.Writer } func write(writer *ioWriter, priority int, message string, parameters ...interface{}) { var record Record record.Timestamp = time.Now().UnixNano() var pc uintptr pc, record.Filename, record.Linenumber, _ = runtime.Caller(2) if true { function := runtime.FuncForPC(pc) if function != nil { record.Function = function.Name() } } record.Priority = priority record.Prefix = "name" record.Message = message record.Parameters = parameters if err := writer.Write(&record); err != nil { panic(err) } } func formatRecord(record *Record) (message string) { timestring := time.Unix(0, record.Timestamp).Format(DefaultTimeFormat) filestrings := strings.SplitAfter(record.Filename, "src/") message = fmt.Sprintf(record.Message, record.Parameters...) message = fmt.Sprintf("<%d>%s,%d,%s,%s:%d,%s,%s", record.Priority, timestring, record.Timestamp%1e9, record.Prefix, filestrings[len(filestrings)-1], record.Linenumber, record.Function, message) return } func NewStderrWriter() *ioWriter { return &ioWriter{os.Stderr} } func (writer *ioWriter) Write(record *Record) (error error) { iolock.Lock() defer iolock.Unlock() message := formatRecord(record) if len(message) > lineMax { message = message[:lineMax] } _, error = fmt.Fprintf(writer.writer, "%s\n", message) return } var rlimits = map[int]string{ RLIMIT_MEMLOCK: "RLIMIT_MEMLOCK", RLIMIT_NOFILE: "RLIMIT_NOFILE", RLIMIT_NPROC: "RLIMIT_NPROC", } var writer = NewStderrWriter() func Printf(message string, parameters ...interface{}) { write(writer, 7, message, parameters...) } func Hello(name string) { Printf("%s Go version: %s", name, runtime.Version()) Printf("Pid=%d Uid=%d Gid=%d", os.Getpid(), os.Getuid(), os.Getgid()) Printf("GOMAXPROCS=%d", runtime.GOMAXPROCS(0)) for resource, resStr := range rlimits { if rlim, err := Getrlimit(resource); err != nil { panic(os.NewSyscallError("getrlimit", err)) } else { Printf("%s=%+v", resStr, rlim) } } rlim, err := Getrlimit(RLIMIT_MEMLOCK) if err != nil { panic(os.NewSyscallError("getrlimit", err)) } Printf("RLIMIT_MEMLOCK=%+v", rlim) } func TestHello(t *testing.T) { Hello("test") } var tests = []testing.InternalTest{ {"TestHello", TestHello}, } func matchString(pat, str string) (result bool, err error) { if matchRe == nil || matchPat != pat { matchPat = pat matchRe, err = regexp.Compile(matchPat) if err != nil { return } } return matchRe.MatchString(str), nil } func main() { testing.Main(matchString, tests, benchmarks, examples) }