<commit_before>// Copyright 2020 The Hugo Authors. All rights reserved.
//
// 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 loggers

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"regexp"
	"runtime"
	"time"

	"github.com/gohugoio/hugo/common/terminal"

	jww "github.com/spf13/jwalterweatherman"
)

var (
	// Counts ERROR logs to the global jww logger.
	GlobalErrorCounter *jww.Counter
	PanicOnWarning     bool
)

func init() {
	GlobalErrorCounter = &jww.Counter{}
	jww.SetLogListeners(jww.LogCounter(GlobalErrorCounter, jww.LevelError))
}

func LoggerToWriterWithPrefix(logger *log.Logger, prefix string) io.Writer {
	return prefixWriter{
		logger: logger,
		prefix: prefix,
	}
}

type prefixWriter struct {
	logger *log.Logger
	prefix string
}

func (w prefixWriter) Write(p []byte) (n int, err error) {
	w.logger.Printf("%s: %s", w.prefix, p)
	return len(p), nil
}

type Logger interface {
	Printf(format string, v ...interface{})
	Println(v ...interface{})
	PrintTimerIfDelayed(start time.Time, name string)
	Debug() *log.Logger
	Debugf(format string, v ...interface{})
	Debugln(v ...interface{})
	Info() *log.Logger
	Infof(format string, v ...interface{})
	Infoln(v ...interface{})
	Warn() *log.Logger
	Warnf(format string, v ...interface{})
	Warnln(v ...interface{})
	Error() *log.Logger
	Errorf(format string, v ...interface{})
	Errorln(v ...interface{})
	Errors() string

	Out() io.Writer

	Reset()

	// Used in tests.
	LogCounters() *LogCounters
}

type LogCounters struct {
	ErrorCounter *jww.Counter
	WarnCounter  *jww.Counter
}

type logger struct {
	*jww.Notepad

	// The writer that represents stdout.
	// Will be ioutil.Discard when in quiet mode.
	out io.Writer

	logCounters *LogCounters

	// This is only set in server mode.
	errors *bytes.Buffer
}

func (l *logger) Printf(format string, v ...interface{}) {
	l.FEEDBACK.Printf(format, v...)
}

func (l *logger) Println(v ...interface{}) {
	l.FEEDBACK.Println(v...)
}

func (l *logger) Debug() *log.Logger {
	return l.DEBUG
}

func (l *logger) Debugf(format string, v ...interface{}) {
	l.DEBUG.Printf(format, v...)
}

func (l *logger) Debugln(v ...interface{}) {
	l.DEBUG.Println(v...)
}

func (l *logger) Infof(format string, v ...interface{}) {
	l.INFO.Printf(format, v...)
}

func (l *logger) Infoln(v ...interface{}) {
	l.INFO.Println(v...)
}

func (l *logger) Info() *log.Logger {
	return l.INFO
}

const panicOnWarningMessage = "Warning trapped. Remvove the --panicOnWarning flag to continue."

func (l *logger) Warnf(format string, v ...interface{}) {
	l.WARN.Printf(format, v...)
	if PanicOnWarning {
		panic(panicOnWarningMessage)<commit_msg>Fix typo in panicOnWarning message<commit_after>// Copyright 2020 The Hugo Authors. All rights reserved.
//
// 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 loggers

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"regexp"
	"runtime"
	"time"

	"github.com/gohugoio/hugo/common/terminal"

	jww "github.com/spf13/jwalterweatherman"
)

var (
	// Counts ERROR logs to the global jww logger.
	GlobalErrorCounter *jww.Counter
	PanicOnWarning     bool
)

func init() {
	GlobalErrorCounter = &jww.Counter{}
	jww.SetLogListeners(jww.LogCounter(GlobalErrorCounter, jww.LevelError))
}

func LoggerToWriterWithPrefix(logger *log.Logger, prefix string) io.Writer {
	return prefixWriter{
		logger: logger,
		prefix: prefix,
	}
}

type prefixWriter struct {
	logger *log.Logger
	prefix string
}

func (w prefixWriter) Write(p []byte) (n int, err error) {
	w.logger.Printf("%s: %s", w.prefix, p)
	return len(p), nil
}

type Logger interface {
	Printf(format string, v ...interface{})
	Println(v ...interface{})
	PrintTimerIfDelayed(start time.Time, name string)
	Debug() *log.Logger
	Debugf(format string, v ...interface{})
	Debugln(v ...interface{})
	Info() *log.Logger
	Infof(format string, v ...interface{})
	Infoln(v ...interface{})
	Warn() *log.Logger
	Warnf(format string, v ...interface{})
	Warnln(v ...interface{})
	Error() *log.Logger
	Errorf(format string, v ...interface{})
	Errorln(v ...interface{})
	Errors() string

	Out() io.Writer

	Reset()

	// Used in tests.
	LogCounters() *LogCounters
}

type LogCounters struct {
	ErrorCounter *jww.Counter
	WarnCounter  *jww.Counter
}

type logger struct {
	*jww.Notepad

	// The writer that represents stdout.
	// Will be ioutil.Discard when in quiet mode.
	out io.Writer

	logCounters *LogCounters

	// This is only set in server mode.
	errors *bytes.Buffer
}

func (l *logger) Printf(format string, v ...interface{}) {
	l.FEEDBACK.Printf(format, v...)
}

func (l *logger) Println(v ...interface{}) {
	l.FEEDBACK.Println(v...)
}

func (l *logger) Debug() *log.Logger {
	return l.DEBUG
}

func (l *logger) Debugf(format string, v ...interface{}) {
	l.DEBUG.Printf(format, v...)
}

func (l *logger) Debugln(v ...interface{}) {
	l.DEBUG.Println(v...)
}

func (l *logger) Infof(format string, v ...interface{}) {
	l.INFO.Printf(format, v...)
}

func (l *logger) Infoln(v ...interface{}) {
	l.INFO.Println(v...)
}

func (l *logger) Info() *log.Logger {
	return l.INFO
}

const panicOnWarningMessage = "Warning trapped. Remove the --panicOnWarning flag to continue."

func (l *logger) Warnf(format string, v ...interface{}) {
	l.WARN.Printf(format, v...)
	if PanicOnWarning {
		panic(panicOnWarningMessage)
{'fraction_non_alphanumeric': 0.1454429977770721, 'fraction_numerical': 0.0031756113051762463, 'mean_word_length': 3.7467972871137905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6927083', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2134, 'n_words': 659}