Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from JesseCoretta/v1.0.3
Browse files Browse the repository at this point in the history
v1.0.3
  • Loading branch information
JesseCoretta committed Mar 31, 2024
2 parents 0227eae + f0e2280 commit 88b62cd
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 1,043 deletions.
16 changes: 7 additions & 9 deletions cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,10 @@ func (r *nodeConfig) kind() (kind string) {
valid returns an error if the receiver is considered to be
invalid or nil.
*/
func (r *nodeConfig) valid() (err error) {
err = errorf("%T instance is nil; aborting", r)
func (r *nodeConfig) valid() (is bool) {
if !r.isZero() {
err = errorf("%T instance defines no stack \"kind\", or %T is invalid", r, r)
if r.typ != 0x0 {
err = nil
is = true
}
}

Expand All @@ -278,7 +276,7 @@ positive returns a Boolean value indicative of whether the specified
cfgFlag input value is "on" within the receiver's opt field.
*/
func (r nodeConfig) positive(x cfgFlag) (is bool) {
if err := r.valid(); err == nil {
if r.valid() {
is = r.opt.positive(x)
}
return
Expand All @@ -297,7 +295,7 @@ setOpt sets the specified cfgFlag to "on" within the receiver's
opt field.
*/
func (r *nodeConfig) setOpt(x cfgFlag) (err error) {
if err = r.valid(); err == nil {
if r.valid() {
r.opt.shift(x)
}
return
Expand Down Expand Up @@ -437,7 +435,7 @@ setMutex enables the receiver's mutual exclusion
locking capabilities.
*/
func (r *nodeConfig) setMutex() {
if err := r.valid(); err == nil {
if r.valid() {
if r.mtx == nil {
r.mtx = &sync.Mutex{}
}
Expand All @@ -449,7 +447,7 @@ unsetOpt sets the specified cfgFlag to "off" within the receiver's
opt field.
*/
func (r *nodeConfig) unsetOpt(x cfgFlag) (err error) {
if err = r.valid(); err == nil {
if r.valid() {
r.opt.unshift(x)
}
return
Expand All @@ -469,7 +467,7 @@ to its current value in terms of the input value. In other words, if the
state is "on" for a flag, it will be toggled to "off" and vice versa.
*/
func (r *nodeConfig) toggleOpt(x cfgFlag) (err error) {
if err = r.valid(); err == nil {
if r.valid() {
r.opt.toggle(x)
}
return
Expand Down
193 changes: 10 additions & 183 deletions cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,13 @@ of *condition, which is the embedded type instance found within
(properly initialized) Condition instances.
*/
func initCondition() (r *condition) {
var data map[string]string = make(map[string]string, 0)

r = new(condition)
r.cfg = new(nodeConfig)
r.cfg.log = newLogSystem(cLogDefault)
r.cfg.log.lvl = logLevels(NoLogLevels)

r.cfg.typ = cond
data[`type`] = cond.String()

if !logDiscard(r.cfg.log.log) {
data[`laddr`] = sprintf("%p", r.cfg.log.log)
data[`lpfx`] = r.cfg.log.log.Prefix()
data[`lflags`] = sprintf("%d", r.cfg.log.log.Flags())
data[`lwriter`] = sprintf("%T", r.cfg.log.log.Writer())
}

r.debug(`ALLOC`, data)

return
}
Expand All @@ -127,31 +116,18 @@ func (r Condition) SetAuxiliary(aux ...Auxiliary) Condition {
setAuxiliary is a private method called by Condition.SetAuxiliary.
*/
func (r *condition) setAuxiliary(aux ...Auxiliary) {
fname := fmname()
r.calls(sprintf("%s: in: variadic %T(len:%d)",
fname, aux, len(aux)))

var _aux Auxiliary
if len(aux) == 0 {
r.trace(sprintf("%s: ALLOC %T (no variadic input)",
fname, _aux))
_aux = make(Auxiliary, 0)
} else {
if aux[0] == nil {
r.trace(sprintf("%s: ALLOC %T (nil variadic slice)",
fname, _aux))
_aux = make(Auxiliary, 0)
} else {
r.trace(sprintf("%s: assign user %T(len:%d)",
fname, _aux, _aux.Len()))
_aux = aux[0]
}
}

r.cfg.aux = _aux
r.debug(sprintf("%s: registered %T(len:%d)",
fname, r.cfg.aux, r.cfg.aux.Len()))
r.calls(sprintf("%s: out:void", fname))
}

/*
Expand All @@ -168,14 +144,7 @@ func (r Condition) Auxiliary() (aux Auxiliary) {
auxiliary is a private method called by Condition.Auxiliary.
*/
func (r condition) auxiliary() (aux Auxiliary) {
fname := fmname()
r.calls(sprintf("%s: in:niladic", fname))
aux = r.cfg.aux
r.debug(sprintf("%s: get %T(len:%d)",
fname, aux, aux.Len()))
r.calls(sprintf("%s: out:%T(%d)",
fname, aux, aux.Len()))

return
}

Expand Down Expand Up @@ -255,13 +224,7 @@ func (r Condition) SetLogLevel(l ...any) Condition {
}

func (r *condition) setLogLevel(l ...any) {
r.calls(sprintf("%s: in:%T(%v;len:%d)",
fmname(), l, l, len(l)))

r.cfg.log.shift(l...)

r.calls(sprintf("%s: out:%T(self)",
fmname(), r))
}

/*
Expand Down Expand Up @@ -292,14 +255,7 @@ func (r Condition) UnsetLogLevel(l ...any) Condition {
}

func (r *condition) unsetLogLevel(l ...any) {
fname := fmname()
r.calls(sprintf("%s: in:%T(%v;len:%d)",
fname, l, l, len(l)))

r.cfg.log.unshift(l...)

r.calls(sprintf("%s: out:%T(self)",
fname, r))
}

/*
Expand Down Expand Up @@ -511,7 +467,7 @@ func (r Condition) SetID(id string) Condition {
if lc(id) == `_random` {
id = randomID(randIDSize)
} else if lc(id) == `_addr` {
id = sprintf("%s", r.Addr())
id = r.Addr()
}

r.condition.cfg.setID(id)
Expand Down Expand Up @@ -554,6 +510,8 @@ func (r Condition) Len() int {
Addr returns the string representation of the pointer
address for the receiver. This may be useful for logging
or debugging operations.
Note: this method calls fmt.Sprintf.
*/
func (r Condition) Addr() (addr string) {
if r.IsInit() {
Expand Down Expand Up @@ -615,7 +573,7 @@ are checked.
*/
func (r Condition) Valid() (err error) {
if !r.IsInit() {
err = errorf("%T instance is nil", r)
err = errorf("condition instance is nil")
return
}

Expand All @@ -630,23 +588,23 @@ func (r Condition) Valid() (err error) {

// verify keyword
if kw := r.Keyword(); len(kw) == 0 {
err = errorf("%T keyword value is zero", r)
err = errorf("keyword value is zero")
return
}

// verify comparison operator
if cop := r.Operator(); cop != nil {
if assert, ok := cop.(ComparisonOperator); ok {
if !(1 <= int(assert) && int(assert) <= 6) {
err = errorf("%T operator value is bogus", r)
err = errorf("operator value is bogus")
return
}
}
}

// verify expression value
if r.Expression() == nil {
err = errorf("%T expression value is nil", r)
err = errorf("expression value is nil")
}

return
Expand All @@ -663,7 +621,7 @@ execution of this method, the return value shall always be false.
*/
func (r Condition) Evaluate(x ...any) (ev any, err error) {
if r.IsInit() {
if err = errorf("No %T.%T func/meth found", r, r.cfg.evl); r.cfg.evl != nil {
if err = errorf("No func/meth found"); r.cfg.evl != nil {
ev, err = r.cfg.evl(x...)
}
}
Expand Down Expand Up @@ -825,16 +783,10 @@ isFIFO is a private method called by the Condition.IsFIFO method.
*/
func (r condition) isFIFO() (result bool) {

fname := fmname()
r.calls(sprintf("%s: in:niladic", fname))

if stk, ok := stackTypeAliasConverter(r.ex); ok {
result = stk.IsFIFO()
}

r.calls(sprintf("%s: out:%T(%v)",
fmname(), result, result))

return
}

Expand Down Expand Up @@ -1007,137 +959,12 @@ func (r condition) string() string {
pad = ``
}

s := sprintf("%s%s%s%s%s", r.kw, pad, r.op, pad, val)
s := r.kw + pad + r.op.String() + pad + val
if r.cfg.positive(parens) {
s = sprintf("(%s%s%s)", pad, s, pad)
s = `(` + pad + s + pad + `)`
}

return s
}

/*
mkmsg is the private method called by eventDispatch for the
purpose of Message assembly prior to submission to a logger.
*/
func (r *condition) mkmsg(typ string) (m Message, ok bool) {
if r.isInit() {
if len(typ) > 0 {
m = Message{
ID: getLogID(r.cfg.id),
Tag: typ,
Addr: ptrString(r),
Type: sprintf("%T", *r),
Time: timestamp(),
Len: -1, // initially N/A
Cap: -1, // initially N/A
}
ok = true
}
}

return
}

/*
error conditions that are fatal and always serious
*/
func (r *condition) fatal(x any, data ...map[string]string) {
if r != nil && x != nil {
r.eventDispatch(x, LogLevel5, `FATAL`, data...)
}
}

/*
error conditions that are not fatal but potentially serious
*/
func (r *condition) error(x any, data ...map[string]string) {
if r != nil && x != nil {
r.eventDispatch(x, LogLevel5, `ERROR`, data...)
}
}

/*
extreme depth operational details
*/
func (r *condition) trace(x any, data ...map[string]string) {
if r != nil && x != nil {
r.eventDispatch(x, LogLevel6, `TRACE`, data...)
}
}

/*
relatively deep operational details
*/
func (r *condition) debug(x any, data ...map[string]string) {
if r != nil && x != nil {
r.eventDispatch(x, LogLevel4, `DEBUG`, data...)
}
}

/*
policy method operational details, as well as caps, r/o, etc.
*/
func (r *condition) policy(x any, data ...map[string]string) {
if r != nil && x != nil {
r.eventDispatch(x, LogLevel2, `POLICY`, data...)
}
}

/*
calls records in/out signatures and realtime meta-data regarding
individual method runtimes.
*/
func (r *condition) calls(x any, data ...map[string]string) {
if r != nil && x != nil {
r.eventDispatch(x, LogLevel1, `CALL`, data...)
}
}

/*
state records interrogations of, and changes to, the underlying
configuration value.
*/
func (r *condition) state(x any, data ...map[string]string) {
if r != nil && x != nil {
r.eventDispatch(x, LogLevel3, `STATE`, data...)
}
}

/*
eventDispatch is the main dispatcher of events of any severity.
A severity of FATAL (in any case) will result in a logger-driven
call of os.Exit.
*/
func (r condition) eventDispatch(x any, ll LogLevel, severity string, data ...map[string]string) {
if !(r.cfg.log.positive(ll) ||
eq(severity, `FATAL`) ||
r.cfg.log.lvl == logLevels(AllLogLevels)) {
return
}

printers := map[string]func(...any){
`FATAL`: r.logger().Fatalln,
`STATE`: r.logger().Println,
`ERROR`: r.logger().Println,
`CALL`: r.logger().Println,
`DEBUG`: r.logger().Println,
`TRACE`: r.logger().Println,
`POLICY`: r.logger().Println,
}

if m, ok := r.mkmsg(severity); ok {
if ok = m.setText(x); ok {
if len(data) > 0 {
if data[0] != nil {
m.Data = data[0]
if _, ok := data[0][`FATAL`]; ok {
severity = `ERROR`
}
}
}
printers[severity](m)
}
}
}

const badCond = `<invalid_condition>`

0 comments on commit 88b62cd

Please sign in to comment.