File: log.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (72 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2012 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package sh

import (
	"io/ioutil"
	"log"
	"log/syslog"
	"os"
)

const PATH = "/sbin:/bin:/usr/sbin:/usr/bin"

const logFilename = "/.shutil.log" // in boot

var (
	env   []string
	home  string // to expand symbol "~"
	BOOT  bool   // does the script is being run during boot?
	DEBUG bool

	logFile *os.File
	Log     = log.New(ioutil.Discard, "", 0)
)

// Sets environment variables and a null logger.
func init() {
	log.SetFlags(0)
	log.SetPrefix("ERROR: ")

	if BOOT {
		env = []string{"PATH=" + PATH} // from file boot
	} else {
		env = os.Environ()
		home = os.Getenv("HOME")
	}

	/*if path := os.Getenv("PATH"); path == "" {
		if err = os.Setenv("PATH", PATH); err != nil {
			log.Print(err)
		}
	}*/
}

// StartLogger initializes the log file.
func StartLogger() {
	var err error

	if BOOT {
		if logFile, err = os.OpenFile(logFilename, os.O_WRONLY|os.O_TRUNC, 0); err != nil {
			log.Print(err)
		} else {
			Log = log.New(logFile, "", log.Lshortfile)
		}
	} else {
		if Log, err = syslog.NewLogger(syslog.LOG_NOTICE, log.Lshortfile); err != nil {
			log.Fatal(err)
		}
	}
}

// CloseLogger closes the log file.
func CloseLogger() error {
	if BOOT {
		return logFile.Close()
	}
	return nil
}