Documentation ¶
Overview ¶
a simple log lib some code from code.google.com/p/log4go console log is open, file and sock log is close by default you can use functin SetxxxLevel open or close the log pattern it will only print the log whose level is higher than the pattern's level
Index ¶
- Constants
- Variables
- func CanSetBool(x reflect.Value) bool
- func CanSetFloat(x reflect.Value) bool
- func CanSetInt(x reflect.Value) bool
- func CanSetUint(x reflect.Value) bool
- func EncodeProtocol(msg interface{}, encode int) ([]byte, error)
- func FormatLogRecord(rec *LogRecord) string
- func Marshal(m interface{}, encodeType int) ([]byte, error)
- func MsgLen(b []byte) uint32
- func SendJsonCmd(sess *Session, msgID uint64, msg []byte) error
- func SendSpbCmd(sess *Session, msgID uint64, msg interface{}) error
- func SpbDecode(data []byte, x interface{}) error
- func SpbEncode(data interface{}) ([]byte, error)
- func Unmarshal(data []byte, m interface{}, encodeType int) error
- type BufferPool
- type CMDType
- type Closer
- type Connect
- type Connector
- func (c *Connector) Addr() string
- func (c *Connector) ChangeAddr(addr string)
- func (c *Connector) Close()
- func (c *Connector) GetID() uint64
- func (c *Connector) IsClose() bool
- func (c *Connector) IsConnected() bool
- func (c *Connector) NotifyReconn()
- func (c *Connector) ReconnCount() int
- func (c *Connector) Send(data []byte) error
- func (c *Connector) Session() *Session
- type CurrentContent
- type FileLogWriter
- type FuncOnClose
- type FuncOnOpen
- type HttpHandler
- func (h *HttpHandler) Handle(pattern string, handler http.Handler)
- func (h *HttpHandler) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
- func (h *HttpHandler) Handler(r *http.Request) (http.Handler, string)
- func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type HttpService
- type JsonProto
- type JsonService
- type Level
- type Listener
- type LogRecord
- type Logger
- func (log *Logger) Close()
- func (log *Logger) Critical(arg0 interface{}, args ...interface{})
- func (log *Logger) Debug(arg0 interface{}, args ...interface{})
- func (log *Logger) Error(arg0 interface{}, args ...interface{})
- func (log *Logger) Info(arg0 interface{}, args ...interface{})
- func (log *Logger) SetFileLevel(lvl Level, fname string, param ...int)
- func (log *Logger) SetLevel(lvl Level)
- func (log *Logger) SetTermLevel(lvl Level)
- func (log *Logger) System(arg0 interface{}, args ...interface{})
- func (log *Logger) Warn(arg0 interface{}, args ...interface{})
- type LoopService
- type MsgParse
- type ReqProto
- type RpcFuncException
- type RpcService
- type RspProto
- type Server
- func (svr *Server) AddEchoService(name, address string, heartbeat uint32, threadId int) (*Service, error)
- func (svr *Server) AddHttpService(name, address string, heartbeat uint32, imp HttpService, h *HttpHandler, ...) (*Service, error)
- func (svr *Server) AddJsonService(name, address string, heartbeat uint32, imp JsonService, threadId int) (*Service, error)
- func (svr *Server) AddLoopService(name string, imp LoopService, threadId int) (*Service, error)
- func (svr *Server) AddRpcService(name, address string, heartbeat uint32, imp *ServiceRpc, threadId int) (*Service, error)
- func (svr *Server) AddService(name, address string, heartbeat uint32, imp ServiceImp, threadId int) (*Service, error)
- func (svr *Server) AddSpbService(name, address string, heartbeat uint32, imp *ServiceSpb, threadId int) (*Service, error)
- func (svr *Server) AddTcpProxyService(address string, heartbeat uint32, threadId int, proxyaddr []string, ...) error
- func (svr *Server) PushRequest(servicename string, sess *Session, msgid int64, msg interface{}) error
- func (svr *Server) SetLogLvl(lvl Level)
- func (svr *Server) Start() error
- func (svr *Server) Stop()
- type Service
- func (service *Service) GetConnect(id uint64) *Connect
- func (service *Service) Imp() ServiceImp
- func (service *Service) IterateConnect(callback func(*Connect) bool)
- func (service *Service) NewConnect(address string, userdata interface{}) *Connect
- func (service *Service) ParseMsg(sess *Session, data []byte) int
- func (service *Service) PushRequest(sess *Session, msgid int64, msg interface{}) error
- type ServiceBase
- func (service *ServiceBase) Destroy()
- func (service *ServiceBase) HandleError(current *CurrentContent, err error)
- func (service *ServiceBase) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
- func (service *ServiceBase) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
- func (service *ServiceBase) HeartBeatTimeOut(sess *Session)
- func (service *ServiceBase) Init() bool
- func (service *ServiceBase) Loop()
- func (service *ServiceBase) SessionClose(sess *Session)
- func (service *ServiceBase) SessionOpen(sess *Session)
- func (service *ServiceBase) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)
- type ServiceEcho
- type ServiceHttp
- func (service *ServiceHttp) HandleError(current *CurrentContent, err error)
- func (service *ServiceHttp) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
- func (service *ServiceHttp) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
- func (service *ServiceHttp) Init() bool
- func (service *ServiceHttp) Loop()
- func (service *ServiceHttp) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)
- type ServiceImp
- type ServiceJson
- func (service *ServiceJson) HandleError(current *CurrentContent, err error)
- func (service *ServiceJson) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
- func (service *ServiceJson) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
- func (service *ServiceJson) Init() bool
- func (service *ServiceJson) Loop()
- func (service *ServiceJson) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)
- type ServiceLoop
- type ServiceProxyC
- type ServiceProxyS
- func (service *ServiceProxyS) HandleError(current *CurrentContent, err error)
- func (service *ServiceProxyS) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
- func (service *ServiceProxyS) HeartBeatTimeOut(sess *Session)
- func (service *ServiceProxyS) SessionClose(sess *Session)
- func (service *ServiceProxyS) SessionOpen(sess *Session)
- func (service *ServiceProxyS) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)
- type ServiceRpc
- func (service *ServiceRpc) HandleError(current *CurrentContent, err error)
- func (service *ServiceRpc) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
- func (service *ServiceRpc) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
- func (service *ServiceRpc) Init() bool
- func (service *ServiceRpc) Loop()
- func (service *ServiceRpc) RpcCall(sess *Session, funcName string, params ...interface{}) error
- func (service *ServiceRpc) RpcCall_Sync(sess *Session, funcName string, params ...interface{}) error
- func (service *ServiceRpc) UdpRpcCall(sess *Session, peer net.Addr, funcName string, params ...interface{}) error
- func (service *ServiceRpc) UdpRpcCall_Sync(sess *Session, peer net.Addr, funcName string, params ...interface{}) error
- func (service *ServiceRpc) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)
- type ServiceSpb
- func (service *ServiceSpb) HandleError(current *CurrentContent, err error)
- func (service *ServiceSpb) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
- func (service *ServiceSpb) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
- func (service *ServiceSpb) Init() bool
- func (service *ServiceSpb) Loop()
- func (service *ServiceSpb) RegisterMsg(msgId uint64, msg interface{}) error
- func (service *ServiceSpb) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)
- type Session
- type Spb
- type SpbService
Constants ¶
const ( EncodeTyepSpb = 0 EncodeTyepJson = 1 )
const ( RpcErrNoRemoteFunc = -1 RpcErrCallTimeout = -2 RpcErrFuncParamErr = -3 )
const ( SpbPackDataType_Integer_Positive = 0 SpbPackDataType_Integer_Negative = 1 SpbPackDataType_Float = 2 SpbPackDataType_Double = 3 SpbPackDataType_String = 4 SpbPackDataType_Vector = 5 SpbPackDataType_Map = 6 SpbPackDataType_StructBegin = 7 SpbPackDataType_StructEnd = 8 )
Variables ¶
var ( ErrSocketClosed = errors.New("socket closed") ErrSocketIsOpen = errors.New("socket is open") //ErrSendOverTime = errors.New("send message over time") // length of send(recv) buffer = 256(tcp) 10240(udp) default ErrSendBuffIsFull = errors.New("send buffer is full") ErrMsgParseNil = errors.New("MsgParse is nil") )
var ( MsgBuffSize = 1024 MinMsgSize = 64 MaxMsgSize = 2 * 1024 * 1024 //the length of send(recv) queue WriterListLen = 256 RecvListLen = 256 )
message recv buffer size
var GlobalSessionID uint64
session id
var (
TimeOut int64 = 5 //sec
)
Functions ¶
func CanSetBool ¶
func CanSetFloat ¶
func CanSetUint ¶
func EncodeProtocol ¶
func FormatLogRecord ¶
func SendSpbCmd ¶
Types ¶
type BufferPool ¶
type BufferPool struct { }
func (*BufferPool) Alloc ¶
func (bp *BufferPool) Alloc(bufsize int) []byte
func (*BufferPool) Free ¶
func (bp *BufferPool) Free([]byte)
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
func NewConnector ¶
NewConnector reconnect at 0 1 4 9 16...times reconnectMSec(100ms);when call send or changeAddr, it will NotifyReconn and reconnect at once;when call Close, reconnect will stop
func (*Connector) ChangeAddr ¶
func (*Connector) IsConnected ¶
func (*Connector) NotifyReconn ¶
func (c *Connector) NotifyReconn()
func (*Connector) ReconnCount ¶
type CurrentContent ¶
type FileLogWriter ¶
type FileLogWriter struct {
// contains filtered or unexported fields
}
This log writer sends output to a file
type HttpHandler ¶
type HttpHandler struct {
// contains filtered or unexported fields
}
func (*HttpHandler) Handle ¶
func (h *HttpHandler) Handle(pattern string, handler http.Handler)
Handle registers the handler for the given pattern in the DefaultServeMux. The documentation for ServeMux explains how patterns are matched.
func (*HttpHandler) HandleFunc ¶
func (h *HttpHandler) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
HandleFunc registers the handler function for the given pattern.
func (*HttpHandler) ServeHTTP ¶
func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.
type HttpService ¶
type HttpService interface { Init() bool Loop() HandleError(current *CurrentContent, e error) HashProcessor(current *CurrentContent, req *http.Request) (processorID int) }
type JsonService ¶
type JsonService interface { Init() bool Loop() Handle(current *CurrentContent, cmd JsonProto, e error) HashProcessor(current *CurrentContent, cmd JsonProto) (processorID int) }
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
func NewListener ¶
func NewUdpListener ¶
func (*Listener) GetSession ¶
func (*Listener) IterateSession ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func NewFileLogger ¶
func (*Logger) SetFileLevel ¶
等级 文件名 log文件最大值 是否每天滚动 最大备份文件个数 param: maxsize int (the maxsize of single log file), daily int(is rotate daily), maxbackup int(max count of the backup log files)
func (*Logger) SetTermLevel ¶
type LoopService ¶
type LoopService interface { Init() bool Loop() }
type RpcFuncException ¶
type RpcFuncException func(rspCode int32)
type RpcService ¶
type RpcService interface { Loop() HandleError(current *CurrentContent, err error) HashProcessor(current *CurrentContent) (processorID int) }
type Server ¶
type Server struct { ProcessorThreadsNum int //number of threads in server. // contains filtered or unexported fields }
func (*Server) AddEchoService ¶
func (*Server) AddHttpService ¶
func (svr *Server) AddHttpService(name, address string, heartbeat uint32, imp HttpService, h *HttpHandler, threadId int) (*Service, error)
AddHttpService rsp: HttpHandler
func (*Server) AddJsonService ¶
func (svr *Server) AddJsonService(name, address string, heartbeat uint32, imp JsonService, threadId int) (*Service, error)
AddJsonService use SendJsonCmd to send message
func (*Server) AddLoopService ¶
func (*Server) AddRpcService ¶
func (svr *Server) AddRpcService(name, address string, heartbeat uint32, imp *ServiceRpc, threadId int) (*Service, error)
AddRpcService imp: NewServiceRpc
func (*Server) AddService ¶
func (svr *Server) AddService(name, address string, heartbeat uint32, imp ServiceImp, threadId int) (*Service, error)
AddService must be called before server started. address could be null,then you get a service without listen; address could be udp,example udp:127.0.0.1:6060,default use tcp(127.0.0.1:6060) when heartbeat(second)=0,heartbeat will be close. threadId should be between 1-ProcessorThreadsNum. call Service.NewConnect start a connector
func (*Server) AddSpbService ¶
func (svr *Server) AddSpbService(name, address string, heartbeat uint32, imp *ServiceSpb, threadId int) (*Service, error)
AddSpbService imp: NewServiceSpb, use SendSpbCmd to send message, RegisterMsg to register msg.
func (*Server) AddTcpProxyService ¶
func (*Server) PushRequest ¶
func (svr *Server) PushRequest(servicename string, sess *Session, msgid int64, msg interface{}) error
PushRequest push message into handle thread;id of thread is the result of ServiceImp.HashProcessor
type Service ¶
func (*Service) GetConnect ¶
func (*Service) Imp ¶
func (service *Service) Imp() ServiceImp
func (*Service) IterateConnect ¶
func (*Service) NewConnect ¶
NewConnect reconnect at 0 1 4 9 16...times reconnectMSec(100ms);when call send or changeAddr, it will NotifyReconn and reconnect at once;when call Close, reconnect will stop
type ServiceBase ¶
type ServiceBase struct { }
ServiceImpBase
func (*ServiceBase) Destroy ¶
func (service *ServiceBase) Destroy()
func (*ServiceBase) HandleError ¶
func (service *ServiceBase) HandleError(current *CurrentContent, err error)
func (*ServiceBase) HandleMessage ¶
func (service *ServiceBase) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
func (*ServiceBase) HashProcessor ¶
func (service *ServiceBase) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
processorID is the thread who process this msg;it should between 1-ProcessorThreadsNum. if processorID == 0, it only uses main thread of the service. if processorID < 0, it will use hash of session id.
func (*ServiceBase) HeartBeatTimeOut ¶
func (service *ServiceBase) HeartBeatTimeOut(sess *Session)
func (*ServiceBase) Init ¶
func (service *ServiceBase) Init() bool
func (*ServiceBase) Loop ¶
func (service *ServiceBase) Loop()
func (*ServiceBase) SessionClose ¶
func (service *ServiceBase) SessionClose(sess *Session)
func (*ServiceBase) SessionOpen ¶
func (service *ServiceBase) SessionOpen(sess *Session)
type ServiceEcho ¶
type ServiceEcho struct {
ServiceBase
}
ServiceImpEcho
func (*ServiceEcho) HashProcessor ¶
func (service *ServiceEcho) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
type ServiceHttp ¶
type ServiceHttp struct { ServiceBase // contains filtered or unexported fields }
ServiceHttp
func (*ServiceHttp) HandleError ¶
func (service *ServiceHttp) HandleError(current *CurrentContent, err error)
func (*ServiceHttp) HandleMessage ¶
func (service *ServiceHttp) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
func (*ServiceHttp) HashProcessor ¶
func (service *ServiceHttp) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
func (*ServiceHttp) Init ¶
func (service *ServiceHttp) Init() bool
func (*ServiceHttp) Loop ¶
func (service *ServiceHttp) Loop()
type ServiceImp ¶
type ServiceImp interface { Init() bool Loop() Destroy() //in hash thread by HashProcessor HandleMessage(current *CurrentContent, msgID uint64, msg interface{}) //in main thread of service HandleError(*CurrentContent, error) SessionClose(sess *Session) HeartBeatTimeOut(sess *Session) //in random thread SessionOpen(sess *Session) // Unmarshal protocol parsed //lenParsed is the length read from 'data'. //msgID and msg are messages parsed from data. //when lenParsed <= 0 or msgID < 0,msg and err will be ignored. Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error) // HashProcessor sess msgID msg are returned by func of Unmarshal //processorID is the thread who process this msg;it should be between 1-ProcessorThreadsNum. //if processorID == 0, it only uses main thread of the service. //if processorID < 0, it will use hash of session id. HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int) }
type ServiceJson ¶
type ServiceJson struct { ServiceBase // contains filtered or unexported fields }
ServiceJson
func (*ServiceJson) HandleError ¶
func (service *ServiceJson) HandleError(current *CurrentContent, err error)
func (*ServiceJson) HandleMessage ¶
func (service *ServiceJson) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
func (*ServiceJson) HashProcessor ¶
func (service *ServiceJson) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
func (*ServiceJson) Init ¶
func (service *ServiceJson) Init() bool
func (*ServiceJson) Loop ¶
func (service *ServiceJson) Loop()
type ServiceLoop ¶
type ServiceLoop struct { ServiceBase // contains filtered or unexported fields }
func (*ServiceLoop) Init ¶
func (service *ServiceLoop) Init() bool
func (*ServiceLoop) Loop ¶
func (service *ServiceLoop) Loop()
type ServiceProxyC ¶
type ServiceProxyC struct {
ServiceBase
}
func (*ServiceProxyC) HashProcessor ¶
func (service *ServiceProxyC) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
type ServiceProxyS ¶
type ServiceProxyS struct { ServiceBase // contains filtered or unexported fields }
func (*ServiceProxyS) HandleError ¶
func (service *ServiceProxyS) HandleError(current *CurrentContent, err error)
func (*ServiceProxyS) HashProcessor ¶
func (service *ServiceProxyS) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
func (*ServiceProxyS) HeartBeatTimeOut ¶
func (service *ServiceProxyS) HeartBeatTimeOut(sess *Session)
func (*ServiceProxyS) SessionClose ¶
func (service *ServiceProxyS) SessionClose(sess *Session)
func (*ServiceProxyS) SessionOpen ¶
func (service *ServiceProxyS) SessionOpen(sess *Session)
type ServiceRpc ¶
type ServiceRpc struct { ServiceBase // contains filtered or unexported fields }
func NewServiceRpc ¶
func NewServiceRpc(imp RpcService) *ServiceRpc
func (*ServiceRpc) HandleError ¶
func (service *ServiceRpc) HandleError(current *CurrentContent, err error)
func (*ServiceRpc) HandleMessage ¶
func (service *ServiceRpc) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
func (*ServiceRpc) HashProcessor ¶
func (service *ServiceRpc) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
func (*ServiceRpc) Init ¶
func (service *ServiceRpc) Init() bool
func (*ServiceRpc) Loop ¶
func (service *ServiceRpc) Loop()
func (*ServiceRpc) RpcCall ¶
func (service *ServiceRpc) RpcCall(sess *Session, funcName string, params ...interface{}) error
RpcCall remotesession remotefunction(string) functionparams callback(could nil) exception(could nil, func(rspCode int32))
func (*ServiceRpc) RpcCall_Sync ¶
func (service *ServiceRpc) RpcCall_Sync(sess *Session, funcName string, params ...interface{}) error
func (*ServiceRpc) UdpRpcCall ¶
func (*ServiceRpc) UdpRpcCall_Sync ¶
type ServiceSpb ¶
type ServiceSpb struct { ServiceBase // contains filtered or unexported fields }
ServiceSpb
func NewServiceSpb ¶
func NewServiceSpb(imp SpbService) *ServiceSpb
func (*ServiceSpb) HandleError ¶
func (service *ServiceSpb) HandleError(current *CurrentContent, err error)
func (*ServiceSpb) HandleMessage ¶
func (service *ServiceSpb) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
func (*ServiceSpb) HashProcessor ¶
func (service *ServiceSpb) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
func (*ServiceSpb) Init ¶
func (service *ServiceSpb) Init() bool
func (*ServiceSpb) Loop ¶
func (service *ServiceSpb) Loop()
func (*ServiceSpb) RegisterMsg ¶
func (service *ServiceSpb) RegisterMsg(msgId uint64, msg interface{}) error
type Session ¶
type Session struct { UserData interface{} // contains filtered or unexported fields }
func NewSession ¶
func NewSession(con net.Conn, msgparse MsgParse, onopen FuncOnOpen, onclose FuncOnClose, heartbeat uint32, isudp bool) (*Session, error)
func (*Session) RemoteAddr ¶
type SpbService ¶
type SpbService interface { Init() bool Loop() Handle(current *CurrentContent, cmdId uint64, cmd interface{}, e error) HashProcessor(current *CurrentContent, cmdId uint64) (processorID int) }