Hey, It's commented "release lock while getting caller info - it's 
expensive" in source code of /src/log/log.go line:150.

I'm confused with what makes it expensive if we didn't unlock the l.mu ? 
Does goroutines have context ?

func (l *Logger) Output(calldepth int, s string) error {
    now := time.Now() // get this early.
    var file string
    var line int
    l.mu.Lock()
    defer l.mu.Unlock()
    if l.flag&(Lshortfile|Llongfile) != 0 {
        // release lock while getting caller info - it's expensive.
        l.mu.Unlock()
        var ok bool
        _, file, line, ok = runtime.Caller(calldepth)
        if !ok {
            file = "???"
            line = 0
        }
        l.mu.Lock()
    }
    l.buf = l.buf[:0]
    l.formatHeader(&l.buf, now, file, line)
    l.buf = append(l.buf, s...)
    if len(s) == 0 || s[len(s)-1] != '\n' {
        l.buf = append(l.buf, '\n')
    }
    _, err := l.out.Write(l.buf)
    return err
}

AND

What will happen if I moved the _, file, line, ok = 
runtime.Caller(calldepth) like below? Is better than the upper one ?

func (l *Logger) Output(calldepth int, s string) error {
    now := time.Now() // get this early.
    var file string
    var line int

        //moved here
    var ok bool
    _, file, line, ok = runtime.Caller(calldepth)

    l.mu.Lock()
    defer l.mu.Unlock()

        l.buf = l.buf[:0]
    if l.flag&(Lshortfile|Llongfile) != 0 {
        if !ok {
            file = "???"
            line = 0
        }
            l.formatHeader(&l.buf, now, file, line)
    }

    l.buf = append(l.buf, s...)
    if len(s) == 0 || s[len(s)-1] != '\n' {
        l.buf = append(l.buf, '\n')
    }
    _, err := l.out.Write(l.buf)
    return err
}

Thanks for all

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to