int swServer_create(swServer *serv) { //EOF最大长度为8字节 if (serv->package_eof_len > sizeof(serv->package_eof)) { serv->package_eof_len = sizeof(serv->package_eof); } //初始化日志 if(serv->log_file[0] != 0) { swLog_init(serv->log_file); } //保存指针到全局变量中去 //TODO 未来全部使用此方式访问swServer/swFactory对象 SwooleG.serv = serv; SwooleG.factory = &serv->factory; //单进程单线程模式 if (serv->factory_mode == SW_MODE_SINGLE) { return swReactorProcess_create(serv); } else { return swReactorThread_create(serv); } }
void swLog_put(int level, char *cnt) { const char *level_str; char date_str[SW_LOG_DATE_STRLEN]; char log_str[SW_LOG_BUFFER_SIZE]; int n; switch (level) { case SW_LOG_DEBUG: level_str = "DEBUG"; break; case SW_LOG_NOTICE: level_str = "NOTICE"; break; case SW_LOG_ERROR: level_str = "ERROR"; break; case SW_LOG_WARNING: level_str = "WARNING"; break; case SW_LOG_TRACE: level_str = "TRACE"; break; default: level_str = "INFO"; break; } time_t t; struct tm *p; t = time(NULL); p = localtime(&t); snprintf(date_str, SW_LOG_DATE_STRLEN, "%d-%02d-%02d %02d:%02d:%02d", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec); char process_flag = '@'; int process_id = 0; switch(SwooleG.process_type) { case SW_PROCESS_MASTER: process_flag = '#'; process_id = SwooleTG.id; break; case SW_PROCESS_MANAGER: process_flag = '$'; break; case SW_PROCESS_WORKER: process_flag = '*'; process_id = SwooleWG.id; break; case SW_PROCESS_TASKWORKER: process_flag = '^'; process_id = SwooleWG.id; break; default: break; } n = snprintf(log_str, SW_LOG_BUFFER_SIZE, "[%s %c%d.%d]\t%s\t%s\n", date_str, process_flag, SwooleG.pid, process_id, level_str, cnt); //reopen log file if (SwooleG.log_file && SwooleWG.logfile_version < SwooleGS->logfile_version) { close(SwooleG.log_fd); swLog_init(SwooleG.log_file); SwooleWG.logfile_version = SwooleGS->logfile_version; } if (write(SwooleG.log_fd, log_str, n) < 0) { printf("write(log_fd, size=%d) failed. Error: %s[%d].\n", n, strerror(errno), errno); } }