// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package capnslog

import (
	
	
	
	
	
	
	
	
)

var pid = os.Getpid()

type GlogFormatter struct {
	StringFormatter
}

func ( io.Writer) *GlogFormatter {
	 := &GlogFormatter{}
	.w = bufio.NewWriter()
	return 
}

func ( GlogFormatter) ( string,  LogLevel,  int,  ...interface{}) {
	.w.Write(GlogHeader(, +1))
	.StringFormatter.Format(, , +1, ...)
}

func ( LogLevel,  int) []byte {
	// Lmmdd hh:mm:ss.uuuuuu threadid file:line]
	 := time.Now().UTC()
	, , ,  := runtime.Caller() // It's always the same number of frames to the user's call.
	if ! {
		 = "???"
		 = 1
	} else {
		 := strings.LastIndex(, "/")
		if  >= 0 {
			 = [+1:]
		}
	}
	if  < 0 {
		 = 0 // not a real line number
	}
	 := &bytes.Buffer{}
	.Grow(30)
	, ,  := .Date()
	, ,  := .Clock()
	.WriteString(.Char())
	twoDigits(, int())
	twoDigits(, )
	.WriteByte(' ')
	twoDigits(, )
	.WriteByte(':')
	twoDigits(, )
	.WriteByte(':')
	twoDigits(, )
	.WriteByte('.')
	.WriteString(strconv.Itoa(.Nanosecond() / 1000))
	.WriteByte('Z')
	.WriteByte(' ')
	.WriteString(strconv.Itoa(pid))
	.WriteByte(' ')
	.WriteString()
	.WriteByte(':')
	.WriteString(strconv.Itoa())
	.WriteByte(']')
	.WriteByte(' ')
	return .Bytes()
}

const digits = "0123456789"

func twoDigits( *bytes.Buffer,  int) {
	 := digits[%10]
	 /= 10
	 := digits[%10]
	.WriteByte()
	.WriteByte()
}