logger.cpp
来自「2009 ROBOCUP 仿真2DSERVER 源码」· C++ 代码 · 共 1,363 行 · 第 1/3 页
CPP
1,363 行
pmode = M_stadium.playmode(); char pm = static_cast< char >( pmode ); mode = htons( PM_MODE ); writeToGameLog( reinterpret_cast< const char * >( &mode ), sizeof( mode ) ); writeToGameLog( reinterpret_cast< const char * >( &pm ), sizeof( char ) ); } // if teams or score has changed, write teams and score if ( M_stadium.teamLeft().point() != score_l || M_stadium.teamRight().point() != score_r || M_stadium.teamLeft().name() != teams[ 0 ].name || M_stadium.teamRight().name() != teams[ 1 ].name ) { score_l = M_stadium.teamLeft().point(); score_r = M_stadium.teamLeft().point(); std::strncpy( teams[ 0 ].name, M_stadium.teamLeft().name().c_str(), std::min( M_stadium.teamLeft().name().length() + 1, size_t( 16 ) ) ); teams[ 0 ].score = htons( score_l ); std::strncpy( teams[ 1 ].name, M_stadium.teamRight().name().c_str(), std::min( M_stadium.teamRight().name().length() + 1, size_t( 16 ) ) ); teams[ 1 ].score = htons( score_r ); mode = htons( TEAM_MODE ); writeToGameLog( reinterpret_cast< const char * >( &mode ), sizeof( mode ) ); writeToGameLog( reinterpret_cast< const char * >( teams ), sizeof( team_t ) * 2 ); } // write positional data short_showinfo_t2 show; show.time = htons( M_stadium.time() ); show.ball.x = htonl( (Int32)rint( ( M_stadium.ball().pos().x * SHOWINFO_SCALE2 ) ) ); show.ball.y = htonl( (Int32)rint( ( M_stadium.ball().pos().y * SHOWINFO_SCALE2 ) ) ); show.ball.deltax = htonl( (Int32)rint( ( M_stadium.ball().vel().x * SHOWINFO_SCALE2 ) ) ); show.ball.deltay = htonl( (Int32)rint( ( M_stadium.ball().vel().y * SHOWINFO_SCALE2) ) ); const Stadium::PlayerCont & players = M_stadium.players(); for ( int i = 0; i < MAX_PLAYER * 2; ++i ) { show.pos[i].mode = htons( players[i]->state() ); show.pos[i].type = htons( players[i]->playerTypeId() ); show.pos[i].x = htonl( (Int32)rint( players[i]->pos().x * SHOWINFO_SCALE2 ) ); show.pos[i].y = htonl( (Int32)rint( players[i]->pos().y * SHOWINFO_SCALE2 ) ); show.pos[i].deltax = htonl( (Int32)rint( players[i]->vel().x * SHOWINFO_SCALE2 ) ); show.pos[i].deltay = htonl( (Int32)rint( players[i]->vel().y * SHOWINFO_SCALE2 ) ); show.pos[i].body_angle = htonl( (Int32)rint( players[i]->angleBodyCommitted() * SHOWINFO_SCALE2 ) ); show.pos[i].head_angle = htonl( (Int32)rint( players[i]->angleNeckCommitted() * SHOWINFO_SCALE2 ) ); show.pos[i].view_width = htonl( (Int32)rint( players[i]->visibleAngle() * SHOWINFO_SCALE2 ) ); show.pos[i].view_quality = htons( (Int16)rint( players[i]->highquality() ) ); show.pos[i].stamina = htonl( (Int32)rint( players[i]->stamina() * SHOWINFO_SCALE2 ) ); show.pos[i].effort = htonl( (Int32)rint( players[i]->effort() * SHOWINFO_SCALE2 ) ); show.pos[i].recovery = htonl( (Int32)rint( players[i]->recovery() * SHOWINFO_SCALE2 ) ); show.pos[i].kick_count = htons( players[i]->kickCount() ); show.pos[i].dash_count = htons( players[i]->dashCount() ); show.pos[i].turn_count = htons( players[i]->turnCount() ); show.pos[i].say_count = htons( players[i]->sayCount() ); show.pos[i].tneck_count = htons( players[i]->turnNeckCount() ); show.pos[i].catch_count = htons( players[i]->catchCount() ); show.pos[i].move_count = htons( players[i]->moveCount() ); show.pos[i].chg_view_count = htons( players[i]->changeViewCount() ); } mode = htons( SHOW_MODE ); writeToGameLog( reinterpret_cast< const char * >( &mode ), sizeof( mode ) ); writeToGameLog( reinterpret_cast< const char * >( &show ), sizeof( short_showinfo_t2 ) );}// TODO: replaced with DispSendervoidLogger::writeGameLogV4(){ static const char * playmode_strings[] = PLAYMODE_STRINGS; static PlayMode pm = PM_Null; static std::string team_l_name, team_r_name; static int team_l_score = 0, team_r_score = 0; static int team_l_pen_taken = 0, team_r_pen_taken = 0; const double prec = 0.0001; const double dprec = 0.001; std::ostream & os = *M_game_log; // if playmode has changed wirte playmode if ( pm != M_stadium.playmode() ) { pm = M_stadium.playmode(); os << "(playmode " << M_stadium.time() << ' ' << playmode_strings[pm] << ")\n"; } // if teams or score has changed, write teams and score if ( team_l_score != M_stadium.teamLeft().point() || team_r_score != M_stadium.teamRight().point() || team_l_pen_taken != M_stadium.teamLeft().penaltyTaken() || team_r_pen_taken != M_stadium.teamRight().penaltyTaken() || M_stadium.teamLeft().name() != team_l_name || M_stadium.teamRight().name() != team_r_name ) { team_l_name = M_stadium.teamLeft().name(); team_r_name = M_stadium.teamRight().name(); team_l_score = M_stadium.teamLeft().point(); team_r_score = M_stadium.teamRight().point(); team_l_pen_taken = M_stadium.teamLeft().penaltyTaken(); team_r_pen_taken = M_stadium.teamRight().penaltyTaken(); os << "(team " << M_stadium.time() << ' ' << ( team_l_name.empty() ? "null" : team_l_name.c_str() ) << ' ' << ( team_r_name.empty() ? "null" : team_r_name.c_str() ) << ' ' << team_l_score << ' ' << team_r_score; if ( team_l_pen_taken > 0 || team_r_pen_taken > 0 ) { os << ' ' << M_stadium.teamLeft().penaltyPoint() << ' ' << team_l_pen_taken - M_stadium.teamLeft().penaltyPoint() << ' ' << M_stadium.teamRight().penaltyPoint() << ' ' << team_r_pen_taken - M_stadium.teamRight().penaltyPoint(); } os << ")\n"; } os << "(show " << M_stadium.time(); os << " (" << BALL_NAME_SHORT << ' ' << Quantize( M_stadium.ball().pos().x, prec ) << ' ' << Quantize( M_stadium.ball().pos().y, prec ) << ' ' << Quantize( M_stadium.ball().vel().x, prec ) << ' ' << Quantize( M_stadium.ball().vel().y, prec ) << ')'; const Stadium::PlayerCont::const_iterator end = M_stadium.players().end(); for ( Stadium::PlayerCont::const_iterator p = M_stadium.players().begin(); p != end; ++p ) { os << " ("; os << "(" << SideStr( (*p)->side() ) << ' ' << (*p)->unum() << ')'; os << ' ' << (*p)->playerTypeId() << ' ' << std::hex << std::showbase << (*p)->state() // include goalie flag << std::dec << std::noshowbase; os << ' ' << Quantize( (*p)->pos().x, prec ) << ' ' << Quantize( (*p)->pos().y, prec ) << ' ' << Quantize( (*p)->vel().x, prec ) << ' ' << Quantize( (*p)->vel().y, prec ) << ' ' << Quantize( Rad2Deg( (*p)->angleBodyCommitted() ), dprec ) << ' ' << Quantize( Rad2Deg( (*p)->angleNeckCommitted() ), dprec ); if ( (*p)->arm().isPointing() ) { os << ' ' << Quantize( (*p)->arm().dest().getX(), prec ) << ' ' << Quantize( (*p)->arm().dest().getY(), prec );// rcss::geom::Vector2D arm_dest;// if ( (*p)->arm().getRelDest( rcss::geom::Vector2D( (*p)->pos().x,// (*p)->pos().y ),// 0.0,// arm_dest ) )// {// os << ' ' << Quantize( arm_dest.getMag(), prec )// << ' ' << Quantize( Rad2Deg( arm_dest.getHead() ), dprec );// } } os << " (v " << ( (*p)->highquality() ? "h " : "l " ) << Quantize( Rad2Deg( (*p)->visibleAngle() ), dprec ) << ')'; os << " (s " << (*p)->stamina() << ' ' << (*p)->effort() << ' ' << (*p)->recovery() << ')'; if ( (*p)->state() != DISABLE && (*p)->getFocusTarget() != NULL ) { os << " (f " << SideStr( (*p)->getFocusTarget()->side() ) << ' ' << (*p)->getFocusTarget()->unum() << ')'; } os << " (c " << (*p)->kickCount() << ' ' << (*p)->dashCount() << ' ' << (*p)->turnCount() << ' ' << (*p)->catchCount() << ' ' << (*p)->moveCount() << ' ' << (*p)->turnNeckCount() << ' ' << (*p)->changeViewCount() << ' ' << (*p)->sayCount() << ' ' << (*p)->tackleCount() << ' ' << (*p)->arm().getCounter() << ' ' << (*p)->attentiontoCount() << ')'; os << ')'; // end of player } os << ")\n";}voidLogger::writeTextLog( const char * message, const TextLogFlag flag ){ if ( flag == RECV || flag == LOG_TEXT ) { if ( isTextLogOpen() ) { *M_text_log << M_stadium.time() << ',' << M_stadium.stoppageTime() << "\t" << message << '\n'; } } if ( ( flag == RECV || flag == SUBS ) && isGameLogOpen() && ServerParam::instance().recordMessages() && M_stadium.playmode() != PM_TimeOver ) { char buf[max_message_length_for_display]; std::strncpy( buf, message, std::min( max_message_length_for_display, (int)std::strlen( message ) ) ); writeMsgToGameLog( LOG_BOARD, buf ); }// if ( ( flag == RECV || flag == SUBS )// && ServerParam::instance().sendComms() )// {// char buf[max_message_length_for_display];// std::strncpy( buf, message, std::min( max_message_length_for_display,// (int)std::strlen( message ) ) );// for ( Stadium::MonitorCont::iterator i = M_stadium.monitors().begin();// i != M_stadium.monitors().end();// ++i )// {// (*i)->sendMsg( LOG_BOARD, buf );// }// }}voidLogger::writePlayerLog( const Player & player, const char * message, const TextLogFlag flag ){ if ( isTextLogOpen() || ( flag == RECV && isGameLogOpen() ) || ( flag == RECV && ServerParam::instance().sendComms() ) ) { char tmp[MaxMesg]; snprintf( tmp, MaxMesg, "%s %s_%d: %s", ( flag == SEND ? "Send" : "Recv" ), player.team()->name().c_str(), player.unum(), message ); writeTextLog( tmp, flag ); }}voidLogger::writeCoachLog( const char * message, const TextLogFlag flag ){ if ( isTextLogOpen() || ( flag == RECV && isGameLogOpen() ) || ( flag == RECV && ServerParam::instance().sendComms() ) ) { char tmp[MaxMesg]; snprintf( tmp, MaxMesg, "%s Coach: %s", ( flag == SEND ? "Send" : "Recv" ), message ) ; writeTextLog( tmp, flag ); }}voidLogger::writeOnlineCoachLog( const OnlineCoach & coach, const char * message, const TextLogFlag flag ){ if ( isTextLogOpen() || ( flag == RECV && isGameLogOpen() ) || ( flag == RECV && ServerParam::instance().sendComms() ) ) { char tmp[MaxMesg]; snprintf( tmp, MaxMesg, "%s %s_Coach: %s", ( flag == SEND ? "Send" : "Recv" ), ( coach.side() == LEFT ? M_stadium.teamLeft().name().c_str() : M_stadium.teamRight().name().c_str() ), message ); writeTextLog( tmp, flag ); }}voidLogger::writeRefereeAudio( const char * msg ){ if ( isTextLogOpen() || ( isGameLogOpen() && M_stadium.playmode() != PM_BeforeKickOff && M_stadium.playmode() != PM_TimeOver ) ) { char buf[max_message_length_for_display]; snprintf( buf, max_message_length_for_display, "(%s %s)", REFEREE_NAME, msg ); if ( isTextLogOpen() ) { writeTextLog( buf, LOG_TEXT ); } if ( isGameLogOpen() ) { writeMsgToGameLog( MSG_BOARD, buf ); } }}voidLogger::writePlayerAudio( const Player & player, const char * msg ){ if ( isGameLogOpen() && ServerParam::instance().recordMessages() && M_stadium.playmode() != PM_BeforeKickOff && M_stadium.playmode() != PM_TimeOver ) { char buf[max_message_length_for_display]; snprintf( buf, max_message_length_for_display, "(%s %s)", player.name().c_str(), msg ); writeMsgToGameLog( MSG_BOARD, buf ); }}voidLogger::writeCoachAudio( const Coach & coach, const char * msg ){ if ( isGameLogOpen() && ServerParam::instance().recordMessages() && M_stadium.playmode() != PM_BeforeKickOff && M_stadium.playmode() != PM_TimeOver ) { char buf[max_message_length_for_display]; char format[40]; snprintf( format, 40, "(%%s %%.%ds)", max_message_length_for_display - (MaxMesg - MaxCoachMesg) ); snprintf( buf, max_message_length_for_display, format, ( coach.side() == RIGHT ? OLCOACH_NAME_R : coach.side() == LEFT ? OLCOACH_NAME_L : REFEREE_NAME ), msg ); writeMsgToGameLog( MSG_BOARD, buf ); }}voidLogger::writeCoachStdAudio( const OnlineCoach & coach, const rcss::clang::Msg & msg ){ if ( isGameLogOpen() && ServerParam::instance().recordMessages() && M_stadium.playmode() != PM_BeforeKickOff && M_stadium.playmode() != PM_TimeOver ) { std::ostringstream coach_mess; coach_mess << msg.getTimeRecv() << " "; msg.print( coach_mess ); char buf[max_message_length_for_display]; char format[40]; snprintf( format, 40, "(%%s %%.%ds)", max_message_length_for_display - (MaxMesg - MaxCoachMesg)); snprintf( buf, max_message_length_for_display, format, (coach.side() == RIGHT) ? OLCOACH_NAME_R : OLCOACH_NAME_L, coach_mess.str().c_str() ); writeMsgToGameLog( MSG_BOARD, buf ); }}voidLogger::writeTimes( const timeval & old_time, const timeval & new_time ){ if ( isTextLogOpen() && ServerParam::instance().logTimes() ) { double diff = (new_time.tv_sec - old_time.tv_sec) * 1000 + (double)(new_time.tv_usec - old_time.tv_usec) / 1000; *M_text_log << M_stadium.time() << ',' << M_stadium.stoppageTime() << "\tCYCLE_TIMES: " << diff << '\n'; }}voidLogger::writeProfile( const timeval & start_time, const timeval & end_time, const char * str ){ if ( isTextLogOpen() && ServerParam::instance().profile() ) { double diff = (end_time.tv_sec - start_time.tv_sec) * 1000 + (double)(end_time.tv_usec - start_time.tv_usec) / 1000; *M_text_log << M_stadium.time() << ',' << M_stadium.stoppageTime() << "\t" << str << ": " << diff << '\n'; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?