func TestNull(t *testing.T) { log.SetNull() log.SetLevel(-1000) if log.IsTrace() { t.Error("There should be no trace") } if log.IsDebug() { t.Error("There should be no debug") } if log.IsInfo() { t.Error("There should be no info") } if log.IsWarn() { t.Error("There should be no warn") } var args []interface{} args = append(args, "key1", 1, "key2", 3.14152) log.Trace("Hello World", args...) log.Debug("Hello World", args...) log.Info("Hello World", args...) log.Warn("Hello World", args...) log.Error("Hello World", args...) log.Log(1, "Hello World", args) }
// String for money type representation in a specific locale. func (c Currency) String() string { var bufC buf if _, err := c.LocalizeWriter(&bufC); err != nil { if log.IsTrace() { log.Trace("Currency=String", "err", err, "c", c) } log.Error("Currency=String", "err", err, "c", c) } return string(bufC) }
// Sub subtracts one Currency type from another. Returns empty Currency on integer overflow. // Errors will be logged and a trace is available when the level for tracing has been set. func (c Currency) Sub(d Currency) Currency { r := c.m - d.m if (r^c.m)&^(r^d.m) < 0 { if log.IsTrace() { log.Trace("Currency=Sub", "err", ErrOverflow, "m", c, "n", d) } log.Error("Currency=Sub", "err", ErrOverflow, "m", c, "n", d) return New() } c.m = r return c }
// ParseFloat transforms a string float value into a real float64 value and // sets it. Current value will be overridden. Returns a logged error. func (c *Currency) ParseFloat(s string) error { f, err := strconv.ParseFloat(s, 64) if err != nil { if log.IsTrace() { log.Trace("Currency=ParseFloat", "err", err, "arg", s, "currency", c) } return log.Error("Currency=ParseFloat", "err", err, "arg", s) } c.Valid = true *c = c.Setf(f) return nil }
// Add adds two Currency types. Returns empty Currency on integer overflow. // Errors will be logged and a trace is available when the level for tracing has been set. func (c Currency) Add(d Currency) Currency { r := c.m + d.m if (r^c.m)&(r^d.m) < 0 { if log.IsTrace() { log.Trace("Currency=Add", "err", ErrOverflow, "m", c, "n", d) } log.Error("Currency=Add", "err", ErrOverflow, "m", c, "n", d) return New() } c.m = r c.Valid = true return c }
// SetCurrencyISO parses a 3-letter ISO 4217 code and sets it to the Currency // struct. If parsing fails errors will be logged and falls back to DefaultCurrencyName. // Calling this function sets also the CurrencySign() to the at the moment // 3-letter ISO code. (Missing feature in text/language package) // This function is called in NewCurrency(). func SetCurrencyISO(cur string) CurrencyOptions { return func(c *Currency) CurrencyOptions { previous := c.ISO.String() lc, err := language.ParseCurrency(cur) if err != nil { if log.IsTrace() { log.Trace("i18n.CurrencyISO.ParseCurrency.error", "err", err, "cur", cur) } log.Error("i18n.CurrencyISO.ParseCurrency", "err", err, "cur", cur) lc = language.MustParseCurrency(DefaultCurrencyName) } c.ISO = lc SetCurrencySign([]byte(lc.String()))(c) return SetCurrencyISO(previous) } }