<commit_before>// Copyright 2017 phcurtis fn Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fn

import (
	"errors"
	"fmt"
	"log"
	"path/filepath"
	"runtime"
	"strings"
	"time"
)

func strMinWidth(str string, width int) string {
	len1 := len(str)
	if len1 < width {
		str = str + strings.Repeat(" ", width-len1)
	}
	return str
}

// fn log trace but may first need to find appropriate filename and line num
func helplt(lvl int, msg, reffile, reflnum string) (newReffile, newReflnum string) {
	var filenlr string

	// get original [current] log flags
	orgflags := logt.Flags()
	sl := log.Lshortfile | log.Llongfile
	lfn := orgflags & sl

	var ref string

	// if log flags are including filename
	if lfn > 0 {
		_, file, line, _ := runtime.Caller(lvl)
		linenum := fmt.Sprintf(":%d", line)
		if orgflags&log.Lshortfile > 0 {
			file = filepath.Base(file)
		} else {
			// log.Llongfile
			if logTraceFlags&Trfilenogps > 0 {
				if strings.HasPrefix(file, gopathsrc) {
					file = file[len(gopathsrc):]
				}
			}
		}
		newReffile = filepath.Base(file)
		newReflnum = linenum

		// seems if this is true there is a problem elsewhere
		// as in a weird invocation end portion of log trace,
		// possible by passing that portion and invoking in
		// another function which package fn does NOT support.
		if reffile != "" && reffile != newReffile {
			log.Panic(errors.New("reffile:" + reffile + " != newReffile:" + newReffile))
		}

		if reffile != "" && logTraceFlags&Trfnobegref == 0 {
			if logTraceFlags&Trfbegrefincfile > 0 {
				ref = "<" + reffile + reflnum + ">"
			} else {
				ref = "<" + reflnum + ">"
			}
		}

		filenlr = file + linenum + ref + " "
		filenlr = strMinWidth(filenlr, logAlignFile)

		// set log flags not to include filename
		logt.SetFlags(orgflags &^ sl)
	}
	logt.Printf("%s%s", filenlr, msg)
	if lfn > 0 {
		// restore log flags
		logt.SetFlags(orgflags)
	}
	return newReffile, newReflnum
}

func formatTime(t time.Time, microseconds bool, msg string) string {
	yr, mon, dy := t.Date()
	hr, min, sec := t.Clock()
	var micro string
	if microseconds {
		micro = fmt.Sprintf(".%06d", t.Nanosecond()/1e3)
	}
	return fmt.Sprintf("%s%d/%02d/%02d %02d:%02d:%02d%s", msg, yr, mon, dy, hr, min, sec, micro)
}

func helpltend(lvladj int, trlabel string, start time.Time, begFn, reffile, reflnum, endMsg string) {
	endTime := time.Now()
	endFn := Lvl(Lgpar + lvladj)
	if begFn != endFn {
		if strings.Contains(CStk(), "<--runtime.gopanic") {
			log.Println("\nGOPANIC DETECTED --exiting '"+trlabel+"'(helpltend)>CStk:", CStk())
			log.Println("begFn:"+begFn+" != endFn:"+endFn, " reffile:", reffile, " reflnum", reflnum, "\n\n ")
			return
		}
		// if Idiomatic usage of LogTrace and LogTraceMsgs then should not have a panic.
		err := fmt.Sprintf("begFn != endFn\n begFn:%s\n endFn:%s\n  Cstk:%s \n"+
			"Panic probable cause due to end trace pairing return portion called from different func",
			begFn, endFn, CStk())
		log.Panic(errors.New(err))
	}
	if endMsg != "" {
		endMsg = " " + endMsg
	}
	var str string

	muLogt.Lock()
	defer muLogt.Unlock()

	if logTraceFlags&Trfnbase > 0 {
		str = filepath.Base(endFn)
	} else {
		str = endFn
	}

	str = strMinWidth(str, logAlignFunc) + endMsg
<commit_msg>removed linefeed on log.Panic<commit_after>// Copyright 2017 phcurtis fn Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fn

import (
	"errors"
	"fmt"
	"log"
	"path/filepath"
	"runtime"
	"strings"
	"time"
)

func strMinWidth(str string, width int) string {
	len1 := len(str)
	if len1 < width {
		str = str + strings.Repeat(" ", width-len1)
	}
	return str
}

// fn log trace but may first need to find appropriate filename and line num
func helplt(lvl int, msg, reffile, reflnum string) (newReffile, newReflnum string) {
	var filenlr string

	// get original [current] log flags
	orgflags := logt.Flags()
	sl := log.Lshortfile | log.Llongfile
	lfn := orgflags & sl

	var ref string

	// if log flags are including filename
	if lfn > 0 {
		_, file, line, _ := runtime.Caller(lvl)
		linenum := fmt.Sprintf(":%d", line)
		if orgflags&log.Lshortfile > 0 {
			file = filepath.Base(file)
		} else {
			// log.Llongfile
			if logTraceFlags&Trfilenogps > 0 {
				if strings.HasPrefix(file, gopathsrc) {
					file = file[len(gopathsrc):]
				}
			}
		}
		newReffile = filepath.Base(file)
		newReflnum = linenum

		// seems if this is true there is a problem elsewhere
		// as in a weird invocation end portion of log trace,
		// possible by passing that portion and invoking in
		// another function which package fn does NOT support.
		if reffile != "" && reffile != newReffile {
			log.Panic(errors.New("reffile:" + reffile + " != newReffile:" + newReffile))
		}

		if reffile != "" && logTraceFlags&Trfnobegref == 0 {
			if logTraceFlags&Trfbegrefincfile > 0 {
				ref = "<" + reffile + reflnum + ">"
			} else {
				ref = "<" + reflnum + ">"
			}
		}

		filenlr = file + linenum + ref + " "
		filenlr = strMinWidth(filenlr, logAlignFile)

		// set log flags not to include filename
		logt.SetFlags(orgflags &^ sl)
	}
	logt.Printf("%s%s", filenlr, msg)
	if lfn > 0 {
		// restore log flags
		logt.SetFlags(orgflags)
	}
	return newReffile, newReflnum
}

func formatTime(t time.Time, microseconds bool, msg string) string {
	yr, mon, dy := t.Date()
	hr, min, sec := t.Clock()
	var micro string
	if microseconds {
		micro = fmt.Sprintf(".%06d", t.Nanosecond()/1e3)
	}
	return fmt.Sprintf("%s%d/%02d/%02d %02d:%02d:%02d%s", msg, yr, mon, dy, hr, min, sec, micro)
}

func helpltend(lvladj int, trlabel string, start time.Time, begFn, reffile, reflnum, endMsg string) {
	endTime := time.Now()
	endFn := Lvl(Lgpar + lvladj)
	if begFn != endFn {
		if strings.Contains(CStk(), "<--runtime.gopanic") {
			log.Println("GOPANIC DETECTED --exiting '"+trlabel+"'(helpltend)>CStk:", CStk())
			log.Println("begFn:"+begFn+" != endFn:"+endFn, " reffile:", reffile, " reflnum", reflnum, "\n\n ")
			return
		}
		// if Idiomatic usage of LogTrace and LogTraceMsgs then should not have a panic.
		err := fmt.Sprintf("begFn != endFn\n begFn:%s\n endFn:%s\n  Cstk:%s \n"+
			"Panic probable cause due to end trace pairing return portion called from different func",
			begFn, endFn, CStk())
		log.Panic(errors.New(err))
	}
	if endMsg != "" {
		endMsg = " " + endMsg
	}
	var str string

	muLogt.Lock()
	defer muLogt.Unlock()

	if logTraceFlags&Trfnbase > 0 {
		str = filepath.Base(endFn)
	} else {
		str = endFn
	}

	str = strMinWidth(str, logAlignFunc) + endMsg

{'fraction_non_alphanumeric': 0.13911472448057813, 'fraction_numerical': 0.008431195423065342, 'mean_word_length': 3.411022576361222, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2506656', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2475, 'n_words': 701}