int backup_master::init(const char *db_name, const char* ip, const char * user, const char* psw) { // glog InitGoogleLogging("backup"); SetLogDestination(INFO,"./log/info_"); SetLogDestination(WARNING,"./log/error_"); SetLogDestination(FATAL,""); // check user id if (getuid() != 0) { std::cout << "erro root privileges needed" << std::endl; LOG(WARNING) << "erro root privileges needed "; FlushLogFiles(INFO); return BK_ERROR; } // init pref if (m_pref.login_db(db_name, ip, user, psw) == BK_SUCESS && m_pref.check() == BK_SUCESS) { POSTMESSAGENODATA(CONFIG_COMPLETE); return BK_SUCESS; } POSTMESSAGENODATA(CONFIG_COMPLETE); return BK_ERROR; }
int main() { InitGoogleLogging("backup"); SetLogDestination(INFO,"info_"); SetLogDestination(WARNING,""); SetLogDestination(FATAL,""); LOG(INFO) << "llll"; testmemoryleak(); int addTask = 0; thread_pool* pool = thread_pool::getInstance(); // Initialize the threadpool. pool->initialize(2, 4); printf("Thread pool Initialized with %d threads.\n", pool->getActiveThreads()); // system("pause"); // Add tasks to our threadpool. for(int i = 0; i < 500; ++i) { SampleTask* data = new SampleTask(i); if(i%2==0) pool->add_task(Sample1, data); else pool->add_task(Sample2, data); } while(true) { // This needs to be done in the main program loop, // or no tasks will be processed. pool->process(); // Simulates active adding to the thread pool // during the main program loop #if defined(_DEBUG) addTask = rand()%(500); if(addTask==7) pool->add_task(Sample1, new SampleTask(-1)); else if(addTask==26) pool->add_task(Sample1, new SampleTask(-2)); #endif // Check to see if all tasks are completed. This is // only for this sample program. In others, the // pool will be shut down once the program exits. if(pool->getActiveThreads() == pool->getNumDormantThreads()) break; //// Cut-off in the middle of execution //if(GetAsyncKeyState(VK_ESCAPE)) // break; } // Fin pool->shut_down(); printf("Thread Pool has finished.\n"); // system("pause"); return 0; }
// 初始化日志 void CLog::InitLog(const char* argv0, const char* dir) { // 获取不含路径和后缀名的程序名 const char* slash = strrchr(argv0, '/'); #ifdef _WIN32 if (!slash) slash = strrchr(argv0, '\\'); #endif slash = slash ? slash + 1 : argv0; char buf[1024] = { 0 }; char* p = strrchr((char*)slash, '.'); if( p ) strncpy(buf, slash, (p-slash)); else strcpy(buf, slash); // 初始化Google glog InitGoogleLogging(buf); // 设置日志路径 SetLogDir(dir); // 日志文件只写一个,所以取消Info以外的文件 SetLogDestination(FATAL, ""); SetLogDestination(ERROR, ""); SetLogDestination(WARN, ""); // 完整的日志文件名称: <short program name>.<hostname>.log.<date>-<time>.<pid> // 拼接日志文件名的前部:<short program name>.<hostname>.log. string hostname; GetHostName(&hostname); *(buf+strlen(buf)) = '.'; strcpy(buf+strlen(buf), hostname.c_str()); strcpy(buf+strlen(buf), ".log."); // 设置INFO级别的日志文件前缀 if( 0 != strcmp(dir, "./")) { string tmp = dir; tmp += buf; SetLogDestination(INFO, tmp.c_str()); } else SetLogDestination(INFO, buf); // 默认情况下WARN级别的日志会屏幕输出 SetStderrThreshold(WARN); // 日志是否写屏幕( Debug写,Release不写 ) //SetAlsoLogToStderr(m_bIsDebug); SetLogToStderr(false); SetAlsoLogToStderr(false); }