Ejemplo n.º 1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	log.Global = log.NewDefaultLogger(log.DEBUG)
	flag.Parse()
	defer log.Close()
	begin, err := strconv.Atoi(os.Args[1])
	if err != nil {
		panic(err)
	}

	num, err := strconv.Atoi(os.Args[2])
	if err != nil {
		panic(err)
	}

	//time.AfterFunc(time.Duration(t)*time.Second, stop)
	go result()

	for i := begin; i < begin+num; i++ {
		key := fmt.Sprintf("%d", i)
		go startClient(key)
	}

	var exit chan bool
	<-exit
}
Ejemplo n.º 2
0
Archivo: main.go Proyecto: dulumao/goim
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	log.Global = log.NewDefaultLogger(log.DEBUG)
	flag.Parse()
	defer log.Close()
	begin, err := strconv.Atoi(os.Args[1])
	if err != nil {
		panic(err)
	}

	num, err := strconv.Atoi(os.Args[2])
	if err != nil {
		panic(err)
	}

	go result()

	for i := begin; i < begin+num; i++ {
		key := fmt.Sprintf("%d", i)
		quit := make(chan bool, 1)
		go startClient(key, quit)
	}

	var exit chan bool
	<-exit
}
Ejemplo n.º 3
0
Archivo: log.go Proyecto: wxjs33/kfkcp
func GetLogger(path string, level string) *Log {
	var log Log

	if path == "" {
		path = DEFAULT_LOG_PATH
	}

	lv := log4go.ERROR
	switch level {
	case "debug":
		lv = log4go.DEBUG
	case "error":
		lv = log4go.ERROR
	case "info":
		lv = log4go.INFO
	}

	l := log4go.NewDefaultLogger(lv)
	//l.AddFilter("log", lv, log4go.NewFileLogWriter(path, true))
	l.AddFilter("log", lv, log4go.NewFileLogWriter(path, false))

	log.l = l

	return &log
}
Ejemplo n.º 4
0
func NewServer() *Server {
	return &Server{
		Config: Config,
		Logger: l4g.NewDefaultLogger(l4g.INFO),
		Env:    map[string]interface{}{},
	}
}
Ejemplo n.º 5
0
func (s *Server) initServer() {
	if s.Config == nil {
		s.Config = &ServerConfig{}
	}

	if s.Logger == nil {
		s.Logger = l4g.NewDefaultLogger(l4g.INFO)
	}
}
Ejemplo n.º 6
0
func initLogger() log4go.Logger {
	loglevel := log4go.INFO
	if *logdebug {
		loglevel = log4go.DEBUG
	}
	if *logfinest {
		loglevel = log4go.FINEST
	}

	return log4go.NewDefaultLogger(loglevel)
}
Ejemplo n.º 7
0
func SetupLogging() {
	flag.Parse()

	level := log.ERROR

	switch strings.ToLower(*loglevel) {
	case "debug":
		level = log.DEBUG
	case "trace":
		level = log.TRACE
	case "info":
		level = log.INFO
	case "warning":
		level = log.WARNING
	case "error":
		level = log.ERROR
	case "critical":
		level = log.CRITICAL
	}

	logger = log.NewDefaultLogger(level)
}
Ejemplo n.º 8
0
//init for logger
func InitLogger(logFilename string) log4go.Logger {
	//var filenameOnly string = GetCurFilename()
	//var logFilename string =  filenameOnly + ".log";

	//gLogger = log4go.NewLogger()
	//gLogger = make(log4go.Logger)

	//for console
	//gLogger.AddFilter("stdout", log4go.INFO, log4go.NewConsoleLogWriter())
	gLogger = log4go.NewDefaultLogger(log4go.INFO)

	//for log file
	if _, err := os.Stat(logFilename); err == nil {
		//fmt.Printf("found old log file %s, now remove it\n", logFilename)
		os.Remove(logFilename)
	}
	//gLogger.AddFilter("logfile", log4go.FINEST, log4go.NewFileLogWriter(logFilename, true))
	//gLogger.AddFilter("logfile", log4go.FINEST, log4go.NewFileLogWriter(logFilename, false))
	gLogger.AddFilter("log", log4go.FINEST, log4go.NewFileLogWriter(logFilename, false))
	gLogger.Debug("Current time is : %s", time.Now().Format("15:04:05 MST 2006/01/02"))

	return gLogger
}
Ejemplo n.º 9
0
func main() {
	log := l4g.NewDefaultLogger(l4g.DEBUG)

	//输出到控制台,级别为DEBUG
	log.AddFilter("stdout", l4g.DEBUG, l4g.NewConsoleLogWriter())

	//logs文件夹需要运行前创建,否则异常
	//输出到文件,级别为DEBUG,文件名为test.log,每次追加该原文件
	log.AddFilter("log", l4g.DEBUG, l4g.NewFileLogWriter("logs/test.log", false))

	//使用加载配置文件,类似与java的log4j.propertites
	//l4g.LoadConfiguration("log4go.xml")

	//l4g.Info("xxx")
	//log.Error("yyy")
	log.Debug("the time is now :%s -- %s", "213", "sad")

	//程序退出太快是来不及写log的
	time.Sleep(1000 * time.Millisecond)
	//log.Write2Exit()

	//注:如果不是一直运行的程序,请加上这句话,否则主线程结束后,也不会输出和log到日志文件
	defer log.Close()
}
Ejemplo n.º 10
0
import (
	l4g "code.google.com/p/log4go"
	"flag"
	"github.com/jasocox/goblog/blog"
	"github.com/jasocox/goblog/reader"
	"github.com/jasocox/goblog/router"
	"github.com/jasocox/goblog/view"
	"net/http"
	"os"
	"time"
)

var (
	blog_dir = flag.String("b", "", "directory where blogs a stored")
	protocol = flag.String("p", "2001", "protocal to run on")
	log      = l4g.NewDefaultLogger(l4g.WARNING)
)

func main() {
	log.Trace("Starting")
	flag.Parse()

	if *blog_dir == "" {
		log.Error("Must specify a directory where blogs are stored")
		time.Sleep(1000)
		os.Exit(1)
	}

	blogs := blog.New()
	blogReader := reader.New(blogs, *blog_dir, log)
	v := view.New(blogs, log)
func main() {
	flag.Parse()

	// Initialize logger
	logLevel = strings.ToLower(logLevel)
	switch {
	case logLevel == "trace":
		log = l4g.NewDefaultLogger(l4g.TRACE)
	case logLevel == "debug":
		log = l4g.NewDefaultLogger(l4g.DEBUG)
	case logLevel == "info":
		log = l4g.NewDefaultLogger(l4g.INFO)
	case logLevel == "warning":
		log = l4g.NewDefaultLogger(l4g.WARNING)
	case logLevel == "error":
		log = l4g.NewDefaultLogger(l4g.ERROR)
	}
	//log.AddFilter("log", l4g.FINE, l4g.NewFileLogWriter("example.log", true))
	bigjimmybot.SetLog(log)

	// Connect to DB
	log.Logf(l4g.TRACE, "main(): before OpenDB, %v goroutines.", runtime.NumGoroutine())
	bigjimmybot.OpenDB(dbUser, dbPassword, dbProtocol, dbHost, dbPort, dbName)
	defer bigjimmybot.CloseDB()
	log.Logf(l4g.TRACE, "main(): after OpenDB, %v goroutines.", runtime.NumGoroutine())

	var err error

	// Connect to Drive
	// Ensure we have googleClientId and googleClientSecret (from command-line args or DB)
	log.Logf(l4g.TRACE, "main(): before getting client id and secret, %v goroutines.", runtime.NumGoroutine())
	if googleClientId == "" {
		googleClientId, err = bigjimmybot.DbGetConfig("GOOGLE_CLIENT_ID")
		if err != nil {
			log.Logf(l4g.ERROR, "attempt to get googleClientId from DB failed: %v", err)
		}
	}
	if googleClientSecret == "" {
		googleClientSecret, err = bigjimmybot.DbGetConfig("GOOGLE_CLIENT_SECRET")
		if err != nil {
			log.Logf(l4g.ERROR, "attempt to get googleClientSecret from DB failed: %v", err)
		}
	}
	if googleClientId == "" || googleClientSecret == "" {
		log.Logf(l4g.ERROR, "Please specify -google_client_id and -google_client_secret (or ensure they are in the DB and database connection is working)")
		flag.Usage()
		l4g.Crashf(usageMsg)
	} else {
		log.Logf(l4g.INFO, "Using google_client_id=%v and google_client_secret=%v", googleClientId, googleClientSecret)
	}
	// Try to get google_domain (for permissions) from DB
	if googleDomain == "" {
		googleDomain, err = bigjimmybot.DbGetConfig("GOOGLE_DOMAIN")
		if err != nil {
			l4g.Crashf("Could not get pb_rest_uri from DB: %v", err)
		}
	}
	log.Logf(l4g.TRACE, "main(): before open drive, %v goroutines.", runtime.NumGoroutine())
	bigjimmybot.OpenDrive(googleClientId, googleClientSecret, googleDomain, cacheFile)
	log.Logf(l4g.TRACE, "main(): after open drive, %v goroutines.", runtime.NumGoroutine())

	// Setup PB REST client
	// Get pbRestUri if we don't have it
	if pbRestUri == "" {
		pbRestUri, err = bigjimmybot.DbGetConfig("PB_REST_URI")
		if err != nil {
			l4g.Crashf("Could not get pb_rest_uri from DB: %v", err)
		}
	}
	bigjimmybot.SetPbRestUri(pbRestUri)

	// Get huntFolderId (either from command-line, from database, or from Google Drive if we have title)
	// Ensure we can get huntFolderId (from command-line arg, DB, or via huntFolderTitle)
	if huntFolderId == "" {
		huntFolderId, _ = bigjimmybot.DbGetConfig("google_hunt_folder_id")
	}
	if huntFolderId == "" && huntFolderTitle == "" {
		// still don't have huntFolderId, and we don't have title either
		// try to get title from DB
		huntFolderTitle, _ = bigjimmybot.DbGetConfig("PB_HUNT")
	} else if huntFolderId != "" && huntFolderTitle != "" {
		log.Logf(l4g.INFO, "you specified hunt_folder_title but we have hunt_folder_id so it is being ignored.")
		huntFolderTitle = ""
	}
	log.Logf(l4g.TRACE, "main(): before getting hunt folder id, %v goroutines.", runtime.NumGoroutine())
	if huntFolderId == "" && huntFolderTitle != "" {
		// huntFolderId neither specified nor in DB, but we do have title
		// so get hunt folder ID from Google by looking it up by title
		// or create it if it does not exist
		log.Logf(l4g.INFO, "looking up google docs folder id for title %v", huntFolderTitle)
		huntFolderId, err = bigjimmybot.GetFolderIdByTitle(huntFolderTitle)
		if err != nil {
			if err, ok := err.(*bigjimmybot.ListError); ok {
				if err.Found > 1 {
					l4g.Crashf("more than one document matches %v", huntFolderTitle)
				} else if err.Found == 0 {
					//l4g.Crashf("no hunt folder found for %v", huntFolderTitle)
					log.Logf(l4g.INFO, "no hunt folder found for %v, creating it", huntFolderTitle)
					var cferr error
					huntFolderId, _, cferr = bigjimmybot.CreateHunt(huntFolderTitle)
					if cferr != nil {
						l4g.Crashf("could not create hunt folder for title [%v]: %v", huntFolderTitle, cferr)
					}
					log.Logf(l4g.INFO, "hunt folder created")
				}
			} else {
				l4g.Crashf("an error occurred getting hunt folder ID: %v", err)
			}
		}
		log.Logf(l4g.INFO, "hunt_folder_id: %v", huntFolderId)
		// DB doesn't yet have huntFolderId, set it if we have it
		if huntFolderId != "" {
			err = bigjimmybot.DbSetConfig("google_hunt_folder_id", huntFolderId)
			if err != nil {
				l4g.Crashf("could not set hunt_folder_id in DB")
			}
		}
	}
	log.Logf(l4g.TRACE, "main(): after getting hunt folder id, %v goroutines.", runtime.NumGoroutine())
	// set huntFolderId in bigjimmybot
	bigjimmybot.SetHuntFolderId(huntFolderId)

	// get initial version diff
	log.Logf(l4g.TRACE, "main(): before bigjimmybot.PbGetInitialVersionDiff %v goroutines.", runtime.NumGoroutine())
	bigjimmybot.PbGetInitialVersionDiff()
	log.Logf(l4g.TRACE, "main(): after bigjimmybot.PbGetInitialVersionDiff %v goroutines.", runtime.NumGoroutine())

	// Start ControlServer main loop
	// Ensure we have httpControlPort and httpControlPath
	log.Logf(l4g.TRACE, "main(): before ensuring httpControlPort and httpControlPath %v goroutines.", runtime.NumGoroutine())
	if httpControlPort == "" {
		httpControlPort, err = bigjimmybot.DbGetConfig("BIGJIMMY_CONTROL_PORT")
		if err != nil {
			log.Logf(l4g.ERROR, "attempt to get httpControlPort from DB failed: %v", err)
		}
	}
	if httpControlPath == "" {
		httpControlPath, err = bigjimmybot.DbGetConfig("BIGJIMMY_CONTROL_PATH")
		if err != nil {
			log.Logf(l4g.ERROR, "attempt to get httpControlPath from DB failed: %v", err)
		}
	}
	if httpControlPort == "" || httpControlPath == "" {
		log.Logf(l4g.ERROR, "Please specify -http_control_port and -http_control_path (or ensure they are in the DB and database connection is working)")
		flag.Usage()
		l4g.Crashf(usageMsg)
	} else {
		log.Logf(l4g.INFO, "Using http_control_port=%v and http_control_path=%v", httpControlPort, httpControlPath)
	}
	log.Logf(l4g.TRACE, "main(): after ensuring httpControlPort and httpControlPath %v goroutines.", runtime.NumGoroutine())

	time.Sleep(5 * time.Second)
	log.Logf(l4g.TRACE, "main(): before bigjimmybot.ControlServer %v goroutines.", runtime.NumGoroutine())
	bigjimmybot.ControlServer(httpControlPort, httpControlPath)
	log.Logf(l4g.TRACE, "main(): after bigjimmybot.ControlServer %v goroutines.", runtime.NumGoroutine())
}
Ejemplo n.º 12
0
func init() {
	log = l4g.NewDefaultLogger(l4g.INFO)
}