/* Log to the logfile only. */ void cLog::log( eLogLevel loglevel, cUOSocket* sock, const QString& string, bool timestamp ) { if ( !( Config::instance()->logMask() & loglevel ) ) { return; } // -> Log Event cPythonScript *globalHook = ScriptManager::instance()->getGlobalHook(EVENT_LOG); if (globalHook && globalHook->canHandleEvent(EVENT_LOG)) { PyObject *args = Py_BuildValue("(iNNO)", (unsigned int)loglevel, PyGetSocketObject(sock), QString2Python(string), Py_None ); bool result = globalHook->callEventHandler(EVENT_LOG, args); Py_DECREF(args); if (result) { return; } } if ( !checkLogFile() ) return; // Timestamp the data QTime now = QTime::currentTime(); QString prelude; if ( timestamp || loglevel == LOG_PYTHON ) { prelude.sprintf( "%02u:%02u:", now.hour(), now.minute() ); if ( sock ) prelude.append( QString( "%1:" ).arg( sock->uniqueId(), 0, 16 ) ); } // LogLevel switch ( loglevel ) { case LOG_ERROR: prelude.append( " ERROR: " ); break; case LOG_WARNING: prelude.append( " WARNING: " ); break; case LOG_PYTHON: prelude.append( " PYTHON: " ); break; default: prelude.append( " " ); } QByteArray utfdata = string.toUtf8(); utfdata.prepend( prelude.toUtf8() ); logfile.write( utfdata ); logfile.flush(); }
void Log_File::log (const char * msg,int size) { int length=0; if(size<=0) length=strlen(msg); else length=size; checkLogFile(); if(m_Fp_Log!=NULL) { fwrite(msg,length,1,m_Fp_Log); fflush(m_Fp_Log); m_curSize+=length; } checkLogFile(); }
void logger::OpenFile(string const& path) { filePath = path; //检查文件是否存在 fstream file; file.open(path + SUFFIX_LOG, ios::in | ios::out | ios::binary); if (!file) { cerr << "No file!" << endl; exit(EXIT_FAILURE); } //检查文件大小是否正确 int begin = file.tellg(); file.seekg(0, ios::end); int end = file.tellg(); if ((end - begin) < 4) { cerr << "Size error!" << endl; exit(EXIT_FAILURE); } //初始化logger类中的fileSize和xChecksum fileSize = end - begin; file.seekp(0, ios::beg); file.read((char*)(&xChecksum), 4);//读取文件中存储的xChecksum file.close(); //对log文件大小进行核查 Rewind(); uint32_t xChecksum_tmp = checkLogFile(); Rewind(); //文件大小与xChecksum记录的大小不一致,删除文件 if (xChecksum != xChecksum_tmp) { cerr << "xChecksum error!" << endl << "Bad log file!" << endl; //是否Truncate? 是的,Truncate! // TODO //由于更新xCheckSum的时候数据库发生崩溃, 则会导致整个log文件不能使用. //所以暂时放弃xCheckSum, 之后将xCheckSum改为由booter管理. fstream file(path + SUFFIX_LOG, ios::trunc); if (file.is_open()) { file.close(); } exit(EXIT_FAILURE); } }
int main(int argc, char *argv[]) { if (1 >= argc) { background = true; } else { background = false; } if ( background ) { pid_t pid = fork(); if (pid < 0) { cerr << "fork: unable to fork" << endl; return -1; } if (pid ) //parent { exit(0); } } checkLogFile(); setup_signal_handlers(); pid_t pidt = getpid(); gOsLog<<"main thread pid="<<(int)pidt<<endl; printSysTime (); // bSysStop = false; pthread_t tidListen = 0; if (pthread_create(&tidListen, 0, (void *(*)(void*))&listenThread, 0)) { gOsLog<<"create gk thread fail!"<<endl; exit(0); } // //signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); // main loop for dialogic SR event while(!bSysStop) { sleep(10); if (1 >= argc) { checkLogFile(); } } pthread_join(tidListen, NULL); //closeSys(); printSysTime (); gOsLog<<"exit from the main thread!"<<endl; return 0; }