github.com/aerospike/aerospike-client-go/v6@v6.15.1/internal/lua/lua_aerospike.go (about)

     1  //go:build !app_engine
     2  // +build !app_engine
     3  
     4  // Copyright 2014-2022 Aerospike, Inc.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  // http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package lua
    19  
    20  import (
    21  	"github.com/aerospike/aerospike-client-go/v6/logger"
    22  	lua "github.com/yuin/gopher-lua"
    23  )
    24  
    25  const luaLuaAerospikeTypeName = "LuaAerospike"
    26  
    27  // Registers my luaAerospike type to given L.
    28  func registerLuaAerospikeType(L *lua.LState) {
    29  	mt := L.NewTypeMetatable(luaLuaAerospikeTypeName)
    30  
    31  	L.SetGlobal("aerospike", mt)
    32  
    33  	// static attributes
    34  	L.SetField(mt, "log", L.NewFunction(luaAerospikeLog))
    35  
    36  	L.SetMetatable(mt, mt)
    37  }
    38  
    39  func luaAerospikeLog(L *lua.LState) int {
    40  	if L.GetTop() < 2 || L.GetTop() > 3 {
    41  		L.ArgError(1, "2 arguments are expected for aerospike:log method")
    42  		return 0
    43  	}
    44  
    45  	// account for calling it on a table
    46  	paramIdx := 1
    47  	if L.GetTop() == 3 {
    48  		paramIdx = 2
    49  	}
    50  
    51  	level := L.CheckInt(paramIdx)
    52  	str := L.CheckString(paramIdx + 1)
    53  
    54  	switch level {
    55  	case 1:
    56  		logger.Logger.Warn(str)
    57  	case 2:
    58  		logger.Logger.Info(str)
    59  	case 3, 4:
    60  		logger.Logger.Debug(str)
    61  	}
    62  
    63  	return 0
    64  }