File: debug.go

package info (click to toggle)
golang-github-chewxy-hm 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,024 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
// +build debug

package hm

import (
	"fmt"
	"log"
	"os"
	"strings"
	"sync/atomic"
)

// DEBUG returns true when it's in debug mode
const DEBUG = false

var tabcount uint32

var _logger_ = log.New(os.Stderr, "", 0)
var replacement = "\n"

func tc() int {
	return int(atomic.LoadUint32(&tabcount))
}

func enterLoggingContext() {
	atomic.AddUint32(&tabcount, 1)
	tabs := tc()
	_logger_.SetPrefix(strings.Repeat("\t", tabs))
	replacement = "\n" + strings.Repeat("\t", tabs)
}

func leaveLoggingContext() {
	tabs := tc()
	tabs--

	if tabs < 0 {
		atomic.StoreUint32(&tabcount, 0)
		tabs = 0
	} else {
		atomic.StoreUint32(&tabcount, uint32(tabs))
	}
	_logger_.SetPrefix(strings.Repeat("\t", tabs))
	replacement = "\n" + strings.Repeat("\t", tabs)
}

func logf(format string, others ...interface{}) {
	if DEBUG {
		// format = strings.Replace(format, "\n", replacement, -1)
		s := fmt.Sprintf(format, others...)
		s = strings.Replace(s, "\n", replacement, -1)
		_logger_.Println(s)
		// _logger_.Printf(format, others...)
	}
}