summaryrefslogtreecommitdiff
path: root/internal/io/dlog/loggers/factory.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-09-19 13:22:59 +0300
committerPaul Buetow <paul@buetow.org>2021-10-02 12:26:29 +0300
commitfe3e68afd99d8ea246be52893730f987e138ec24 (patch)
tree726e0914730912e0a3b223f7b37facc05ba31140 /internal/io/dlog/loggers/factory.go
parentabeac87aec44249bf67f1b0eca471a31086265ca (diff)
move args to config package
logger package rewrite as dlog
Diffstat (limited to 'internal/io/dlog/loggers/factory.go')
-rw-r--r--internal/io/dlog/loggers/factory.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/internal/io/dlog/loggers/factory.go b/internal/io/dlog/loggers/factory.go
new file mode 100644
index 0000000..3eb29c5
--- /dev/null
+++ b/internal/io/dlog/loggers/factory.go
@@ -0,0 +1,60 @@
+package loggers
+
+import (
+ "fmt"
+ "sync"
+)
+
+type Impl int
+
+const (
+ NONE Impl = iota
+ STDOUT Impl = iota
+ FILE Impl = iota
+ FOUT Impl = iota
+)
+
+var factoryMap map[string]Logger
+var factoryMutex sync.Mutex
+
+func Factory(name string, impl Impl) Logger {
+ factoryMutex.Lock()
+ defer factoryMutex.Unlock()
+
+ id := fmt.Sprintf("name:%s,impl:%v", name, impl)
+
+ if factoryMap == nil {
+ factoryMap = make(map[string]Logger)
+ }
+
+ singleton, ok := factoryMap[id]
+ if !ok {
+ switch impl {
+ case NONE:
+ singleton = none{}
+ case STDOUT:
+ singleton = newStdout()
+ factoryMap[id] = singleton
+ case FILE:
+ singleton = newFile()
+ factoryMap[id] = singleton
+ case FOUT:
+ singleton = newFout()
+ factoryMap[id] = singleton
+ }
+ }
+
+ return singleton
+}
+
+func FactoryRotate() {
+ factoryMutex.Lock()
+ defer factoryMutex.Unlock()
+ if factoryMap == nil {
+ return
+ }
+
+ for _, impl := range factoryMap {
+ impl.Rotate()
+ }
+}