parse application

This commit is contained in:
dave 2018-05-09 23:12:17 -07:00
parent 2ffbdcea7f
commit 9b8fa371ea
1 changed files with 27 additions and 8 deletions

View File

@ -19,6 +19,7 @@ void panic(const char* s) {
struct Message { struct Message {
int priority; int priority;
char application[64];
}; };
int parse_message(struct Message* result, char* message) { int parse_message(struct Message* result, char* message) {
@ -55,16 +56,17 @@ int parse_message(struct Message* result, char* message) {
// Parse date // Parse date
// 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=0, hour=0, minute=0, second=0; int day, hour, minute, second;
{ {
int msg_len = strlen(message); int msg_len = strlen(message);
char message_fromdate[4096]; char message_fromdate[4096];
memset(&message_fromdate, '\0', sizeof(message_fromdate)); memset(&message_fromdate, '\0', sizeof(message_fromdate));
memcpy(message_fromdate, &message[position], msg_len - position); memcpy(message_fromdate, &message[position], msg_len - position);
if(sscanf(message_fromdate, "%s %d %d:%d:%d", month, &day, &hour, &minute, &second) != 5) { 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; return 1; // Failed to parse all desired fields
} }
} // frees message_fromdate, but is this pointless? } // frees message_fromdate, but is this pointless?
@ -74,10 +76,27 @@ int parse_message(struct Message* result, char* message) {
assert(date_length > 0); 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];
memset(&msg_remaining, '\0', sizeof(msg_remaining)); // memset(&msg_remaining, '\0', sizeof(msg_remaining));
memcpy(msg_remaining, &message[position], strlen(message) - position); // memcpy(msg_remaining, &message[position], strlen(message) - position);
printf("'%s'\n", msg_remaining); // printf("'%s'\n", msg_remaining);
// or
// memmove(message, &message[position], strlen(message) - position);
// printf("'%s'\n", message);
// Parse APPLICATION
// filterlog: 5,,,1000000103,cpsw0,match....
char application[64]; // TODO check max length
int app_length;
if(sscanf(message + position, "%s%n", application, &app_length) != 1) { // %n not counted in returned field count
return 1; // Failed to parse all desired fields
}
application[app_length-1] = '\0'; // Remove the trailing :
memcpy(result->application, application, sizeof(application));
position += app_length + 1;
printf("remaining: '%s'\n", message + position + 1);
return 0; return 0;
} }
@ -146,7 +165,7 @@ int main(int argc, char** argv) {
//printf("msg[size_recvd] is: %d", msg[size_recvd]); //printf("msg[size_recvd] is: %d", msg[size_recvd]);
msg[size_recvd] = '\0'; // We receive 1 full string at a time msg[size_recvd] = '\0'; // We receive 1 full string at a time
if(parse_message(&result, msg) != 1) { if(parse_message(&result, msg) != 1) {
printf("message is valid, priority %d\n", result.priority); printf("message is valid,\n\tpriority: %d\n\tapplication: %s\n", result.priority, result.application);
} }
} }