aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nsa.go10
1 files changed, 10 insertions, 0 deletions
diff --git a/nsa.go b/nsa.go
index 827b4e6..0ef6822 100644
--- a/nsa.go
+++ b/nsa.go
@@ -3,6 +3,7 @@ package nsa
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 "encoding/json" 5 "encoding/json"
6 "errors"
6 "fmt" 7 "fmt"
7 "os" 8 "os"
8 "strings" 9 "strings"
@@ -40,6 +41,9 @@ func (l *Logger) getJSON(data interface{}) string {
40} 41}
41 42
42func (l *Logger) insertEvent(id int, data string) { 43func (l *Logger) insertEvent(id int, data string) {
44 if database == nil {
45 return
46 }
43 _, err := database.Exec( 47 _, err := database.Exec(
44 "INSERT INTO nsa.events (type, data, client_ip) VALUES ($1, $2, $3);", 48 "INSERT INTO nsa.events (type, data, client_ip) VALUES ($1, $2, $3);",
45 id, data, l.ip) 49 id, data, l.ip)
@@ -49,6 +53,9 @@ func (l *Logger) insertEvent(id int, data string) {
49} 53}
50 54
51func (l *Logger) Log(event string, data interface{}) { 55func (l *Logger) Log(event string, data interface{}) {
56 if database == nil {
57 return
58 }
52 go func() { 59 go func() {
53 id := l.getEventID(event) 60 id := l.getEventID(event)
54 if id == 0 { 61 if id == 0 {
@@ -79,6 +86,9 @@ func Init(db *sql.DB) error {
79} 86}
80 87
81func SetEvents(events []string) error { 88func SetEvents(events []string) error {
89 if database == nil {
90 return errors.New("not initialized")
91 }
82 rows, err := database.Query("SELECT id, name FROM nsa.event_types;") 92 rows, err := database.Query("SELECT id, name FROM nsa.event_types;")
83 if err != nil { 93 if err != nil {
84 return err 94 return err