From 3d31395d55785a2bc21c7cd38825fd3d3e93d20f Mon Sep 17 00:00:00 2001 From: Shingo INADA Date: Sun, 3 May 2020 16:05:24 +0900 Subject: [PATCH 1/4] good bye glog --- gdxsv/lbs.go | 87 ++++++++++++++++++++++----------------- gdxsv/lbs_battle.go | 7 ++-- gdxsv/lbs_handler.go | 96 ++++++++++++++++++++++++-------------------- gdxsv/lbs_lobby.go | 42 ++++++++++++++++--- gdxsv/lbs_mcsfunc.go | 6 +-- gdxsv/lbs_message.go | 7 ++-- gdxsv/main.go | 79 +++++++++++++++++++++++------------- gdxsv/mcs.go | 63 +++++++++++++++-------------- gdxsv/mcs_room.go | 15 +++---- gdxsv/mcs_tcp.go | 40 ++++++++++++------ gdxsv/shareddata.go | 5 +-- go.mod | 4 +- go.sum | 41 +++++++++++++++++++ 13 files changed, 314 insertions(+), 178 deletions(-) diff --git a/gdxsv/lbs.go b/gdxsv/lbs.go index ffa1b9f..632f008 100644 --- a/gdxsv/lbs.go +++ b/gdxsv/lbs.go @@ -4,12 +4,11 @@ import ( "context" "encoding/json" "fmt" + "go.uber.org/zap" "net" "strings" "sync" "time" - - "github.com/golang/glog" ) const ( @@ -81,7 +80,7 @@ func (lbs *Lbs) GetLobby(platform uint8, lobbyID uint16) *LbsLobby { } func (lbs *Lbs) ListenAndServe(addr string) error { - glog.Info("ListenAndServe", addr) + logger.Info("ListenAndServe", zap.String("addr", addr)) tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { @@ -95,10 +94,10 @@ func (lbs *Lbs) ListenAndServe(addr string) error { for { tcpConn, err := listener.AcceptTCP() if err != nil { - glog.Errorln(err) + logger.Error("failed to accept", zap.Error(err)) continue } - glog.Infoln("A new tcp connection open.", tcpConn.RemoteAddr()) + logger.Info("a new connection open", zap.String("addr", tcpConn.RemoteAddr().String())) peer := lbs.NewPeer(tcpConn) go peer.serve() } @@ -112,6 +111,7 @@ func (lbs *Lbs) NewPeer(conn *net.TCPConn) *LbsPeer { chDispatch: make(chan bool, 1), outbuf: make([]byte, 0, 1024), inbuf: make([]byte, 0, 1024), + logger: logger.With(zap.String("addr", conn.RemoteAddr().String())), } } @@ -165,7 +165,7 @@ func (lbs *Lbs) Quit() { func stripHost(addr string) string { _, port, err := net.SplitHostPort(addr) if err != nil { - glog.Fatal("err in splitPort", err) + logger.DPanic("failed to split host port", zap.Error(err)) } return ":" + fmt.Sprint(port) } @@ -223,28 +223,28 @@ func (lbs *Lbs) eventLoop() { case e := <-lbs.chEvent: switch args := e.(type) { case eventPeerCome: - glog.Infoln("eventPeerCome") + args.peer.logger.Info("eventPeerCome") args.peer.lastRecvTime = time.Now() peers[args.peer.Address()] = args.peer StartLoginFlow(args.peer) case eventPeerMessage: + args.peer.logger.Info("eventPeerMessage", zap.Any("msg", args.msg)) args.peer.lastRecvTime = time.Now() if f, ok := lbs.handlers[args.msg.Command]; ok { f(args.peer, args.msg) } else { - glog.Errorf("======================================") - glog.Errorf("======================================") - glog.Errorf("======================================") - glog.Errorf("Handler not found: 0x%04x %v msg:%v", uint16(args.msg.Command), args.msg.Command, args.msg) - glog.Errorf("======================================") - glog.Errorf("======================================") - glog.Errorf("======================================") + logger.Warn("handler not found", + zap.String("cmd", args.msg.Command.String()), + zap.String("cmd_id", fmt.Sprintf("0x%04x", uint16(args.msg.Command))), + zap.String("msg", args.msg.String()), + zap.Binary("body", args.msg.Body), + ) if args.msg.Category == CategoryQuestion { args.peer.SendMessage(NewServerAnswer(args.msg)) } } case eventPeerLeave: - glog.Infoln("eventPeerLeave") + args.peer.logger.Info("eventPeerLeave") lbs.cleanPeer(args.peer) delete(peers, args.peer.Address()) case eventFunc: @@ -255,7 +255,7 @@ func (lbs *Lbs) eventLoop() { for _, p := range peers { lastRecvSince := time.Since(p.lastRecvTime) if 1 <= lastRecvSince.Minutes() { - glog.Infoln("Kick peer", p.Address()) + logger.Info("kick peer", zap.String("addr", p.Address())) p.conn.Close() } else if 10 <= lastRecvSince.Seconds() { RequestLineCheck(p) @@ -386,15 +386,19 @@ func (lbs *Lbs) BroadcastRoomState(room *LbsRoom) { func (lbs *Lbs) RegisterBattleResult(p *LbsPeer, result *BattleResult) { js, err := json.Marshal(result) if err != nil { - glog.Errorln("Failed to marshal battle result", err) - glog.Infoln(result) + logger.Error("failed to marshal battle result", + zap.Error(err), + zap.String("battle_code", result.BattleCode), + zap.Any("battle_result", result)) return } record, err := getDB().GetBattleRecordUser(result.BattleCode, p.UserID) if err != nil { - glog.Errorln("Failed to load battle record", err) - glog.Infoln(string(js)) + logger.Error("failed to load battle record", + zap.Error(err), + zap.String("battle_code", result.BattleCode), + zap.Any("battle_result", result)) return } @@ -408,15 +412,20 @@ func (lbs *Lbs) RegisterBattleResult(p *LbsPeer, result *BattleResult) { err = getDB().UpdateBattleRecord(record) if err != nil { - glog.Errorln("Failed to save battle record", err) - glog.Infoln(record) + logger.Error("failed to save battle record", + zap.Error(err), + zap.String("battle_code", result.BattleCode), + zap.Any("battle_result", result)) return } - glog.Infoln("before", p.DBUser) + logger.Info("update battle count", + zap.String("user_id", p.UserID), + zap.Any("before", p.DBUser)) + rec, err := getDB().CalculateUserTotalBattleCount(p.UserID, 0) if err != nil { - glog.Errorln("Failed to calculate battle count", err) + logger.Error("failed to calculate battle count", zap.Error(err)) return } @@ -428,7 +437,7 @@ func (lbs *Lbs) RegisterBattleResult(p *LbsPeer, result *BattleResult) { rec, err = getDB().CalculateUserTotalBattleCount(p.UserID, 1) if err != nil { - glog.Errorln("Failed to calculate battle count", err) + logger.Error("failed to calculate battle count", zap.Error(err)) return } @@ -440,7 +449,7 @@ func (lbs *Lbs) RegisterBattleResult(p *LbsPeer, result *BattleResult) { rec, err = getDB().CalculateUserTotalBattleCount(p.UserID, 2) if err != nil { - glog.Errorln("Failed to calculate battle count", err) + logger.Error("failed to calculate battle count", zap.Error(err)) return } @@ -452,7 +461,7 @@ func (lbs *Lbs) RegisterBattleResult(p *LbsPeer, result *BattleResult) { rec, err = getDB().CalculateUserDailyBattleCount(p.UserID) if err != nil { - glog.Errorln("Failed to calculate battle count", err) + logger.Error("failed to calculate battle count", zap.Error(err)) return } @@ -462,14 +471,18 @@ func (lbs *Lbs) RegisterBattleResult(p *LbsPeer, result *BattleResult) { err = getDB().UpdateUser(&p.DBUser) if err != nil { - glog.Errorln(err) + logger.Error("failed to update user", zap.Error(err)) return } - glog.Infoln("after", p.DBUser) + + logger.Info("update battle count", + zap.String("user_id", p.UserID), + zap.Any("after", p.DBUser)) } type LbsPeer struct { DBUser + logger *zap.Logger conn *net.TCPConn app *Lbs @@ -483,8 +496,8 @@ type LbsPeer struct { PilotName string Rank int - lastConnectionID string - lastRecvTime time.Time + lastSessionID string + lastRecvTime time.Time chWrite chan bool chDispatch chan bool @@ -537,7 +550,10 @@ func (c *LbsPeer) serve() { } func (c *LbsPeer) SendMessage(msg *LbsMessage) { - glog.V(2).Infof("\t->%v %v \n", c.Address(), msg) + logger.Debug("lobby -> client", + zap.String("addr", c.Address()), + zap.Any("msg", msg), + ) c.mOutbuf.Lock() c.outbuf = append(c.outbuf, msg.Serialize()...) c.mOutbuf.Unlock() @@ -565,11 +581,11 @@ func (c *LbsPeer) readLoop(ctx context.Context, cancel func()) { c.conn.SetReadDeadline(time.Now().Add(time.Second * 30)) n, err := c.conn.Read(buf) if err != nil { - glog.Infoln("TCP conn error:", err) + logger.Info("tcp read error", zap.Error(err)) return } if n == 0 { - glog.Infoln("TCP read zero") + logger.Info("tcp read zero") return } c.mInbuf.Lock() @@ -607,7 +623,7 @@ func (c *LbsPeer) writeLoop(ctx context.Context, cancel func()) { c.conn.SetWriteDeadline(time.Now().Add(time.Second * 10)) n, err := c.conn.Write(buf[sum:]) if err != nil { - glog.Errorf("%v write error: %v\n", c.Address(), err) + c.logger.Info("tcp write error", zap.Error(err)) break } sum += n @@ -635,7 +651,6 @@ func (c *LbsPeer) dispatchLoop(ctx context.Context, cancel func()) { c.inbuf = c.inbuf[n:] if msg != nil { - glog.V(2).Infof("%v %v\n", c.Address(), msg) c.app.chEvent <- eventPeerMessage{peer: c, msg: msg} } } diff --git a/gdxsv/lbs_battle.go b/gdxsv/lbs_battle.go index 7344868..2ed4641 100644 --- a/gdxsv/lbs_battle.go +++ b/gdxsv/lbs_battle.go @@ -2,11 +2,10 @@ package main import ( "fmt" + "go.uber.org/zap" "net" "strconv" "time" - - "github.com/golang/glog" ) func genBattleCode() string { @@ -53,7 +52,7 @@ func NewBattle(app *Lbs, lobbyID uint16, rule *Rule, mcsRegion string, mcsAddr s ip, port, err := splitIPPort(mcsAddr) if err != nil { - glog.Warning(err) + logger.Error("failed to parse mcs addr", zap.Error(err)) return nil } @@ -100,7 +99,7 @@ func (b *LbsBattle) NumOfEntryUsers() uint16 { func (b *LbsBattle) SetBattleServer(addr string) { ip, port, err := splitIPPort(addr) if err != nil { - glog.Error(err) + logger.Error("failed to set battle server", zap.Error(err)) return } b.ServerIP = ip diff --git a/gdxsv/lbs_handler.go b/gdxsv/lbs_handler.go index a071bfe..68434c1 100644 --- a/gdxsv/lbs_handler.go +++ b/gdxsv/lbs_handler.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "go.uber.org/zap" "hash/fnv" "io/ioutil" "math" @@ -12,7 +13,6 @@ import ( "strconv" "strings" - "github.com/golang/glog" "golang.org/x/text/encoding/japanese" "golang.org/x/text/transform" "golang.org/x/text/width" @@ -185,7 +185,7 @@ func SendServerShutDown(p *LbsPeer) { w := n.Writer() w.WriteString("
サーバがシャットダウンしました") p.SendMessage(n) - glog.Infoln("Sending ShutDown") + p.logger.Info("sending shutdown") } func StartLoginFlow(p *LbsPeer) { @@ -193,9 +193,13 @@ func StartLoginFlow(p *LbsPeer) { } var _ = register(lbsAskConnectionID, func(p *LbsPeer, m *LbsMessage) { - connID := m.Reader().ReadString() - p.lastConnectionID = connID + p.lastSessionID = m.Reader().ReadString() p.SessionID = genSessionID() + p.logger = p.logger.With( + zap.String("last_session_id", p.lastSessionID), + zap.String("session_id", p.SessionID), + ) + p.logger.Info("update session id") p.SendMessage(NewServerQuestion(lbsConnectionID).Writer(). WriteString(p.SessionID).Msg()) }) @@ -219,7 +223,7 @@ var _ = register(lbsRegulationHeader, func(p *LbsPeer, m *LbsMessage) { func sendUserList(p *LbsPeer) { account, err := getDB().GetAccountBySessionID(p.SessionID) if err != nil { - glog.Warning("failed to get account: ", p.SessionID) + p.logger.Warn("failed to get account", zap.Error(err)) p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
FAILED TO GET ACCOUNT INFO").Msg()) return @@ -227,7 +231,7 @@ func sendUserList(p *LbsPeer) { users, err := getDB().GetUserList(account.LoginKey) if err != nil { - glog.Warning("failed to get user list", account.SessionID) + p.logger.Error("failed to get user list", zap.Error(err)) p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
FAILED TO GET USER LIST").Msg()) return @@ -258,13 +262,13 @@ var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) { p.SendMessage(NewServerQuestion(lbsUserInfo1)) case 3: // The user must have valid connection_id. - if p.lastConnectionID == "" { + if p.lastSessionID == "" { p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
INVALID CONNECTION ID").Msg()) return } - account, err := getDB().GetAccountBySessionID(p.lastConnectionID) + account, err := getDB().GetAccountBySessionID(p.lastSessionID) if err != nil { p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
FAILED TO GET ACCOUNT").Msg()) @@ -281,7 +285,7 @@ var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) { sendUserList(p) default: - glog.Warning("UNSUPPORTED LOGIN TYPE", loginType) + p.logger.Info("unsupported login type", zap.Any("login_type", loginType)) p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
UNSUPPORTED LOGIN TYPE").Msg()) } @@ -305,7 +309,7 @@ var _ = register(lbsUserInfo1, func(p *LbsPeer, m *LbsMessage) { if err != nil { account, err = getDB().RegisterAccountWithLoginKey(p.Address(), loginKey) if err != nil { - glog.Error("failed to create account", err) + p.logger.Error("failed to create account", zap.Error(err)) p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
FAILED TO GET ACCOUNT INFO").Msg()) return @@ -316,7 +320,7 @@ var _ = register(lbsUserInfo1, func(p *LbsPeer, m *LbsMessage) { // Update session_id that was generated when the first request. err = getDB().LoginAccount(account, p.SessionID) if err != nil { - glog.Error("failed to login account", err) + logger.Error("failed to login account", zap.Error(err)) p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
FAILED TO LOGIN").Msg()) return @@ -332,23 +336,23 @@ var _ = register(lbsUserInfo9, func(p *LbsPeer, m *LbsMessage) { var _ = register(lbsUserRegist, func(p *LbsPeer, m *LbsMessage) { r := m.Reader() - userID := r.ReadString() // ****** + userID := r.ReadString() handleName := r.ReadShiftJISString() - glog.Infoln("UserRegist", userID, handleName) + logger.Info("create new user", zap.String("user_id", userID), zap.String("handle_name", handleName)) account, err := getDB().GetAccountBySessionID(p.SessionID) if err != nil { - glog.Errorln("failed to get account :", err, p.SessionID) + p.logger.Error("failed to get account", zap.Error(err)) p.conn.Close() return } if userID == "******" { // The peer wants to create new user. - glog.Info("register new user :", err, account.SessionID) + p.logger.Info("register new user") u, err := getDB().RegisterUser(account.LoginKey) if err != nil { - glog.Errorln("failed to register user :", err, account.SessionID) + p.logger.Error("failed to register user :", zap.Error(err)) p.conn.Close() return } @@ -357,14 +361,14 @@ var _ = register(lbsUserRegist, func(p *LbsPeer, m *LbsMessage) { u, err := getDB().GetUser(userID) if err != nil { - glog.Errorln("failed to get user :", err, userID) + logger.Error("failed to get user :", zap.Error(err), zap.String("user_id", userID)) p.conn.Close() return } err = getDB().LoginUser(u) if err != nil { - glog.Errorln("failed to login user :", err, userID) + logger.Error("failed to login user", zap.Error(err), zap.String("user_id", userID)) p.conn.Close() return } @@ -373,30 +377,30 @@ var _ = register(lbsUserRegist, func(p *LbsPeer, m *LbsMessage) { u.SessionID = p.SessionID err = getDB().UpdateUser(u) if err != nil { - glog.Errorln("failed to save user :", err, userID) + p.logger.Error("failed to update user", zap.Error(err), zap.String("user_id", userID)) p.conn.Close() return } p.DBUser = *u p.app.userPeers[p.UserID] = p + p.logger = p.logger.With(zap.String("user_id", p.UserID), zap.String("handle_name", p.Name)) p.SendMessage(NewServerAnswer(m).Writer().WriteString(userID).Msg()) }) var _ = register(lbsUserDecide, func(p *LbsPeer, m *LbsMessage) { userID := m.Reader().ReadString() - glog.Infoln("DecideUserId", userID) u, err := getDB().GetUser(userID) if err != nil { - glog.Errorln("failed to get user :", err, userID) + p.logger.Error("failed to get user", zap.String("user_id", userID), zap.Error(err)) p.conn.Close() return } err = getDB().LoginUser(u) if err != nil { - glog.Errorln("failed to login user :", err, userID) + p.logger.Error("failed to login user", zap.String("user_id", userID), zap.Error(err)) p.conn.Close() return } @@ -404,13 +408,17 @@ var _ = register(lbsUserDecide, func(p *LbsPeer, m *LbsMessage) { u.SessionID = p.SessionID err = getDB().UpdateUser(u) if err != nil { - glog.Errorln("failed to save user :", err, userID) + p.logger.Error("failed to update user", zap.String("user_id", userID), zap.Error(err)) p.conn.Close() return } p.DBUser = *u p.app.userPeers[p.UserID] = p + p.logger = p.logger.With( + zap.String("user_id", p.UserID), + zap.String("handle_name", p.Name), + ) p.SendMessage(NewServerAnswer(m).Writer().WriteString(p.UserID).Msg()) p.SendMessage(NewServerQuestion(lbsAskGameCode)) }) @@ -431,10 +439,7 @@ var _ = register(lbsAskGameCode, func(p *LbsPeer, m *LbsMessage) { case 0x0301: p.Platform = PlatformDC2 default: - glog.Warning("============================") - glog.Warning(" UNKNOWN CLIENT PLATFORM ") - glog.Warning(code) - glog.Warning("============================") + p.logger.Warn("unknown client platform", zap.Any("code", code)) p.SendMessage(NewServerNotice(lbsShutDown).Writer(). WriteString("
UNKNOWN CLIENT PLATFORM").Msg()) return @@ -482,6 +487,7 @@ var _ = register(lbsAskBattleResult, func(p *LbsPeer, m *LbsMessage) { } p.app.RegisterBattleResult(p, result) p.SendMessage(NewServerNotice(lbsLoginOk)) + p.logger.Info("login ok") }) var _ = register(lbsPostGameParameter, func(p *LbsPeer, m *LbsMessage) { @@ -505,10 +511,10 @@ var _ = register(lbsPostGameParameter, func(p *LbsPeer, m *LbsMessage) { } bin, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(buf), japanese.ShiftJIS.NewDecoder())) if err != nil { - glog.Errorln(err) + logger.Error("failed to read pilot name", zap.Error(err)) } p.PilotName = string(bin) - + p.logger = p.logger.With(zap.String("pilot_name", p.PilotName)) p.SendMessage(NewServerAnswer(m)) }) @@ -613,6 +619,7 @@ var _ = register(lbsWinLose, func(p *LbsPeer, m *LbsMessage) { Write32(userBattlePoint1). Write32(userBattlePoint2).Msg()) } else { + p.logger.Warn("unknown top rank", zap.Any("now_top_rank", nowTopRank)) p.SendMessage(NewServerAnswer(m).Writer(). Write16(uint16(1)). Write16(100). @@ -635,8 +642,7 @@ var _ = register(lbsDeviceData, func(p *LbsPeer, m *LbsMessage) { data6 := r.Read16() data7 := r.Read16() data8 := r.Read16() - glog.Info("DeviceData", - data1, data2, data3, data4, data5, data6, data7, data8) + p.logger.Sugar().Info("DeviceData", data1, data2, data3, data4, data5, data6, data7, data8) // PS2: 0 0 0 999 1 0 0 0 // DC1: 0 0 0 0 1 0 0 0 // DC2: 0 0 0 0 1 0 0 0 @@ -958,9 +964,11 @@ var _ = register(lbsSendMail, func(p *LbsPeer, m *LbsMessage) { userID := r.ReadString() comment1 := r.ReadShiftJISString() comment2 := r.ReadShiftJISString() - glog.Infoln("UserID", userID) - glog.Infoln("com1", comment1) - glog.Infoln("com2", comment2) + + logger.Info("send mail", + zap.String("to_user_id", userID), + zap.String("comment1", comment1), + zap.String("comment2", comment2)) u, ok := p.app.userPeers[userID] if !ok { @@ -980,6 +988,7 @@ var _ = register(lbsUserSite, func(p *LbsPeer, m *LbsMessage) { // TODO: Implement userID := m.Reader().ReadString() _ = userID + logger.Warn("not implemented lbsUserSite") p.SendMessage(NewServerAnswer(m).Writer(). Write16(0). Write16(1). @@ -1130,9 +1139,9 @@ var _ = register(lbsPostChatMessage, func(p *LbsPeer, m *LbsMessage) { WriteString(p.UserID). WriteString(p.Name). WriteString(text). - Write8(0). // chat_type - Write8(0). // id color - Write8(0). // handle color + Write8(0). // chat_type + Write8(0). // id color + Write8(0). // handle color Write8(0).Msg() // msg color if p.Room != nil { @@ -1164,7 +1173,7 @@ var _ = register(lbsTopRankingSuu, func(p *LbsPeer, m *LbsMessage) { // How many userPeers there is in the ranking // page: ranking kind? page := m.Reader().Read8() - glog.Infoln("page", page) + p.logger.Info("lbsTopRankingSuu", zap.Any("page", page)) n := 0 if ranking, err := getDB().GetWinCountRanking(0); err == nil { @@ -1178,7 +1187,7 @@ var _ = register(lbsTopRanking, func(p *LbsPeer, m *LbsMessage) { num1 := r.Read8() num2 := r.Read16() num3 := r.Read16() - glog.Infoln("TopRanking", num1, num2, num3) + p.logger.Sugar().Info("TopRanking", num1, num2, num3) ranking, err := getDB().GetWinCountRanking(0) if err != nil { @@ -1198,7 +1207,7 @@ var _ = register(lbsTopRanking, func(p *LbsPeer, m *LbsMessage) { // 3: Yellow // 4: Blue // 5: Purple - // 6: SkyBule + // 6: SkyBlue // 7: White rec := ranking[index] @@ -1324,7 +1333,7 @@ var _ = register(lbsAskMcsAddress, func(p *LbsPeer, m *LbsMessage) { port := p.Battle.ServerPort if ip == nil || ip.To4() == nil || port == 0 { - glog.Error("Invalid mcs address", ip, port) + logger.Error("invalid mcs address", zap.Any("ip", ip), zap.Any("port", port)) p.SendMessage(NewServerAnswer(m).SetErr()) return } @@ -1357,11 +1366,12 @@ var _ = register(lbsExtSyncSharedData, func(p *LbsPeer, m *LbsMessage) { var mcsStatus McsStatus err := json.Unmarshal(m.Reader().ReadBytes(), &mcsStatus) if err != nil { - glog.Error(err) + p.logger.Error("failed to unmarshal mcs status", zap.Error(err)) return } - glog.Infof("update mcs status: %+v", mcsStatus) + p.logger.Info( "update mcs status: %+v", zap.Any("mcs_status", mcsStatus)) + p.app.mcsPeers[mcsStatus.PublicAddr] = p p.mcsStatus = &mcsStatus SyncSharedDataMcsToLbs(&mcsStatus) diff --git a/gdxsv/lbs_lobby.go b/gdxsv/lbs_lobby.go index 64258cb..76da63c 100644 --- a/gdxsv/lbs_lobby.go +++ b/gdxsv/lbs_lobby.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "github.com/golang/glog" + "go.uber.org/zap" ) type LbsLobby struct { @@ -199,14 +199,15 @@ func (l *LbsLobby) CheckLobbyBattleStart() { if McsFuncEnabled() && l.McsRegion != "" { stat := l.app.FindMcs(l.McsRegion) if stat == nil { - glog.Info("call mcsfunc alloc:", l.McsRegion) + logger.Info("mcs status not found") GoMcsFuncAlloc(l.McsRegion) return } peer := l.app.FindMcsPeer(stat.PublicAddr) if peer == nil { - glog.Info("msc peer not found:", stat) + logger.Info("mcs peer not found") + GoMcsFuncAlloc(l.McsRegion) return } @@ -220,7 +221,7 @@ func (l *LbsLobby) CheckLobbyBattleStart() { for _, q := range participants { b.Add(q) q.Battle = b - getDB().AddBattleRecord(&BattleRecord{ + err := getDB().AddBattleRecord(&BattleRecord{ BattleCode: b.BattleCode, UserID: q.UserID, UserName: q.Name, @@ -228,6 +229,13 @@ func (l *LbsLobby) CheckLobbyBattleStart() { Players: len(participants), Aggregate: 1, }) + if err != nil { + logger.Error("AddBattleRecord failed", zap.Error(err)) + return + } + } + + for _, q := range participants { AddUserWhoIsGoingToBattle( b.BattleCode, b.McsRegion, q.UserID, q.Name, q.Team, q.SessionID) NotifyReadyBattle(q) @@ -289,13 +297,24 @@ func (l *LbsLobby) CheckRoomBattleStart() { return } - mcsAddr := conf.BattlePublicAddr + var mcsPeer *LbsPeer + var mcsAddr = conf.BattlePublicAddr + if McsFuncEnabled() && l.McsRegion != "" { stat := l.app.FindMcs(l.McsRegion) if stat == nil { GoMcsFuncAlloc(l.McsRegion) return } + + peer := l.app.FindMcsPeer(stat.PublicAddr) + if peer == nil { + logger.Info("mcs peer not found") + GoMcsFuncAlloc(l.McsRegion) + return + } + + mcsPeer = peer mcsAddr = stat.PublicAddr } @@ -304,7 +323,7 @@ func (l *LbsLobby) CheckRoomBattleStart() { for _, q := range participants { b.Add(q) q.Battle = b - getDB().AddBattleRecord(&BattleRecord{ + err := getDB().AddBattleRecord(&BattleRecord{ BattleCode: b.BattleCode, UserID: q.UserID, UserName: q.Name, @@ -312,8 +331,19 @@ func (l *LbsLobby) CheckRoomBattleStart() { Players: len(participants), Aggregate: 1, }) + if err != nil { + logger.Error("AddBattleRecord failed", zap.Error(err)) + return + } + } + + for _, q := range participants { AddUserWhoIsGoingToBattle( b.BattleCode, b.McsRegion, q.UserID, q.Name, q.Team, q.SessionID) NotifyReadyBattle(q) } + + if mcsPeer != nil { + NotifyLatestLbsStatus(mcsPeer) + } } diff --git a/gdxsv/lbs_mcsfunc.go b/gdxsv/lbs_mcsfunc.go index c075f95..8d8c56a 100644 --- a/gdxsv/lbs_mcsfunc.go +++ b/gdxsv/lbs_mcsfunc.go @@ -2,11 +2,11 @@ package main import ( "fmt" + "go.uber.org/zap" "io/ioutil" "net/http" "time" - "github.com/golang/glog" "golang.org/x/oauth2" "golang.org/x/oauth2/google" ) @@ -90,7 +90,7 @@ func McsFuncAlloc(region string) error { if err != nil { return err } - glog.Info(string(body)) + logger.Info("mcsfunc alloc", zap.ByteString("response", body)) return nil } @@ -98,7 +98,7 @@ func GoMcsFuncAlloc(region string) { go func() { err := McsFuncAlloc(region) if err != nil { - glog.Error(err) + logger.Error("mcsfunc alloc failed", zap.Error(err)) } }() } diff --git a/gdxsv/lbs_message.go b/gdxsv/lbs_message.go index d10c0cc..0babe01 100644 --- a/gdxsv/lbs_message.go +++ b/gdxsv/lbs_message.go @@ -5,11 +5,11 @@ import ( "encoding/binary" "encoding/hex" "fmt" + "go.uber.org/zap" "io/ioutil" "strings" "sync/atomic" - "github.com/golang/glog" "golang.org/x/text/encoding/japanese" "golang.org/x/text/transform" ) @@ -235,7 +235,7 @@ func (m *MessageBodyReader) ReadShiftJISString() string { m.r.Read(buf) ret, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(buf), japanese.ShiftJIS.NewDecoder())) if err != nil { - glog.Errorln(err) + logger.Error("failed to read sjis string", zap.Error(err), zap.Any("msg", m)) } return string(bytes.Trim(ret, "\x00")) } @@ -316,7 +316,8 @@ func (m *MessageBodyWriter) WriteBytes(bin []byte) *MessageBodyWriter { func (m *MessageBodyWriter) WriteString(v string) *MessageBodyWriter { ret, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(v), japanese.ShiftJIS.NewEncoder())) if err != nil { - glog.Errorln(err) + logger.Error("failed to write string", + zap.Error(err), zap.String("value", v), zap.Any("cmd", m.msg.Command)) } binary.Write(m.buf, binary.BigEndian, uint16(len(ret))) m.buf.WriteString(string(ret)) diff --git a/gdxsv/main.go b/gdxsv/main.go index c30f65f..38376f5 100644 --- a/gdxsv/main.go +++ b/gdxsv/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "io/ioutil" + "log" "math/rand" "net/http" "os" @@ -14,8 +15,8 @@ import ( "github.com/caarlos0/env" "github.com/davecgh/go-spew/spew" - "github.com/golang/glog" "github.com/jmoiron/sqlx" + "go.uber.org/zap" ) var ( @@ -29,9 +30,15 @@ var ( dump = flag.Bool("dump", false, "enable var dump to dump.txt") cpu = flag.Int("cpu", 2, "setting GOMAXPROCS") profile = flag.Int("profile", 1, "0: no profile, 1: enable http pprof, 2: enable blocking profile") + prodlog = flag.Bool("prodlog", false, "use production logging mode") mcsdelay = flag.Duration("mcsdelay", 0, "mcs room delay for network lag emulation") ) +var ( + logger *zap.Logger + sugger *zap.SugaredLogger +) + type Config struct { LobbyAddr string `env:"GDXSV_LOBBY_ADDR" envDefault:"localhost:3333"` LobbyPublicAddr string `env:"GDXSV_LOBBY_PUBLIC_ADDR" envDefault:"127.0.0.1:3333"` @@ -44,36 +51,37 @@ type Config struct { } func printHeader() { - glog.Infoln("========================================================================") - glog.Infoln(" gdxsv - Mobile Suit Gundam: Federation vs. Zeon&DX Private Game Server.") - glog.Infof(" Version: %v (%v)\n", gdxsvVersion, gdxsvRevision) - glog.Infoln("========================================================================") + fmt.Println(" ========================================================================") + fmt.Println(" gdxsv - Mobile Suit Gundam: Federation vs. Zeon&DX Private Game Server.") + fmt.Printf(" Version: %v (%v)\n", gdxsvVersion, gdxsvRevision) + fmt.Println(" ========================================================================") } func printUsage() { - glog.Info("") - glog.Info("Usage: gdxsv [lbs, mcs, initdb]") - glog.Info("") - glog.Info(" lbs: Serve lobby server and default battle server.") - glog.Info(" A lbs hosts PS2, DC1 and DC2 version, but their lobbies are separated internally.") - glog.Info("") - glog.Info(" mcs: Serve battle server.") - glog.Info(" The mcs attempts to register itself with a lbs.") - glog.Info(" When the mcs is vacant for a certain period, it will automatically end.") - glog.Info(" It is supposed to host mcs in a different location than the lobby server.") - glog.Info("") - glog.Info(" initdb: Initialize database.") - glog.Info(" It is supposed to run this command when first booting manually.") - glog.Info(" Note that if the database file already exists it will be permanently deleted.") + fmt.Print(` +Usage: gdxsv [lbs, mcs, initdb] + + lbs: Serve lobby server and default battle server. + A lbs hosts PS2, DC1 and DC2 version, but their lobbies are separated internally. + + mcs: Serve battle server. + The mcs attempts to register itself with a lbs. + When the mcs is vacant for a certain period, it will automatically end. + It is supposed to host mcs in a different location than the lobby server. + + initdb: Initialize database. + It is supposed to run this command when first booting manually. + Note that if the database file already exists it will be permanently deleted. +`) } func loadConfig() { var c Config if err := env.Parse(&c); err != nil { - glog.Fatal(err) + logger.Fatal("config load failed", zap.Error(err)) } - glog.Infof("%+v", c) + logger.Info("config loaded", zap.Any("config", c)) conf = c } @@ -94,12 +102,14 @@ func prepareOption(command string) { go func() { port := pprofPort(command) addr := fmt.Sprintf(":%v", port) - glog.Errorln(http.ListenAndServe(addr, nil)) + err := http.ListenAndServe(addr, nil) + logger.Error("http.ListenAndServe error", zap.Error(err), zap.String("addr", addr)) }() } if *profile >= 2 { runtime.MemProfileRate = 1 runtime.SetBlockProfileRate(1) + logger.Warn("mem profile mode enabled") } } @@ -112,7 +122,7 @@ func getDB() DB { func prepareDB() { conn, err := sqlx.Open("sqlite3", conf.DBName) if err != nil { - glog.Fatal(err) + logger.Fatal("failed to open database", zap.Error(err)) } defaultdb = SQLiteDB{ @@ -130,6 +140,8 @@ func mainLbs() { go mcs.ListenAndServe(stripHost(conf.BattleAddr)) defer mcs.Quit() + logger.Sugar() + c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) if *dump { @@ -157,19 +169,30 @@ func mainMcs() { err := mcs.DialAndSyncWithLbs(conf.LobbyPublicAddr, conf.BattlePublicAddr, conf.BattleRegion) if err != nil { - glog.Error(err) + logger.Error("failed to dial lbs", zap.Error(err)) } } func main() { - flag.Set("logtostderr", "true") + printHeader() flag.Parse() - rand.Seed(time.Now().UnixNano()) - printHeader() + var err error + if *prodlog { + logger, err = zap.NewDevelopment() + } else { + logger, err = zap.NewProduction() + logger = logger.With(zap.String("version", gdxsvVersion), zap.String("revision", gdxsvRevision)) + } + if err != nil { + log.Fatalf("Failed to setup logger: %v", err) + } + defer logger.Sync() + + logger.Info("gdxsv", zap.String("version", gdxsvVersion), zap.String("revision", gdxsvRevision)) + rand.Seed(time.Now().UnixNano()) args := flag.Args() - glog.Infoln(args, len(args)) if len(args) < 1 { printUsage() diff --git a/gdxsv/mcs.go b/gdxsv/mcs.go index b658db3..5f544f4 100644 --- a/gdxsv/mcs.go +++ b/gdxsv/mcs.go @@ -2,13 +2,12 @@ package main import ( "encoding/json" - "fmt" "gdxsv/gdxsv/proto" + "go.uber.org/zap" "net" "sync" "time" - "github.com/golang/glog" "golang.org/x/net/context" ) @@ -25,6 +24,7 @@ type McsPeer interface { AddSendMessage(*proto.BattleMessage) Address() string Close() error + Logger() *zap.Logger } type BaseMcsPeer struct { @@ -32,6 +32,7 @@ type BaseMcsPeer struct { userID string roomID string position int + logger *zap.Logger } func (p *BaseMcsPeer) SetUserID(userID string) { @@ -66,6 +67,10 @@ func (p *BaseMcsPeer) McsRoomID() string { return p.roomID } +func (p *BaseMcsPeer) Logger() *zap.Logger { + return p.logger +} + type Mcs struct { mtx sync.Mutex updated time.Time @@ -82,7 +87,7 @@ func NewMcs(delay time.Duration) *Mcs { } func (mcs *Mcs) ListenAndServe(addr string) error { - glog.Info("mcs.ListenAndServe", addr) + logger.Info("mcs.ListenAndServe", zap.String("addr", addr)) tcpSv := NewTCPServer(mcs) return tcpSv.ListenAndServe(addr) } @@ -102,16 +107,18 @@ func (mcs *Mcs) DialAndSyncWithLbs(lobbyAddr string, battlePublicAddr string, ba } sendMcsStatus := func() error { - glog.Info("Send Status", status) statusBin, err := json.MarshalIndent(status, "", " ") if err != nil { + zap.Error(err) return err } + logger.Info("send status to lbs", zap.ByteString("status", statusBin)) buf := NewServerNotice(lbsExtSyncSharedData).Writer().WriteBytes(statusBin).Msg().Serialize() for sum := 0; sum < len(buf); { conn.SetWriteDeadline(time.Now().Add(time.Second)) n, err := conn.Write(buf[sum:]) if err != nil { + logger.Error("send status to lbs failed", zap.Error(err)) return err } sum += n @@ -141,7 +148,7 @@ func (mcs *Mcs) DialAndSyncWithLbs(lobbyAddr string, battlePublicAddr string, ba n, err := conn.Read(buf) if err != nil { - glog.Error(err) + logger.Error("read from lbs failed", zap.Error(err)) return } data = append(data, buf[:n]...) @@ -157,9 +164,14 @@ func (mcs *Mcs) DialAndSyncWithLbs(lobbyAddr string, battlePublicAddr string, ba if msg != nil { switch msg.Command { case lbsExtSyncSharedData: - glog.Info("Recv lbsExtSyncSharedData") + lbsStatusBin := msg.Reader().ReadBytes() + logger.Info("recv lbs status", zap.ByteString("status", lbsStatusBin)) var lbsStatus LbsStatus - json.Unmarshal(msg.Reader().ReadBytes(), &lbsStatus) + err = json.Unmarshal(lbsStatusBin, &lbsStatus) + if err != nil { + logger.Error("json.Unmarshal", zap.Error(err)) + continue + } SyncSharedDataLbsToMcs(&lbsStatus) } } @@ -182,7 +194,7 @@ func (mcs *Mcs) DialAndSyncWithLbs(lobbyAddr string, battlePublicAddr string, ba } if 15 <= time.Since(status.Updated).Minutes() && len(status.Users) == 0 { - fmt.Println("mcs exit") + logger.Info("mcs exit") return nil } } @@ -200,7 +212,7 @@ func (mcs *Mcs) LastUpdated() time.Time { return t } -func (m *Mcs) Join(p McsPeer, sessionID string) *McsRoom { +func (mcs *Mcs) Join(p McsPeer, sessionID string) *McsRoom { user, ok := getBattleUserInfo(sessionID) if !ok { return nil @@ -209,33 +221,22 @@ func (m *Mcs) Join(p McsPeer, sessionID string) *McsRoom { p.SetUserID(user.UserID) p.SetSessionID(sessionID) - m.mtx.Lock() - m.updated = time.Now() - room := m.rooms[user.BattleCode] + mcs.mtx.Lock() + mcs.updated = time.Now() + room := mcs.rooms[user.BattleCode] if room == nil { - room = newMcsRoom(m, user.BattleCode) - m.rooms[user.BattleCode] = room + room = newMcsRoom(mcs, user.BattleCode) + mcs.rooms[user.BattleCode] = room } - m.mtx.Unlock() + mcs.mtx.Unlock() room.Join(p) return room } -func (m *Mcs) OnMcsRoomClose(room *McsRoom) { - m.mtx.Lock() - m.updated = time.Now() - delete(m.rooms, room.battleCode) - m.mtx.Unlock() +func (mcs *Mcs) OnMcsRoomClose(room *McsRoom) { + mcs.mtx.Lock() + mcs.updated = time.Now() + delete(mcs.rooms, room.battleCode) + mcs.mtx.Unlock() removeBattleUserInfo(room.battleCode) } - -func IsFinData(buf []byte) bool { - if len(buf) == 4 && - buf[0] == 4 && - buf[1] == 240 && - buf[2] == 0 && - buf[3] == 0 { - return true - } - return false -} diff --git a/gdxsv/mcs_room.go b/gdxsv/mcs_room.go index dd5ee97..05d13a2 100644 --- a/gdxsv/mcs_room.go +++ b/gdxsv/mcs_room.go @@ -1,11 +1,9 @@ package main import ( - "encoding/hex" + "go.uber.org/zap" "sync" - "github.com/golang/glog" - "gdxsv/gdxsv/proto" ) @@ -39,10 +37,12 @@ func (r *McsRoom) SendMessage(peer McsPeer, msg *proto.BattleMessage) { other := r.peers[i] if other != nil { - if glog.V(2) { - glog.Infof("[ROOM] %v>%v %v", peer.UserID(), other.UserID(), hex.EncodeToString(msg.GetBody())) - } other.AddSendMessage(msg) + logger.Debug("relay", + zap.String("from_user", peer.UserID()), + zap.String("to_user", other.UserID()), + zap.Uint32("seq", msg.GetSeq()), + zap.Binary("data", msg.GetBody())) } } r.RUnlock() @@ -84,5 +84,6 @@ func (r *McsRoom) Leave(p McsPeer) { r.Dispose() } - glog.Infof("leave peer %v", p.Address()) + logger.Info("leave peer") } + diff --git a/gdxsv/mcs_tcp.go b/gdxsv/mcs_tcp.go index 1226d3b..3174a4b 100644 --- a/gdxsv/mcs_tcp.go +++ b/gdxsv/mcs_tcp.go @@ -2,12 +2,12 @@ package main import ( "encoding/hex" + "go.uber.org/zap" "io" "net" "sync" "time" - "github.com/golang/glog" pb "github.com/golang/protobuf/proto" "gdxsv/gdxsv/proto" @@ -26,12 +26,12 @@ func (s *McsTCPServer) ListenAndServe(addr string) error { if err != nil { return err } - listner, err := net.ListenTCP("tcp", tcpAddr) + listener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { return err } for { - conn, err := listner.AcceptTCP() + conn, err := listener.AcceptTCP() if err != nil { continue } @@ -52,10 +52,15 @@ type McsTCPPeer struct { } func NewTCPPeer(conn *net.TCPConn) *McsTCPPeer { - return &McsTCPPeer{ + p := &McsTCPPeer{ conn: conn, seq: 1, } + p.logger = logger.With( + zap.String("proto", "tcp"), + zap.String("addr", conn.RemoteAddr().String()), + ) + return p } func (u *McsTCPPeer) Close() error { @@ -63,8 +68,8 @@ func (u *McsTCPPeer) Close() error { } func (u *McsTCPPeer) Serve(mcs *Mcs) { - glog.Infoln("[TCP]", u.Address(), "Serve Start") - defer glog.Infoln("[TCP]", u.Address(), "Serve End") + u.logger.Info("Serve Start") + defer u.logger.Info("Serve End") // c.f. ps2 symbol ReflectMsg // 6X := category? // 1031 := request connection ID @@ -89,7 +94,7 @@ func (u *McsTCPPeer) AddSendData(data []byte) { for sum := 0; sum < len(data); { n, err := u.conn.Write(data[sum:]) if err != nil { - glog.Errorf("%v write error: %v\n", u.Address(), err) + u.logger.Warn("write error", zap.Error(err)) break } sum += n @@ -115,7 +120,7 @@ func (u *McsTCPPeer) readLoop(mcs *Mcs) { if err != nil { if err != io.EOF { - glog.Errorf("%v read error: %v\n", u.Address(), err) + u.logger.Error("read failed", zap.Error(err)) } return } @@ -125,21 +130,20 @@ func (u *McsTCPPeer) readLoop(mcs *Mcs) { inbuf = append(inbuf, buf[:n]...) if u.room == nil { - glog.Infoln("room nil: ", u.Address()) if len(inbuf) < 20 { continue } // 14 30 00 00 00 08 99 88 00 ff ff ff 35 39 31 32 39 32 36 39 sessionID := string(inbuf[12:20]) inbuf = inbuf[:0] - glog.Infoln("[TCP] SessionID", sessionID, err) + u.logger.Info("recv first message", zap.String("session_id", sessionID), zap.Binary("first_data", inbuf[:20])) u.room = mcs.Join(u, sessionID) if u.room == nil { - glog.Infoln("failed to join room: ", u.UserID(), u.Address()) + u.logger.Error("failed to join room") u.conn.Close() break } - glog.Infoln("join success", u.Address()) + u.logger.Info("entered a room") } else { var tmp []byte for 0 < len(inbuf) { @@ -158,7 +162,19 @@ func (u *McsTCPPeer) readLoop(mcs *Mcs) { msg.Seq = pb.Uint32(u.seq) u.seq++ u.room.SendMessage(u, msg) + proto.PutBattleMessage(msg) } } } } + +func IsFinData(buf []byte) bool { + if len(buf) == 4 && + buf[0] == 4 && + buf[1] == 240 && + buf[2] == 0 && + buf[3] == 0 { + return true + } + return false +} diff --git a/gdxsv/shareddata.go b/gdxsv/shareddata.go index 7ba2446..2e9291f 100644 --- a/gdxsv/shareddata.go +++ b/gdxsv/shareddata.go @@ -2,10 +2,9 @@ package main import ( "encoding/json" + "go.uber.org/zap" "sync" "time" - - "github.com/golang/glog" ) // sharing temporary data between lbs and mcs @@ -99,7 +98,7 @@ func GetMcsUsers() []McsUser { func NotifyLatestLbsStatus(mcs *LbsPeer) { lbsStatusBin, err := json.Marshal(&LbsStatus{Users: GetMcsUsers()}) if err != nil { - glog.Error(err) + logger.Error("json.Marshal", zap.Error(err)) return } mcs.SendMessage(NewServerNotice(lbsExtSyncSharedData).Writer().WriteBytes(lbsStatusBin).Msg()) diff --git a/go.mod b/go.mod index e661688..eb6b21e 100644 --- a/go.mod +++ b/go.mod @@ -5,13 +5,13 @@ go 1.14 require ( cloud.google.com/go v0.38.0 // indirect github.com/caarlos0/env v3.5.0+incompatible - github.com/davecgh/go-spew v1.1.0 - github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b + github.com/davecgh/go-spew v1.1.1 github.com/golang/protobuf v1.3.2 github.com/jmoiron/sqlx v1.2.0 github.com/mattn/go-sqlite3 v2.0.3+incompatible github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.5.1 // indirect + go.uber.org/zap v1.15.0 golang.org/x/net v0.0.0-20190923162816-aa69164e4478 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 golang.org/x/text v0.3.2 diff --git a/go.sum b/go.sum index 386dc38..2ce3dff 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,15 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yixi/rBrKyJs= github.com/caarlos0/env v3.5.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= @@ -23,36 +26,61 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U= github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= +go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A= +go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM= +go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -63,8 +91,10 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= @@ -74,7 +104,13 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IK golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -86,7 +122,12 @@ google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRn google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= From 10bcdf9e0357adfe636f3efe32387a24d73e71a1 Mon Sep 17 00:00:00 2001 From: Shingo INADA Date: Mon, 4 May 2020 01:50:59 +0900 Subject: [PATCH 2/4] update logger setting --- gdxsv/lbs.go | 4 ++-- gdxsv/main.go | 46 ++++++++++++++++++++++++++++++++++++++-------- go.mod | 1 + go.sum | 2 ++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/gdxsv/lbs.go b/gdxsv/lbs.go index 632f008..fee4aaf 100644 --- a/gdxsv/lbs.go +++ b/gdxsv/lbs.go @@ -80,7 +80,7 @@ func (lbs *Lbs) GetLobby(platform uint8, lobbyID uint16) *LbsLobby { } func (lbs *Lbs) ListenAndServe(addr string) error { - logger.Info("ListenAndServe", zap.String("addr", addr)) + logger.Info("lbs.ListenAndServe", zap.String("addr", addr)) tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { @@ -158,7 +158,7 @@ func (lbs *Lbs) Quit() { SendServerShutDown(p) } }) - time.Sleep(1000 * time.Millisecond) + time.Sleep(200 * time.Millisecond) close(lbs.chQuit) } diff --git a/gdxsv/main.go b/gdxsv/main.go index 38376f5..5aaaaa9 100644 --- a/gdxsv/main.go +++ b/gdxsv/main.go @@ -16,6 +16,7 @@ import ( "github.com/caarlos0/env" "github.com/davecgh/go-spew/spew" "github.com/jmoiron/sqlx" + "github.com/tommy351/zap-stackdriver" "go.uber.org/zap" ) @@ -31,6 +32,7 @@ var ( cpu = flag.Int("cpu", 2, "setting GOMAXPROCS") profile = flag.Int("profile", 1, "0: no profile, 1: enable http pprof, 2: enable blocking profile") prodlog = flag.Bool("prodlog", false, "use production logging mode") + loglevel = flag.Int("v", 2, "logging level. 1:error, 2:info, 3:debug") mcsdelay = flag.Duration("mcsdelay", 0, "mcs room delay for network lag emulation") ) @@ -173,23 +175,51 @@ func mainMcs() { } } -func main() { - printHeader() - flag.Parse() - +func prepareLogger() { var err error + var zapConfig zap.Config + if *prodlog { - logger, err = zap.NewDevelopment() + zapConfig = zap.NewProductionConfig() + zapConfig.EncoderConfig = stackdriver.EncoderConfig } else { - logger, err = zap.NewProduction() - logger = logger.With(zap.String("version", gdxsvVersion), zap.String("revision", gdxsvRevision)) + zapConfig = zap.NewDevelopmentConfig() + zapConfig.Encoding = "console" } + + switch *loglevel { + case 0: + zapConfig.Level = zap.NewAtomicLevelAt(zap.ErrorLevel) + case 1: + zapConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel) + case 2: + zapConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel) + default: + zapConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel) + } + + logger, err = zapConfig.Build() if err != nil { log.Fatalf("Failed to setup logger: %v", err) } + + if *prodlog { + logger = logger.With( + zap.String("gdxsv_version", gdxsvVersion), + zap.String("gdxsv_revision", gdxsvRevision)) + } +} + +func main() { + printHeader() + flag.Parse() + + prepareLogger() defer logger.Sync() - logger.Info("gdxsv", zap.String("version", gdxsvVersion), zap.String("revision", gdxsvRevision)) + logger.Info("hello gdxsv", + zap.String("gdxsv_version", gdxsvVersion), + zap.String("gdxsv_revision", gdxsvRevision)) rand.Seed(time.Now().UnixNano()) args := flag.Args() diff --git a/go.mod b/go.mod index eb6b21e..ab06891 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/mattn/go-sqlite3 v2.0.3+incompatible github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.5.1 // indirect + github.com/tommy351/zap-stackdriver v0.1.3 go.uber.org/zap v1.15.0 golang.org/x/net v0.0.0-20190923162816-aa69164e4478 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 diff --git a/go.sum b/go.sum index 2ce3dff..31ed542 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/tommy351/zap-stackdriver v0.1.3 h1:luDjvQeLLYFwuTdlPsKl6w68aJwrhUKReV2wa0iLrIc= +github.com/tommy351/zap-stackdriver v0.1.3/go.mod h1:02SLb1eIv1QhUBnZZW6VBY9jlHYzekvxsSRXcEy2gRA= go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= From c81ae13d40b3043933d493f2da3318fb2c2de6a1 Mon Sep 17 00:00:00 2001 From: Shingo INADA Date: Mon, 4 May 2020 07:40:31 +0900 Subject: [PATCH 3/4] update startup script --- infra/mcsfunc/index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/infra/mcsfunc/index.js b/infra/mcsfunc/index.js index 1f947d5..4a97e96 100644 --- a/infra/mcsfunc/index.js +++ b/infra/mcsfunc/index.js @@ -51,6 +51,19 @@ if grep -xqFe 'ubuntu ALL=NOPASSWD: /sbin/shutdown' /etc/sudoers; then echo 'ubuntu ALL=NOPASSWD: /sbin/shutdown' >> /etc/sudoers fi +cat << 'EOF' > /etc/google-fluentd/config.d/gdxsv.conf + + @type tail + format json + path /var/log/gdxsv.log + pos_file /var/lib/google-fluentd/pos/gdxsv.pos + read_from_head true + tag gdxsv-mcs + +EOF +sudo systemctl restart google-fluentd + + cat << 'EOF' > /home/ubuntu/launch-mcs.sh #!/bin/bash -eux @@ -84,12 +97,15 @@ export GDXSV_LOBBY_PUBLIC_ADDR=zdxsv.net:9876 export GDXSV_BATTLE_ADDR=:9877 export GDXSV_BATTLE_REGION=$(basename $(curl -s -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/zone)) export GDXSV_BATTLE_PUBLIC_ADDR=$(curl -s https://ipinfo.io/ip):9877 -$TAG_NAME/bin/gdxsv mcs + +touch /var/log/gdxsv-mcs.log +truncate -s0 /var/log/gdxsv-mcs.log + +$TAG_NAME/bin/gdxsv -prodlog mcs 2>&1 > /var/log/gdxsv-mcs.log EOF chmod +x /home/ubuntu/launch-mcs.sh -su ubuntu -c 'cd /home/ubuntu && nohup ./launch-mcs.sh 2>&1 | logger &' - +su ubuntu -c 'cd /home/ubuntu && nohup ./launch-mcs.sh &' echo "startup-script done" ` } From c2d8751df2fc7ac1f1055345f1c5fe35d2342b82 Mon Sep 17 00:00:00 2001 From: Shingo INADA Date: Mon, 4 May 2020 07:41:52 +0900 Subject: [PATCH 4/4] update startup script --- infra/mcsfunc/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/mcsfunc/index.js b/infra/mcsfunc/index.js index 4a97e96..543947d 100644 --- a/infra/mcsfunc/index.js +++ b/infra/mcsfunc/index.js @@ -101,7 +101,7 @@ export GDXSV_BATTLE_PUBLIC_ADDR=$(curl -s https://ipinfo.io/ip):9877 touch /var/log/gdxsv-mcs.log truncate -s0 /var/log/gdxsv-mcs.log -$TAG_NAME/bin/gdxsv -prodlog mcs 2>&1 > /var/log/gdxsv-mcs.log +$TAG_NAME/bin/gdxsv -prodlog mcs > /var/log/gdxsv-mcs.log 2>&1 EOF chmod +x /home/ubuntu/launch-mcs.sh