我收到一条消息
<14>2012-04-19T03:54:18+08:00.527800 server3000 status: 00|00|The average CPU, memory, disk usage and total network flow are 5.0%, 5.0%, 5.0% and 5 bytes respectively for the last hour.\n
如何通过函数
syslog
获取单词2012-04-19,03:54:18+08:00.527800,server3000,status,00,00 and 5.0
?#include <stdio.h>
#include <stdlib.h>
int main(void) {
int t1,t2 = 0;
char programname[20], deviceip[16], date[11], time[9];
char logmsg[30];
const char * s = "<14>2012-04-19T03:54:18.527800+08:00 server3000 status: 00|00|The average CPU, memory, disk usage and total network flow are 5.0%, 5.0%, 5.0% and 5 bytes respectively for the last hour";
sscanf(s, "<%*d>%[^T]T%[^.].%*[^ ] %[^ ] %[^:]: %d|%d|%[^\n]", date, time,deviceip, programname, &t1, &t2, logmsg);
return 0;
}
最佳答案
您可以保存sscanf
的结果并检查其值:
int rc = sscanf(...);
if(rc < 7)
// failed to match or extract
sscanf
在代码中返回7,这意味着格式字符串匹配并提取了所有7个参数。关于c - 通过sscanf解析系统日志消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10223267/