func setConsoleTextAttribute(handle uintptr, attribute WORD) error {
	r1, r2, err := setConsoleTextAttributeProc.Call(handle, uintptr(attribute), 0)
	use(attribute)
	return checkError(r1, r2, err)
}
setConsoleTextAttribute sets the attributes of characters written to the
console screen buffer by the WriteFile or WriteConsole function.
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047(v=vs.85).aspx.
package logger
import (
	"bytes"
	"fmt"
	"io"
	"os"
	"sync"
	"syscall"
	"golang.org/x/sys/windows"
)
var (
	kernel32DLL                 = windows.NewLazySystemDLL("kernel32.dll")
	setConsoleTextAttributeProc = kernel32DLL.NewProc("SetConsoleTextAttribute")
	stdOutMutex                 sync.Mutex
	stdErrMutex                 sync.Mutex
)
type WORD uint16
const (
	fgBlack     WORD = 0x0000
	fgBlue      WORD = 0x0001
	fgGreen     WORD = 0x0002
	fgCyan      WORD = 0x0003
	fgRed       WORD = 0x0004
	fgMagenta   WORD = 0x0005
	fgYellow    WORD = 0x0006
	fgWhite     WORD = 0x0007
	fgIntensity WORD = 0x0008
	fgMask      WORD = 0x000F
	bgBlack     WORD = 0x0000
	bgBlue      WORD = 0x0010
	bgGreen     WORD = 0x0020
	bgCyan      WORD = 0x0030
	bgRed       WORD = 0x0040
	bgMagenta   WORD = 0x0050
	bgYellow    WORD = 0x0060
	bgWhite     WORD = 0x0070
	bgIntensity WORD = 0x0080
	bgMask      WORD = 0x00F0
)
var codesWin = map[byte]WORD{
	0:  fgWhite | bgBlack,  
	1:  fgIntensity,        
	22: fgWhite,            
	39: fgWhite,            
	49: fgWhite,            
	30: fgBlack,            
	31: fgRed,              
	32: fgGreen,            
	33: fgYellow,           
	34: fgBlue,             
	35: fgMagenta,          
	36: fgCyan,             
	37: fgWhite,            
	90: fgWhite,            
	40: bgBlack,            
	41: bgRed,              
	42: bgGreen,            
	43: bgYellow,           
	44: bgBlue,             
	45: bgMagenta,          
	46: bgCyan,             
	47: bgWhite,            
}
func OutputWriterFromFile(out *os.File) io.Writer {
	return &ColorWriter{w: out, fd: out.Fd(), mutex: &stdOutMutex, lastFgColor: fgWhite}
}
func OutputWriter() io.Writer {
	return OutputWriterFromFile(os.Stdout)
}
func ErrorWriter() io.Writer {
	return &ColorWriter{w: os.Stderr, fd: os.Stderr.Fd(), mutex: &stdErrMutex, lastFgColor: fgWhite}
}
type ColorWriter struct {
	w           io.Writer
	fd          uintptr
	mutex       *sync.Mutex
	lastFgColor WORD
	bold        bool
}
func (cw *ColorWriter) Fd() uintptr {
	return cw.fd
}
func (cw *ColorWriter) Write(p []byte) (n int, err error) {
	cw.mutex.Lock()
	defer cw.mutex.Unlock()
	var totalWritten = len(p)
	ctlStart := []byte{0x1b, '['}
	for nextIndex := 0; len(p) > 0; {
		nextIndex = bytes.Index(p, ctlStart)
		if nextIndex == -1 {
			nextIndex = len(p)
		}
		cw.w.Write(p[0:nextIndex])
		if nextIndex+2 < len(p) {
			nextIndex += 2
		}
		p = p[nextIndex:]
		if len(p) > 0 {
			if '0' <= p[0] && p[0] <= '9' {
				p = cw.parseColorControl(p)
			} else {
				p = cw.parseControlCode(p)
			}
		}
	}
	return totalWritten, nil
}
func (cw *ColorWriter) parseColorControl(p []byte) []byte {
	var controlIndex int
	controlCode := p[controlIndex] - '0'
	controlIndex++
	for i := 0; controlIndex < len(p) && p[controlIndex] != 'm' && i < 4; i++ {
		if '0' <= p[controlIndex] && p[controlIndex] <= '9' {
			controlCode *= 10
			controlCode += p[controlIndex] - '0'
		}
		controlIndex++
	}
	if code, ok := codesWin[controlCode]; ok {
		if controlCode == 1 {
			cw.bold = true
		} else if controlCode == 0 || controlCode == 22 || controlCode == 39 {
			code = fgWhite
			cw.bold = false
		}
		if code >= fgBlue && code <= fgWhite {
			cw.lastFgColor = code
		} else {
			code = cw.lastFgColor
		}
		if cw.bold {
			code = code | fgIntensity
		}
		setConsoleTextAttribute(cw.fd, code)
	}
	if controlIndex+1 <= len(p) {
		controlIndex += 1
	}
	return p[controlIndex:]
}
func (cw *ColorWriter) parseControlCode(p []byte) []byte {
	if p[0] == 'D' {
		cw.w.Write([]byte(fmt.Sprintf("\b")))
	}
	return p[1:]
}