better date parsing method
This commit is contained in:
parent
9b8fa371ea
commit
c260e0744e
|
@ -1,4 +1,4 @@
|
||||||
CC=gcc
|
CC=gcc -Wall
|
||||||
CFLAGS=-g -I.
|
CFLAGS=-g -I.
|
||||||
DEPS=
|
DEPS=
|
||||||
OBJ=main.o
|
OBJ=main.o
|
||||||
|
|
24
src/main.c
24
src/main.c
|
@ -54,26 +54,14 @@ int parse_message(struct Message* result, char* message) {
|
||||||
if (num_digits == 0) return 1; // empty priority <> ?
|
if (num_digits == 0) return 1; // empty priority <> ?
|
||||||
result->priority = atoi(digits);
|
result->priority = atoi(digits);
|
||||||
|
|
||||||
|
|
||||||
// Parse date
|
// Parse date
|
||||||
// May 10 03:09:59 filterlog: 5,,,....
|
// May 10 03:09:59 filterlog: 5,,,....
|
||||||
char month[8];
|
char month[8];
|
||||||
memset(&month, '\0', sizeof(month)); // makes valgrind happy as the above char contains uninitialized memory
|
memset(&month, '\0', sizeof(month)); // makes valgrind happy as the above char contains uninitialized memory
|
||||||
int day, hour, minute, second;
|
int day, hour, minute, second, date_length;
|
||||||
{
|
if(sscanf(message + position, "%s %d %d:%d:%d%n", month, &day, &hour, &minute, &second, &date_length) != 5) {
|
||||||
int msg_len = strlen(message);
|
return 1; // Failed to parse all desired fields
|
||||||
char message_fromdate[4096];
|
}
|
||||||
memset(&message_fromdate, '\0', sizeof(message_fromdate));
|
|
||||||
memcpy(message_fromdate, &message[position], msg_len - position);
|
|
||||||
if(sscanf(message_fromdate, "%s %d %d:%d:%d", month, &day, &hour, &minute, &second) != 5) { // TODO replace this BS with %n like below
|
|
||||||
return 1; // Failed to parse all desired fields
|
|
||||||
}
|
|
||||||
} // frees message_fromdate, but is this pointless?
|
|
||||||
|
|
||||||
// Advance position by finding the length of the date
|
|
||||||
char datestr[24];
|
|
||||||
int date_length = sprintf(datestr, "%s %d %02d:%02d:%02d", month, day, hour, minute, second);
|
|
||||||
assert(date_length > 0);
|
|
||||||
position += date_length + 1; // position now at beginning of HOSTNAME field
|
position += date_length + 1; // position now at beginning of HOSTNAME field
|
||||||
|
|
||||||
// char msg_remaining[4096];
|
// char msg_remaining[4096];
|
||||||
|
@ -84,7 +72,6 @@ int parse_message(struct Message* result, char* message) {
|
||||||
// memmove(message, &message[position], strlen(message) - position);
|
// memmove(message, &message[position], strlen(message) - position);
|
||||||
// printf("'%s'\n", message);
|
// printf("'%s'\n", message);
|
||||||
|
|
||||||
|
|
||||||
// Parse APPLICATION
|
// Parse APPLICATION
|
||||||
// filterlog: 5,,,1000000103,cpsw0,match....
|
// filterlog: 5,,,1000000103,cpsw0,match....
|
||||||
char application[64]; // TODO check max length
|
char application[64]; // TODO check max length
|
||||||
|
@ -96,13 +83,12 @@ int parse_message(struct Message* result, char* message) {
|
||||||
memcpy(result->application, application, sizeof(application));
|
memcpy(result->application, application, sizeof(application));
|
||||||
|
|
||||||
position += app_length + 1;
|
position += app_length + 1;
|
||||||
printf("remaining: '%s'\n", message + position + 1);
|
|
||||||
|
|
||||||
|
printf("remaining: '%s'\n", message + position);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
struct sockaddr_in my_addr, my_peer_addr;
|
struct sockaddr_in my_addr, my_peer_addr;
|
||||||
|
|
Loading…
Reference in New Issue