github.com/cosnicolaou/llog: Update version
Of particular interest is the changes to use Android logging when the
android build tag is provided.
Change-Id: Icdfe592ecc1d3b1a02f5581b91d448e034cfd33b
diff --git a/go/src/github.com/cosnicolaou/llog/README b/go/src/github.com/cosnicolaou/llog/README
index 2dd74cc..a84923e 100644
--- a/go/src/github.com/cosnicolaou/llog/README
+++ b/go/src/github.com/cosnicolaou/llog/README
@@ -8,8 +8,11 @@
http://code.google.com/p/google-glog
-This version refactors the code so as not to rely on a single global variable
-and associated log state. It also separates out the flag parsing so that
-an application can chose to use whatever command line flags they wish to
-configure the package.
-
+Changes:
+- This version refactors code to not rely on a single global variable
+ and associated log state.
+- It separates out the flag parsing so that an application can choose
+ to use whatever command-line flags they wish to configure the
+ package.
+- When built with the 'android' build tag (such as with gomobile),
+ it uses the Android logging primitives instead of writing to files.
diff --git a/go/src/github.com/cosnicolaou/llog/README.google b/go/src/github.com/cosnicolaou/llog/README.google
index 50ca404..86c11e3 100644
--- a/go/src/github.com/cosnicolaou/llog/README.google
+++ b/go/src/github.com/cosnicolaou/llog/README.google
@@ -1,20 +1,26 @@
-URL:https://github.com/cosnicolaou/llog/archive/32b01e43fdd113fb56658658243ba7070edd5ec3.zip
-Version: 32b01e43fdd113fb56658658243ba7070edd5ec3
+URL: https://github.com/cosnicolaou/llog/archive/afed407739a24b4fec02a5a214defc813ffc750c.zip
+Version: afed407739a24b4fec02a5a214defc813ffc750c
License: Apache 2.0
License File: LICENSE
Description:
- Low level implementation of leveled execution logs for Go.
+Low level implementation of leveled execution logs for Go.
-It is a fork of the github.com/golang/glog package that is itself an efficient
+It is a fork of the github.com/golang/glog
+(c6f9652c7179652e2fd8ed7002330db089f4c9db) package that is itself an efficient
pure Go implementation of leveled logs in the manner of the open source C++
package
+
http://code.google.com/p/google-glog
-This version refactors the code so as not to rely on a single global variable
-and associated log state. It also separates out the flag parsing so that
-an application can chose to use whatever command line flags they wish to
-configure the package.
+Changes:
+- This version refactors code to not rely on a single global variable
+ and associated log state.
+- It separates out the flag parsing so that an application can choose
+ to use whatever command-line flags they wish to configure the
+ package.
+- When built with the 'android' build tag (such as with gomobile),
+ it uses the Android logging primitives instead of writing to files.
Local Modifications:
None
diff --git a/go/src/github.com/cosnicolaou/llog/glog.go b/go/src/github.com/cosnicolaou/llog/glog.go
index 9f420be..80cb084 100644
--- a/go/src/github.com/cosnicolaou/llog/glog.go
+++ b/go/src/github.com/cosnicolaou/llog/glog.go
@@ -996,6 +996,14 @@
return err
}
+func newSyncBuffer(l *Log, s Severity, now time.Time) (flushSyncWriter, error) {
+ sb := &syncBuffer{
+ logger: l,
+ sev: s,
+ }
+ return sb, sb.rotateFile(now)
+}
+
// bufferSize sizes the buffer associated with each log file. It's large
// so that log records can accumulate without the logging thread blocking
// on disk I/O. The flushDaemon will block instead.
@@ -1008,14 +1016,11 @@
// Files are created in decreasing severity order, so as soon as we find one
// has already been created, we can stop.
for s := sev; s >= InfoLog && l.file[s] == nil; s-- {
- sb := &syncBuffer{
- logger: l,
- sev: s,
- }
- if err := sb.rotateFile(now); err != nil {
+ w, err := newFlushSyncWriter(l, s, now)
+ if err != nil {
return err
}
- l.file[s] = sb
+ l.file[s] = w
}
return nil
}
diff --git a/go/src/github.com/cosnicolaou/llog/glog_android.go b/go/src/github.com/cosnicolaou/llog/glog_android.go
new file mode 100644
index 0000000..1b39822
--- /dev/null
+++ b/go/src/github.com/cosnicolaou/llog/glog_android.go
@@ -0,0 +1,78 @@
+// Copyright 2013 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// +build android
+
+package llog
+
+// #cgo LDFLAGS: -llog
+//
+// #include <stdlib.h>
+// #include <android/log.h>
+import "C"
+
+import (
+ "bytes"
+ "runtime"
+ "time"
+ "unsafe"
+)
+
+const maxLogSize = 1023 // from an off-hand comment in android/log.h
+
+type androidLogger struct {
+ prio C.int
+ tag *C.char
+}
+
+func (l *androidLogger) Flush() error { return nil }
+func (l *androidLogger) Sync() error { return nil }
+func (l *androidLogger) Write(p []byte) (int, error) {
+ n := len(p)
+ limit := bytes.IndexByte(p, '\n')
+ for limit >= 0 {
+ l.writeOneLine(p[:limit])
+ p = p[limit+1:]
+ limit = bytes.IndexByte(p, '\n')
+ }
+ l.writeOneLine(p)
+ return n, nil
+}
+func (l *androidLogger) writeOneLine(p []byte) {
+ cstr := C.CString(string(p))
+ C.__android_log_write(l.prio, l.tag, cstr)
+ C.free(unsafe.Pointer(cstr))
+}
+
+func newFlushSyncWriter(l *Log, s Severity, now time.Time) (flushSyncWriter, error) {
+ var prio C.int
+ switch {
+ case s <= InfoLog:
+ prio = C.ANDROID_LOG_INFO
+ case s <= WarningLog:
+ prio = C.ANDROID_LOG_WARN
+ case s <= ErrorLog:
+ prio = C.ANDROID_LOG_ERROR
+ case s >= FatalLog:
+ prio = C.ANDROID_LOG_FATAL
+ default:
+ prio = C.ANDROID_LOG_DEFAULT
+ }
+ ret := &androidLogger{
+ prio: prio,
+ tag: C.CString(l.name),
+ }
+ runtime.SetFinalizer(ret, func(l *androidLogger) { C.free(unsafe.Pointer(l.tag)) })
+ return ret, nil
+}
diff --git a/go/src/github.com/cosnicolaou/llog/glog_other.go b/go/src/github.com/cosnicolaou/llog/glog_other.go
new file mode 100644
index 0000000..b9d7af4
--- /dev/null
+++ b/go/src/github.com/cosnicolaou/llog/glog_other.go
@@ -0,0 +1,23 @@
+// Copyright 2013 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// +build !android
+
+package llog
+
+import "time"
+
+func newFlushSyncWriter(l *Log, s Severity, now time.Time) (flushSyncWriter, error) {
+ return newSyncBuffer(l, s, now)
+}