Example #1
0
// Send a closure log message internally
func (log *Logger) intLogc(level int, closure func() string) {
	// Create a vector long enough to not require resizing
	var logto vector.StringVector
	logto.Resize(0, len(log.filterLevels))

	// Determine if any logging will be done
	for filt := range log.filterLevels {
		if level >= log.filterLevels[filt] {
			logto.Push(filt)
		}
	}

	// Only log if a filter requires it
	if len(logto) > 0 {
		// Determine caller func
		pc, _, lineno, ok := runtime.Caller(2)
		src := ""
		if ok {
			src = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), lineno)
		}

		// Make the log record from the closure's return
		rec := newLogRecord(level, src, closure())

		// Dispatch the logs
		for _, filt := range logto {
			log.filterLogWriters[filt].LogWrite(rec)
		}
	}
}
Example #2
0
// Send a log message manually
func (log *Logger) Log(level int, source, message string) {
	// Create a vector long enough to not require resizing
	var logto vector.StringVector
	logto.Resize(0, len(log.filterLevels))

	// Determine if any logging will be done
	for filt := range log.filterLevels {
		if level >= log.filterLevels[filt] {
			logto.Push(filt)
		}
	}

	// Only log if a filter requires it
	if len(logto) > 0 {
		// Make the log record
		rec := newLogRecord(level, source, message)

		// Dispatch the logs
		for _, filt := range logto {
			lw := log.filterLogWriters[filt]
			if lw.Good() {
				lw.LogWrite(rec)
			}
		}
	}
}
Example #3
0
func removeEmptyStrings(arr []string) []string {
	sv := new(vector.StringVector)
	sv.Resize(0, len(arr))
	for _, s := range arr {
		if s != "" {
			sv.Push(s)
		}
	}
	return *sv
}
Example #4
0
// Known format codes:
// %T - Time (15:04:05 MST)
// %t - Time (15:04)
// %D - Date (2006/01/02)
// %d - Date (01/02/06)
// %L - Level (FNST, FINE, DEBG, TRAC, WARN, EROR, CRIT)
// %S - Source
// %M - Message
// Ignores unknown formats
// Recommended: "[%D %T] [%L] (%S) %M"
func FormatLogRecord(format string, rec *LogRecord) string {
	var ovec vector.StringVector

	// Split the string into pieces by % signs
	pieces := strings.Split(format, "%", -1)
	ovec.Resize(0, 2*len(pieces)+2) // allocate enough pieces for each piece and its previous plus an extra for the first and last piece for good measure

	// Iterate over the pieces, replacing known formats
	for i, piece := range pieces {
		if i > 0 && len(piece) > 0 {
			switch piece[0] {
			case 'T':
				ovec.Push(rec.Created.Format("15:04:05 MST"))
			case 't':
				ovec.Push(rec.Created.Format("15:04"))
			case 'D':
				ovec.Push(rec.Created.Format("2006/01/02"))
			case 'd':
				ovec.Push(rec.Created.Format("01/02/06"))
			case 'L':
				ovec.Push(levelStrings[rec.Level])
			case 'S':
				ovec.Push(rec.Source)
			case 'M':
				ovec.Push(rec.Message)
			}
			if len(piece) > 1 {
				ovec.Push(piece[1:])
			}
		} else if len(piece) > 0 {
			ovec.Push(piece)
		}
	}

	return strings.Join(ovec, "") + "\n"
}