initialise repo
[debian/orchestra.git] / src / orchestra / shared.go
1 /* various important shared defaults. */
2 package orchestra
3
4 import (
5         "os"
6         "net"
7         "syslog"
8         "fmt"
9         "runtime/debug"
10 )
11
12 const (
13         DefaultMasterPort = 2258
14         DefaultHTTPPort = 2259
15 )
16
17 var     logWriter  *syslog.Writer = nil
18
19 func SetLogName(name string) {
20         if nil != logWriter {
21                 logWriter.Close()
22                 logWriter = nil
23         }
24         var err os.Error
25         logWriter, err = syslog.New(syslog.LOG_DEBUG, name)
26         MightFail(err, "Couldn't reopen syslog")
27 }
28
29
30 func Debug(format string, args ...interface{}) {
31         if nil != logWriter {
32                 logWriter.Debug(fmt.Sprintf(format, args...))
33         }
34 }
35
36 func Info(format string, args ...interface{}) {
37         if nil != logWriter {
38                 logWriter.Info(fmt.Sprintf(format, args...))
39         }
40 }
41
42 func Warn(format string, args ...interface{}) {
43         if nil != logWriter {
44                 logWriter.Warning(fmt.Sprintf(format, args...))
45         }
46 }
47
48 func Fail(mesg string, args ...interface {}) {
49         if nil != logWriter {
50                 logWriter.Err(fmt.Sprintf(mesg, args...))
51         }
52         fmt.Fprintf(os.Stderr, "ERR: "+mesg+"\n", args...);
53         os.Exit(1)
54 }       
55
56 func MightFail(err os.Error, mesg string, args ...interface {}) {
57         if (nil != err) {
58                 imesg := fmt.Sprintf(mesg, args...)
59                 Fail("%s: %s", imesg, err.String())
60         }
61 }
62
63 // Throws a generic assertion error, stacktraces, dies.
64 // only really to be used where the runtime-time configuration
65 // fucks up internally, not for user induced errors.
66 func Assert(mesg string, args ...interface{}) {
67         fmt.Fprintf(os.Stderr, mesg, args...)
68         debug.PrintStack()
69         os.Exit(1)
70 }
71
72 func ProbeHostname() (fqdn string) {
73         var shortHostname string
74
75         shortHostname, err := os.Hostname()
76         addr, err := net.LookupHost(shortHostname)
77         MightFail(err, "Failed to get address for hostname")
78         hostnames, err := net.LookupAddr(addr[0])
79         MightFail(err, "Failed to get full hostname for address")
80
81         return hostnames[0]
82 }