github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/shim/runsc/utils.go (about)

     1  // Copyright 2018 The containerd Authors.
     2  // Copyright 2018 The gVisor Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     https://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package runsc
    17  
    18  import (
    19  	"bytes"
    20  	"strings"
    21  	"sync"
    22  )
    23  
    24  var bytesBufferPool = sync.Pool{
    25  	New: func() interface{} {
    26  		return bytes.NewBuffer(nil)
    27  	},
    28  }
    29  
    30  func getBuf() *bytes.Buffer {
    31  	return bytesBufferPool.Get().(*bytes.Buffer)
    32  }
    33  
    34  func putBuf(b *bytes.Buffer) {
    35  	b.Reset()
    36  	bytesBufferPool.Put(b)
    37  }
    38  
    39  // FormatRunscLogPath parses runsc config, and fill in %ID% in the log path.
    40  func FormatRunscLogPath(id string, config map[string]string) {
    41  	if path, ok := config["debug-log"]; ok {
    42  		config["debug-log"] = strings.Replace(path, "%ID%", id, -1)
    43  	}
    44  }
    45  
    46  // FormatShimLogPath creates the file path to the log file. It replaces %ID%
    47  // in the path with the provided "id". It also uses a default log name if the
    48  // path end with '/'.
    49  func FormatShimLogPath(path string, id string) string {
    50  	if strings.HasSuffix(path, "/") {
    51  		// Default format: <path>/runsc-shim-<ID>.log
    52  		path += "runsc-shim-%ID%.log"
    53  	}
    54  	return strings.Replace(path, "%ID%", id, -1)
    55  }