#include #include #include #include #include #include #include "bulletin_broad_show.h" #include "mine_business.h" #include #include "log.h" bulletin_broad_show::bulletin_broad_show() { } bulletin_broad_show* bulletin_broad_show::inst() { static bulletin_broad_show mb; return &mb; } void bulletin_broad_show::OnInit(config_file * config) { if (config == nullptr) { return; } //公告牌显示设置 m_bulletin_board_time = config->get("bulletinboard.showtime",1*60); std::string tmpSz = config->get("bulletinboard.show_filepath","../log/bulletin"); set_bulletin_board_path(tmpSz); m_bulletin_board_file_name = config->get("bulletinboard.show_filename","bulletin.txt"); } void bulletin_broad_show::CreateDirectoryEx(const std::string & sPathName ) { std::vector vecSegTag; boost::split(vecSegTag, sPathName, boost::is_any_of("/")); if (vecSegTag.size() == 1) //数据发生错误 { return; } std::string curPath = ""; for (int i = 0 ; i < (int)vecSegTag.size() ; i++) { curPath += vecSegTag[i]; if(vecSegTag[i].length() > 1 && vecSegTag[i][0] != '.' ) { int a = access(curPath.c_str(), F_OK); if(a == -1) { mkdir(curPath.c_str(),0755); } } curPath += "/"; } } void bulletin_broad_show::run_bulletin_board() { time_t t = std::time(nullptr); if (t - m_lastshow_bulletin_time > m_bulletin_board_time ) { //把当前文件写到文件中 m_lastshow_bulletin_time = t; uint32_t num = mine_business::inst()->get_mine_display_staff_num(); std::string path = m_bulletin_board_path; static bool g_bCreatePath = false; if (!g_bCreatePath) { CreateDirectoryEx(path); //创建目录 g_bCreatePath = true; } path += "/" + m_bulletin_board_file_name; std::ofstream f1(path.c_str());//打开文件用于写,若文件不存在就创建它 if(!f1.is_open()) { return;//打开文件失败则结束运行 } std::string szText = "井下当前总人数: " + std::to_string(num) + "人"; f1 << szText << std::endl; f1.close(); //关闭文件 log_info("show_bulletin_board : %s" ,szText.c_str()); } } void bulletin_broad_show::set_bulletin_board_path(std::string & path) { if (path.empty()) return; for(unsigned i = path.length() - 1 ; i > 0 ;i--) { if ( path[i] == '/') path.pop_back(); else break; } m_bulletin_board_path = path; }