github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/internal/debug/trace.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  //+build go1.5
    13  
    14  package debug
    15  
    16  import (
    17  	"errors"
    18  	"os"
    19  	"runtime/trace"
    20  
    21  	"github.com/Sberex/go-sberex/log"
    22  )
    23  
    24  // StartGoTrace turns on tracing, writing to the given file.
    25  func (h *HandlerT) StartGoTrace(file string) error {
    26  	h.mu.Lock()
    27  	defer h.mu.Unlock()
    28  	if h.traceW != nil {
    29  		return errors.New("trace already in progress")
    30  	}
    31  	f, err := os.Create(expandHome(file))
    32  	if err != nil {
    33  		return err
    34  	}
    35  	if err := trace.Start(f); err != nil {
    36  		f.Close()
    37  		return err
    38  	}
    39  	h.traceW = f
    40  	h.traceFile = file
    41  	log.Info("Go tracing started", "dump", h.traceFile)
    42  	return nil
    43  }
    44  
    45  // StopTrace stops an ongoing trace.
    46  func (h *HandlerT) StopGoTrace() error {
    47  	h.mu.Lock()
    48  	defer h.mu.Unlock()
    49  	trace.Stop()
    50  	if h.traceW == nil {
    51  		return errors.New("trace not in progress")
    52  	}
    53  	log.Info("Done writing Go trace", "dump", h.traceFile)
    54  	h.traceW.Close()
    55  	h.traceW = nil
    56  	h.traceFile = ""
    57  	return nil
    58  }