logger
parent
100643d8fc
commit
4284fb4048
|
@ -23,6 +23,8 @@ import (
|
||||||
|
|
||||||
// TODO put colorscheme, prompting stuff into own packages for use in the other commands. would be good to get off of survey.
|
// TODO put colorscheme, prompting stuff into own packages for use in the other commands. would be good to get off of survey.
|
||||||
|
|
||||||
|
// TODO use new lockingwriter for logging in the other external commands
|
||||||
|
|
||||||
func connectDB() (*sql.DB, error) {
|
func connectDB() (*sql.DB, error) {
|
||||||
db, err := sql.Open("sqlite3", "/town/var/codes/codes.db?mode=rw")
|
db, err := sql.Open("sqlite3", "/town/var/codes/codes.db?mode=rw")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -125,7 +127,7 @@ func (p *Prompter) Select(prompt string, opts []string) (int, error) {
|
||||||
|
|
||||||
func _main(cs colorScheme) error {
|
func _main(cs colorScheme) error {
|
||||||
lw := lockingwriter.New()
|
lw := lockingwriter.New()
|
||||||
l := log.New(lw, "help", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile|log.Lmsgprefix)
|
l := log.New(lw, "help: ", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile|log.Lmsgprefix)
|
||||||
|
|
||||||
db, err := connectDB()
|
db, err := connectDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -8,6 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// for now this package defines a writer for use with log.New(). It it intended to be used from the external ssh applications. This logger uses a file lock to allow all the various ssh applications to log to the same file.
|
// for now this package defines a writer for use with log.New(). It it intended to be used from the external ssh applications. This logger uses a file lock to allow all the various ssh applications to log to the same file.
|
||||||
|
// the correct way to do this is to send log events to a daemon
|
||||||
|
// but i'm trying to cut a corner
|
||||||
|
|
||||||
type LockingWriter struct {
|
type LockingWriter struct {
|
||||||
path string
|
path string
|
||||||
|
@ -20,10 +22,10 @@ const (
|
||||||
|
|
||||||
func New() *LockingWriter {
|
func New() *LockingWriter {
|
||||||
f, err := os.OpenFile(fp, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0660)
|
f, err := os.OpenFile(fp, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0660)
|
||||||
if err != nil {
|
if err != nil && !os.IsExist(err) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
f.Close()
|
||||||
|
|
||||||
return &LockingWriter{
|
return &LockingWriter{
|
||||||
path: fp,
|
path: fp,
|
||||||
|
@ -43,7 +45,7 @@ func (l *LockingWriter) Write(p []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var f *os.File
|
var f *os.File
|
||||||
f, err = os.OpenFile(l.path, os.O_APPEND|os.O_WRONLY, 0600)
|
f, err = os.OpenFile(l.path, os.O_APPEND|os.O_WRONLY, 0660)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -51,5 +53,12 @@ func (l *LockingWriter) Write(p []byte) (n int, err error) {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
defer fl.Unlock()
|
defer fl.Unlock()
|
||||||
|
|
||||||
return f.Write(p)
|
n, err = f.Write(p)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
//return f.Write(p)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue