2

I got a syslog message

<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

How can I get the word 2012-04-19,03:54:18+08:00.527800,server3000,status,00,00 and 5.0 through the function sscanf?

#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;
}
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
EricDai
  • 25
  • 3

1 Answers1

0

You may like to save the result of sscanf and check its value:

int rc = sscanf(...);
if(rc < 7)
    // failed to match or extract

sscanf in your code returns 7, meaning that your format string matched and extracted all 7 arguments.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271