Exemplo n.º 1
0
func logSetup() {
	level, err := log.LevelInt(*f_loglevel)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	color := true
	if runtime.GOOS == "windows" {
		color = false
	}

	if *f_log {
		log.AddLogger("stdio", os.Stderr, level, color)
	}

	if *f_logfile != "" {
		err := os.MkdirAll(filepath.Dir(*f_logfile), 0755)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		logfile, err := os.OpenFile(*f_logfile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		log.AddLogger("file", logfile, level, false)
	}
}
Exemplo n.º 2
0
func cliLogFile(c *minicli.Command) *minicli.Response {
	resp := &minicli.Response{Host: hostname}

	if len(c.StringArgs) == 0 {
		// Print true or false depending on whether file is enabled
		if logFile != nil {
			resp.Response = logFile.Name()
		}
	} else {
		var err error

		// Enable logging to file if it's not already enabled
		level, _ := log.LevelInt(*f_loglevel)

		if logFile != nil {
			if err = stopFileLogger(); err != nil {
				resp.Error = err.Error()
				return resp
			}
		}

		flags := os.O_WRONLY | os.O_APPEND | os.O_CREATE
		logFile, err = os.OpenFile(c.StringArgs["file"], flags, 0660)
		if err != nil {
			resp.Error = err.Error()
		} else {
			log.AddLogger("file", logFile, level, false)
		}
	}

	return resp
}
Exemplo n.º 3
0
func cliLogStderr(c *minicli.Command) *minicli.Response {
	resp := &minicli.Response{Host: hostname}

	if c.BoolArgs["false"] {
		// Turn off logging to stderr
		log.DelLogger("stdio")
	} else if len(c.BoolArgs) == 0 {
		// Print true or false depending on whether stderr is enabled
		_, err := log.GetLevel("stdio")
		resp.Response = strconv.FormatBool(err == nil)
	} else if c.BoolArgs["true"] {
		// Enable stderr logging or adjust the level if already enabled
		level, _ := log.LevelInt(*f_loglevel)
		_, err := log.GetLevel("stdio")
		if err != nil {
			log.AddLogger("stdio", os.Stderr, level, true)
		} else {
			// TODO: Why do this? cliLogLevel updates stdio level whenever
			// f_loglevel is changed.
			log.SetLevel("stdio", level)
		}
	}

	return resp
}
Exemplo n.º 4
0
func cliLogFile(c *minicli.Command, resp *minicli.Response) error {
	if len(c.StringArgs) == 0 {
		// Print true or false depending on whether file is enabled
		if logFile != nil {
			resp.Response = logFile.Name()
		}

		return nil
	}

	// Enable logging to file if it's not already enabled
	level, _ := log.LevelInt(*f_loglevel)

	if logFile != nil {
		if err := stopFileLogger(); err != nil {
			return err
		}
	}

	err := os.MkdirAll(filepath.Dir(c.StringArgs["file"]), 0755)
	if err != nil {
		return err
	}

	flags := os.O_WRONLY | os.O_APPEND | os.O_CREATE
	logFile, err = os.OpenFile(c.StringArgs["file"], flags, 0660)
	if err != nil {
		return err
	}

	log.AddLogger("file", logFile, level, false)
	return nil
}
Exemplo n.º 5
0
func cliLogSyslog(c *minicli.Command, resp *minicli.Response) error {
	var network string
	var address string

	if c.BoolArgs["local"] {
		network = "local"
	} else {
		address = c.StringArgs["address"]
		if c.BoolArgs["tcp"] {
			network = "tcp"
		} else {
			network = "udp"
		}
	}

	level, _ := log.LevelInt(*f_loglevel)

	return log.AddSyslog(network, address, "minimega", level)
}
Exemplo n.º 6
0
func cliLogLevel(c *minicli.Command) *minicli.Response {
	resp := &minicli.Response{Host: hostname}

	if len(c.BoolArgs) == 0 {
		// Print the level
		resp.Response = *f_loglevel
	} else {
		// Bool args should only have a single key that is the log level
		for k := range c.BoolArgs {
			level, err := log.LevelInt(k)
			if err != nil {
				log.Fatalln("someone goofed on the patterns")
			}

			*f_loglevel = k
			// forget the error, if they don't exist we shouldn't be setting
			// their level, so we're fine.
			log.SetLevel("stdio", level)
			log.SetLevel("file", level)
		}
	}

	return resp
}
Exemplo n.º 7
0
func cliLogLevel(c *minicli.Command, resp *minicli.Response) error {
	if len(c.BoolArgs) == 0 {
		// Print the level
		resp.Response = *f_loglevel
		return nil
	}

	// Bool args should only have a single key that is the log level
	for k := range c.BoolArgs {
		level, err := log.LevelInt(k)
		if err != nil {
			return errors.New("unreachable")
		}

		*f_loglevel = k
		// forget the error, if they don't exist we shouldn't be setting
		// their level, so we're fine.
		log.SetLevel("stdio", level)
		log.SetLevel("file", level)
		log.SetLevel("syslog", level)
	}

	return nil
}
Exemplo n.º 8
0
func logSetup() {
	level, err := log.LevelInt(*f_loglevel)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	color := true
	if runtime.GOOS == "windows" {
		color = false
	}

	if *f_log {
		log.AddLogger("stdio", os.Stderr, level, color)
	}

	if *f_logfile != "" {
		err := os.MkdirAll(filepath.Dir(*f_logfile), 0755)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		logfile, err := os.OpenFile(*f_logfile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		log.AddLogger("file", logfile, level, false)
	}

	// a special logger for pushing logs up to minimega
	if *f_miniccc != "" && *f_u == "" {
		var f tagLogger
		log.AddLogger("taglogger", &f, level, false)
	}
}