<commit_before>//
// Ebase frame for daemon program
// Author <NAME>
// Date 2013-07-05
// Copyright (c) 2013 ForEase Times Technology Co., Ltd. All rights reserved.
//
package ebase

import (
	"flag"
	"fmt"
	"github.com/forease/config"
	"os"
	"os/signal"
	"path"
	"reflect"
	"runtime"
	"syscall"
)

var (
	// 日志
	Log        *BaseLog
	Config     *config.Config
	SigHandler = make(map[string]interface{}) //
	G          = make(map[string]interface{}) //

	// 定义命令行参数
	verbose = flag.Bool("v", false, "Verbose output")
	help    = flag.Bool("h", false, "Show this help")
	chroot  = flag.Bool("w", false, "Setup enable chroot")
	cfgfile = flag.String("c", "", "Config file")
	workdir = flag.String("d", "", "Setup work dir")
	pidfile = flag.String("p", "", "Pid file")
)

func init() {
	flag.Parse()
	if *help {
		Help()
		return
	}

	if *verbose {
		fmt.Println("version:")
		os.Exit(0)
	}

	if *workdir != "" {
		fmt.Println("workdir: ", *workdir, os.Args)
		if err := syscall.Chdir(*workdir); err != nil {
			fmt.Printf("Can't change to work dir [%s]: %s\n", *workdir, err)
			os.Exit(1)
		}

		if *chroot {
			pwd, _ := os.Getwd()
			if err := syscall.Chroot(pwd); err != nil {
				fmt.Printf("Can't Chroot to %s: %s\n", *workdir, err)
				os.Exit(1)
			}
			fmt.Printf("I'll Chroot to %s !\n", pwd)
		}
	}

	runtime.GOMAXPROCS(runtime.NumCPU())

	Config = LoadConfig("")
	CreatePid()
	Log = defaultLog()

	SigHandler["sighup"] = func() {
		Log.Debug("reload config")
		Config = LoadConfig("")
	}

	//
	if ok, _ := Config.Bool("sys.signal", false); ok {
		go SignalHandle(SigHandler)
	}
}

func LoadConfig(configFile string) (cfg *config.Config) {
	var err error
	if configFile == "" {
		if *cfgfile != "" {
			configFile = *cfgfile
		} else {
			configFile = "/opt/etc/" + path.Base(os.Args[0]) + ".conf"
		}
	}
	if configFile == "" {
		return
	}

	cfg, err = config.NewConfig(configFile, 5)
	if err != nil {
		fmt.Println("read config file error: ", err)
		os.Exit(1)
	}

	return cfg
}

func defaultLog() (l *BaseLog) {

	logType, _ := Config.String("log.type", "consloe")
	logFile, _ := Config.String("log.file", "")
	logLevel, _ := Config.Int("log.level", 5)
	logFlag, _ := Config.Int("log.flag", 9)
	//logEnable, _ = Config.Bool("log.enable", true)
	opt := &LogOptions{Type: logType, File: logFile, Level: logLevel, Flag: logFlag}
	return NewLog(opt)
}

//
func SignalHandle(funcs map[string]interface{}) {
	ch := make(chan os.Signal)<commit_msg>change toml load level to 16<commit_after>//
// Ebase frame for daemon program
// Author <NAME>
// Date 2013-07-05
// Copyright (c) 2013 ForEase Times Technology Co., Ltd. All rights reserved.
//
package ebase

import (
	"flag"
	"fmt"
	"github.com/forease/config"
	"os"
	"os/signal"
	"path"
	"reflect"
	"runtime"
	"syscall"
)

var (
	// 日志
	Log        *BaseLog
	Config     *config.Config
	SigHandler = make(map[string]interface{}) //
	G          = make(map[string]interface{}) //

	// 定义命令行参数
	verbose = flag.Bool("v", false, "Verbose output")
	help    = flag.Bool("h", false, "Show this help")
	chroot  = flag.Bool("w", false, "Setup enable chroot")
	cfgfile = flag.String("c", "", "Config file")
	workdir = flag.String("d", "", "Setup work dir")
	pidfile = flag.String("p", "", "Pid file")
)

func init() {
	flag.Parse()
	if *help {
		Help()
		return
	}

	if *verbose {
		fmt.Println("version:")
		os.Exit(0)
	}

	if *workdir != "" {
		fmt.Println("workdir: ", *workdir, os.Args)
		if err := syscall.Chdir(*workdir); err != nil {
			fmt.Printf("Can't change to work dir [%s]: %s\n", *workdir, err)
			os.Exit(1)
		}

		if *chroot {
			pwd, _ := os.Getwd()
			if err := syscall.Chroot(pwd); err != nil {
				fmt.Printf("Can't Chroot to %s: %s\n", *workdir, err)
				os.Exit(1)
			}
			fmt.Printf("I'll Chroot to %s !\n", pwd)
		}
	}

	runtime.GOMAXPROCS(runtime.NumCPU())

	Config = LoadConfig("")
	CreatePid()
	Log = defaultLog()

	SigHandler["sighup"] = func() {
		Log.Debug("reload config")
		Config = LoadConfig("")
	}

	//
	if ok, _ := Config.Bool("sys.signal", false); ok {
		go SignalHandle(SigHandler)
	}
}

func LoadConfig(configFile string) (cfg *config.Config) {
	var err error
	if configFile == "" {
		if *cfgfile != "" {
			configFile = *cfgfile
		} else {
			configFile = "/opt/etc/" + path.Base(os.Args[0]) + ".conf"
		}
	}
	if configFile == "" {
		return
	}

	cfg, err = config.NewConfig(configFile, 16)
	if err != nil {
		fmt.Println("read config file error: ", err)
		os.Exit(1)
	}

	return cfg
}

func defaultLog() (l *BaseLog) {

	logType, _ := Config.String("log.type", "consloe")
	logFile, _ := Config.String("log.file", "")
	logLevel, _ := Config.Int("log.level", 5)
	logFlag, _ := Config.Int("log.flag", 9)
	//logEnable, _ = Config.Bool("log.enable", true)
	opt := &LogOptions{Type: logType, File: logFile, Level: logLevel, Flag: logFlag}
	return NewLog(opt)
}

//
func SignalHandle(funcs map[string]interface{}) {
	ch := make(chan os.Signal)
{'fraction_non_alphanumeric': 0.18991459943066288, 'fraction_numerical': 0.008743391622610817, 'mean_word_length': 3.330105633802817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2901399', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1919, 'n_words': 442}