logger.cpp

来自「2009 ROBOCUP 仿真2DSERVER 源码」· C++ 代码 · 共 1,363 行 · 第 1/3 页

CPP
1,363 行
字号
        if ( ! M_stadium.teamLeft().olcoach()->name().empty() )        {            team_name_score += M_stadium.teamLeft().olcoach()->name();            team_name_score += "_";        }        team_name_score += boost::lexical_cast< std::string >( M_stadium.teamLeft().point() );        if ( bAddPenaltyScore )        {            team_name_score += "_";            team_name_score += boost::lexical_cast< std::string >( M_stadium.teamLeft().penaltyPoint() );            team_name_score += ( M_stadium.teamLeft().penaltyWon() ? "w" : "" );        }    }    else    {        team_name_score += "null";    }    team_name_score += "-vs-";    if ( M_stadium.teamRight().enabled() )    {        team_name_score += M_stadium.teamRight().name();        team_name_score += "_";        if ( ! M_stadium.teamRight().olcoach()->name().empty() )        {            team_name_score += M_stadium.teamRight().olcoach()->name();            team_name_score += "_";        }        team_name_score += boost::lexical_cast< std::string >( M_stadium.teamRight().point() );        if ( bAddPenaltyScore )        {            team_name_score += "_";            team_name_score += boost::lexical_cast< std::string >( M_stadium.teamRight().penaltyPoint() );            team_name_score += ( M_stadium.teamRight().penaltyWon() ? "w" : "" );        }    }    else    {        team_name_score += "null";    }    //    // write result    //    if ( isGameLogOpen() )    {        char time_stamp[32];        std::strftime( time_stamp, 32,                       "%Y%m%d%H%M",                       &( M_stadium.realTime() ) );        char msg[256];        snprintf( msg, 256, "(result %s %s)", time_stamp, team_name_score.c_str() );        writeMsgToGameLog( MSG_BOARD, msg, true );    }    //    // rename text log    //    if ( isTextLogOpen()         && ! ServerParam::instance().textLogFixed() )    {        std::string newname = ServerParam::instance().textLogDir();        if ( *newname.rbegin() != '/' )        {            newname += '/';        }        if ( ServerParam::instance().textLogDated() )        {            newname += time_str;        }        newname += team_name_score;        newname += Logger::DEF_TEXT_SUFFIX;        if ( ServerParam::instance().textLogCompression() > 0 )        {            newname += ".gz";        }        closeTextLog();        if ( std::rename( M_text_log_name.c_str(),                          newname.c_str() ) )        {            std::cerr << __FILE__ << ": " << __LINE__                      << ": error renaming " << M_text_log_name << std::endl;        }        M_text_log_name = newname;    }    //    // rename game log    //    if ( isGameLogOpen()         && ! ServerParam::instance().gameLogFixed() )    {        std::string newname = ServerParam::instance().gameLogDir();        if ( *newname.rbegin() != '/' )        {            newname += '/';        }        if ( ServerParam::instance().gameLogDated() )        {            newname += time_str;        }        newname += team_name_score;        newname += Logger::DEF_GAME_SUFFIX;        if ( ServerParam::instance().gameLogCompression() > 0 )        {            newname += ".gz";        }        closeGameLog();        if ( std::rename( M_game_log_name.c_str(),                          newname.c_str() ) )        {            std::cerr << __FILE__ << ": " << __LINE__                      << ": error renaming " << M_game_log_name << std::endl;        }        M_game_log_name = newname;    }    //    // rename keepaway log    //    if ( M_kaway_log.is_open()         && ! ServerParam::instance().kawayLogFixed() )    {        std::string newname = ServerParam::instance().kawayLogDir();        if ( *newname.rbegin() != '/' )        {            newname += '/';        }        if ( ServerParam::instance().kawayLogDated() )        {            newname += time_str;        }        newname += team_name_score;        newname += Logger::DEF_KAWAY_SUFFIX;        closeKawayLog();        if( std::rename( M_kaway_log_name.c_str(),                         newname.c_str() ) )        {            std::cerr << __FILE__ << ": " << __LINE__                      << ": error renaming " << M_kaway_log_name << std::endl;        }    }}voidLogger::writeToGameLog( const char * str,                        const std::streamsize n ){    if ( M_game_log )    {        M_game_log->write( str, n );    }}voidLogger::writeMsgToGameLog( const BoardType board_type,                           const char * msg,                           const bool compulsion ){    if ( ! isGameLogOpen() )    {        return;    }    if ( ServerParam::instance().gameLogVersion() >= REC_VERSION_4 )    {        if ( compulsion             || ServerParam::instance().recordMessages() )        {            std::string str( msg );            std::replace( str.begin(), str.end(), '\n', ' ' );            *M_game_log << "(msg " << M_stadium.time()                        << ' ' << board_type << " \"" << str << "\")\n";        }    }    else if ( ServerParam::instance().gameLogVersion() != REC_OLD_VERSION )    {        if ( compulsion             || ServerParam::instance().recordMessages() )        {            Int16 mode = htons( MSG_MODE );            writeToGameLog( reinterpret_cast< const char * >( &mode ),                            sizeof( mode ) );            Int16 board = htons( board_type );            writeToGameLog( reinterpret_cast< const char * >( &board ),                            sizeof( board ) );            // calculate the string length and write it            const Int16 max_len = std::min( Int16( std::strlen( msg ) ), Int16( 2048 ) );            Int16 len = 1;            while ( (msg[len-1] != '\0') && (len < max_len) )            {                ++len;            }            /* pfr 1/7/00 Need to put length in network byte order */            Int16 nlen = htons( len );            writeToGameLog( reinterpret_cast< const char* >( &nlen ),                            sizeof( nlen ) );            // write the message            writeToGameLog( msg, len );        }    }    else    {        dispinfo_t disp;        disp.mode = htons( MSG_MODE );        disp.body.msg.board = htons( board_type );        std::strncpy( disp.body.msg.message,                      msg,                      std::min( sizeof( disp.body.msg.message ),                                std::strlen( msg ) ) );        writeToGameLog( reinterpret_cast< const char * >( &disp ),                        sizeof( dispinfo_t ) );    }}voidLogger::writeGameLog(){    /* TH - 2-NOV-2000 */    static bool wrote_final_cycle = false;    if ( ! isGameLogOpen() )    {        return;    }    if ( M_stadium.playmode() != PM_BeforeKickOff         && M_stadium.playmode() != PM_TimeOver )    {        writeGameLogImpl();//         switch ( ServerParam::instance().gameLogVersion() ) {//         case REC_VERSION_4://             writeGameLogV4();//             break;//         case REC_VERSION_3://             writeGameLogV3();//             break;//         case REC_VERSION_2://             writeGameLogV2();//             break;//         case REC_OLD_VERSION://             writeGameLogV1();//             break;//         default://             break;//         }    }    else if ( M_stadium.playmode() == PM_TimeOver              && ! wrote_final_cycle )    {        writeGameLogImpl();//         switch ( ServerParam::instance().gameLogVersion() ) {//         case REC_VERSION_4://             writeGameLogV4();//             break;//         case REC_VERSION_3://             writeGameLogV3();//             break;//         case REC_VERSION_2://             writeGameLogV2();//             break;//         case REC_OLD_VERSION://             writeGameLogV1();//             break;//         default://             break;//         }        wrote_final_cycle = true;    }}voidLogger::writeGameLogImpl(){    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;   // if playmode has changed wirte playmode    if ( pm != M_stadium.playmode() )    {        pm = M_stadium.playmode();        M_init_observer->sendPlayMode();    }    // 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();        M_init_observer->sendTeam();    }    M_observer->sendShow();}// TODO: replaced with DispSendervoidLogger::writeGameLogV1(){    dispinfo_t disp;    disp.mode = htons( SHOW_MODE );    disp.body.show.time = htons( M_stadium.time() );    disp.body.show.pmode = static_cast< char >( M_stadium.playmode() );    disp.body.show.pos[0].x = htons( (Int16)rint( ( M_stadium.ball().pos().x * SHOWINFO_SCALE ) ) );    disp.body.show.pos[0].y = htons( (Int16)rint( ( M_stadium.ball().pos().y * SHOWINFO_SCALE ) ) );    const Stadium::PlayerCont & players = M_stadium.players();    for ( int i = 0; i < MAX_PLAYER * 2; ++i )    {        disp.body.show.pos[i+1].enable = htons( players[i]->state() );        disp.body.show.pos[i+1].x = htons( (Int16)rint( players[i]->pos().x * SHOWINFO_SCALE ) );        disp.body.show.pos[i+1].y = htons( (Int16)rint( players[i]->pos().y * SHOWINFO_SCALE ) );        disp.body.show.pos[i+1].angle = htons( (Int16)Rad2IDegRound( players[i]->angleBodyCommitted() ) );        disp.body.show.pos[i+1].side = htons( players[i]->side() );        disp.body.show.pos[i+1].unum = htons( players[i]->unum() );    }    if ( ! M_stadium.teamLeft().name().empty() )    {        std::strncpy( disp.body.show.team[0].name,                      M_stadium.teamLeft().name().c_str(),                      std::min( M_stadium.teamLeft().name().length() + 1, size_t( 16 ) ) );        disp.body.show.team[0].score = htons( M_stadium.teamLeft().point() );    }    else    {        std::memset( &disp.body.show.team[0].name,                     0,                     sizeof( disp.body.show.team[0].name ) );    }    if ( ! M_stadium.teamRight().name().empty() )    {        std::strncpy( disp.body.show.team[1].name,                      M_stadium.teamRight().name().c_str(),                      std::min( M_stadium.teamRight().name().length() + 1, size_t( 16 ) ) );        disp.body.show.team[1].score = htons( M_stadium.teamRight().point() );    }    else    {        std::memset( &disp.body.show.team[1].name,                     0,                     sizeof( disp.body.show.team[1].name ) );    }    writeToGameLog( reinterpret_cast< const char * >( &disp ),                    sizeof( dispinfo_t ) );}// TODO: replaced with DispSendervoidLogger::writeGameLogV2(){    Int16 mode = htons( SHOW_MODE );    showinfo_t show;    show.time = htons( M_stadium.time() );    show.pmode = static_cast< char >( M_stadium.playmode() );    show.pos[0].x = htons( (Int16)rint( ( M_stadium.ball().pos().x * SHOWINFO_SCALE ) ) );    show.pos[0].y = htons( (Int16)rint( ( M_stadium.ball().pos().y * SHOWINFO_SCALE ) ) );    const Stadium::PlayerCont & players = M_stadium.players();    for ( int i = 0; i < MAX_PLAYER * 2; ++i )    {        show.pos[i+1].enable = htons( players[i]->state() );        show.pos[i+1].x = htons( (Int16)rint( players[i]->pos().x * SHOWINFO_SCALE ) );        show.pos[i+1].y = htons( (Int16)rint( players[i]->pos().y * SHOWINFO_SCALE ) );        show.pos[i+1].angle = htons( (Int16)Rad2IDegRound( players[i]->angleBodyCommitted() ) );        show.pos[i+1].side = htons( players[i]->side() );        show.pos[i+1].unum = htons( players[i]->unum() );    }    if ( ! M_stadium.teamLeft().name().empty() )    {        std::strncpy( show.team[0].name,                      M_stadium.teamLeft().name().c_str(),                      std::min( M_stadium.teamLeft().name().length() + 1, size_t( 16 ) ) );        show.team[0].score = htons( M_stadium.teamLeft().point() );    }    else    {        std::memset( &show.team[0].name,                     0,                     sizeof( show.team[0].name ) );        show.team[0].score = htons( 0 );    }    if ( ! M_stadium.teamRight().name().empty() )    {        std::strncpy( show.team[1].name,                      M_stadium.teamRight().name().c_str(),                      std::min( M_stadium.teamRight().name().length() + 1, size_t( 16 ) ) );        show.team[1].score = htons( M_stadium.teamRight().point() );    }    else    {        std::memset( &show.team[1].name,                     0,                     sizeof( show.team[1].name ) );        show.team[1].score = htons( 0 );    }    // write a show struct    writeToGameLog( reinterpret_cast< const char * >( &mode ),                    sizeof( Int16 ) );    writeToGameLog( reinterpret_cast< const char * >( &show ),                    sizeof( showinfo_t ) );}// TODO: replaced with DispSendervoidLogger::writeGameLogV3(){    static PlayMode pmode = PM_Null;    static team_t teams[2] = { { "", 0 },                               { "", 0 } };    static int score_l = 0;    static int score_r = 0;    Int16 mode;    // if playmode has changed wirte playmode    if ( pmode != M_stadium.playmode() )    {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?