123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #include <stdio.h>
- #include "event_log.h"
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <iostream>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <time.h>
- #include <sstream>
- void CEventLog::Start()
- {
- if(access("./log/",F_OK)!=0)
- {
- int status = mkdir("./log/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
-
- if (status == 0) {
- std::cout << "文件夹创建成功!" << std::endl;
- } else {
- std::cout << "文件夹创建失败!" << std::endl;
- }
- }
- std::stringstream oss;
- oss<<"./log/"<<GetLocalTime()<<".log";
- std::string str=oss.str();
- _ofile.open(str,std::ios_base::out);
- _thread=std::thread(&CEventLog::Run,this);
- // _ofile.open()
- }
- std::string CEventLog::GetLocalTime()
- {
- time_t t=time(nullptr);
- tm * ld=localtime(&t);
- char tmp[128];
- strftime(tmp,sizeof(tmp),"%Y-%m-%d-%H-%M-%S",ld);
- return std::string(tmp);
- }
- void CEventLog::Run()
- {
- _run=true;
- while(_run)
- {
- EventType type;
- int32_t steer,accel,brake;
- {
- std::lock_guard<std::mutex> l(_mutex);
- type=_type;
- steer=_steer;
- accel=_accel;
- brake=_brake;
-
- }
- if(type!=EventType::NORMAL)
- {
- std::string s;
- switch(_type)
- {
- case EventType::EMERGENCY:
- s= "碰撞临界:";
- break;
- case EventType::OFFLINE:
- s="车辆掉线:";
- break;
- case EventType::LOW_RPM:
- s="车辆熄火:";
- break;
- };
- std::stringstream oss;
- oss<<GetLocalTime()<<": "<<s<<" 方向盘速度:"<<_steer<<", 油门值:"<<_accel<<", 刹车值:"<<_brake;
- std::string str=oss.str();
- _ofile<<str.c_str()<<std::endl;
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- }
- void CEventLog::Stop()
- {
- _ofile.close();
- _run=false;
- _thread.join();
- }
- void CEventLog::SetEvent(EventType type, int32_t steer,int32_t accel,int32_t brake)
- {
- std::lock_guard<std::mutex> l(_mutex);
- _type=type;
- _steer=steer;
- _accel=accel;
- _brake=brake;
- }
-
|