You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.3 KiB
116 lines
2.3 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name filters.go
|
|
// Created by DEV
|
|
// Modified 24/04/2024
|
|
//
|
|
// Copyright 2024 activething.com
|
|
// ------------------------------------------------------------------------
|
|
// 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.
|
|
// ------------------------------------------------------------------------
|
|
|
|
package logs
|
|
|
|
|
|
type (
|
|
|
|
Filters struct {
|
|
allows []*Filter
|
|
denies []*Filter
|
|
}
|
|
|
|
|
|
FiltersFnc func(f *Filters)
|
|
)
|
|
|
|
|
|
func NewFilters (options ...FiltersFnc) *Filters {
|
|
f := &Filters {
|
|
}
|
|
for _,o := range options {
|
|
o(f)
|
|
}
|
|
return f
|
|
}
|
|
|
|
func (f *Filters) AddFilterAllowed(filter *Filter) {
|
|
f.add(filter,&f.allows) }
|
|
|
|
func (f *Filters) AddFilterDenied (filter *Filter) {
|
|
f.add(filter,&f.denies) }
|
|
|
|
func (f *Filters) DelFilterAllowed(name string) {
|
|
f.del(name,&f.allows) }
|
|
|
|
func (f *Filters) DelFilterDenied (name string) {
|
|
f.del(name,&f.denies) }
|
|
|
|
|
|
|
|
func (f *Filters) Filter (entry *Entry) bool {
|
|
|
|
if len(f.allows) > 0 {
|
|
p := false
|
|
for _,fi := range f.allows {
|
|
if fi.IsActive() {
|
|
if p = fi.Filter(entry); p {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if !p {
|
|
return false
|
|
}
|
|
}
|
|
|
|
for _,fi := range f.denies {
|
|
if fi.IsActive() {
|
|
if fi.Filter(entry) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
|
|
|
|
func (f *Filters) add (ft *Filter, ls *[]*Filter) {
|
|
if p := filterPos(*ls, ft.name); p != -1 {
|
|
(*ls)[p]= ft
|
|
return
|
|
}
|
|
*ls = append(*ls, ft)
|
|
}
|
|
|
|
func (f *Filters) del (name string, ls *[]*Filter) {
|
|
if p := filterPos(*ls,name); p != -1 {
|
|
*ls = append((*ls)[:p],(*ls)[p+1:]...)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func filterPos (list []*Filter, name string) int {
|
|
for i,f := range list {
|
|
if f.name == name {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
} |