在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
下面是前段时间做项目用到的一个输出LOG日志的代码,每个文件最多记录10W行,超过10W行,将当前日志文件重命名,重新再打开。直接看代码吧,很简单!int InitTCpRtpLog() { char log[256]; mkdir(TCP_RTP_LOG_PATH, 0755); snprintf(log, sizeof(log), "%s/%s", TCP_RTP_LOG_PATH, TCP_RTP_LOG_FILE); vTcpRtpLogHandle = fopen((char*)log, "a"); if (vTcpRtpLogHandle){ PrintMsgTrace("###%s(),creat log file %s sucessfull!\n",__FUNCTION__, log); return 0; }else{ PrintMsgTrace("###$s(), Fail openning event log file (errno=%s)\n", __FUNCTION__, strerror(errno)); return - 1; } } static int _rebuildTcpRtpLogFiles() { char tmp[256]; char tmp2[256]; char date[128]; time_t now; struct tm ptm; if (vTcpRtpLogHandle) { fclose(vTcpRtpLogHandle); time(&now); localtime_r(&now,&ptm); strftime((char *)date, 128, "_%F_%T", &ptm); snprintf(tmp, sizeof(tmp), "%s/%s", TCP_RTP_LOG_PATH, TCP_RTP_LOG_FILE); snprintf(tmp2, sizeof(tmp2), "%s_%s.log", tmp,date); remove(tmp2); rename(tmp, tmp2); vTcpRtpLogHandle = fopen((char*)tmp, "a"); } return 1; } void LogTcpRtp(char *p_fmt, ...) { char date[256]; time_t now; struct tm ptm; va_list ap; if (!vTcpRtpLogHandle){ return; } pthread_mutex_lock(&_vTcpRtpLogMutex); time(&now); // Gets the system time if (localtime_r(&now, &ptm)) { strftime(date, sizeof (date), "%F %T", &ptm); fprintf(vTcpRtpLogHandle, "[ %s ]:", date); va_start(ap, p_fmt); vfprintf(vTcpRtpLogHandle, p_fmt, ap); va_end(ap); fflush(vTcpRtpLogHandle); _vCurTcpRtpLogNum++; if (_vCurTcpRtpLogNum > MAX_RTP_TCP_NUMBER){ _rebuildTcpRtpLogFiles(); _vCurTcpRtpLogNum = 0; } } pthread_mutex_unlock(&_vTcpRtpLogMutex); }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论