static unsigned char should_print_message(const char *buffer, size_t length) { if (length < 3) return 0; // don't want blank lines size_t space_offsets[3]; find_space_offsets(buffer, length, space_offsets); // Check whether process name matches the one passed to -p option and filter if needed if (strlen(requiredProcessName)) { char processName[256]; memset(processName, '\0', 256); memcpy(processName, buffer + space_offsets[0] + 1, space_offsets[1] - space_offsets[0]); for (int i=strlen(processName); i!=0; i--) if (processName[i]=='[') processName[i]='\0'; if (strcmp(processName, requiredProcessName)!=0) return 0; } // More filtering options can be added here and return 0 when they won't meed filter criteria return 1; }
static unsigned char should_print_message(const char *buffer, size_t length) { if (length < 3) return 0; // don't want blank lines unsigned char should_print = 1; size_t space_offsets[3]; find_space_offsets(buffer, length, space_offsets); // Check whether process name matches the one passed to -p option and filter if needed if (requiredProcessNames != NULL) { char *currentProcessName; int nameLength = space_offsets[1] - space_offsets[0]; //This size includes the NULL terminator. char *processName = malloc(nameLength); processName[nameLength - 1] = '\0'; memcpy(processName, buffer + space_offsets[0] + 1, nameLength - 1); for (int i = strlen(processName); i != 0; i--) if (processName[i] == '[') processName[i] = '\0'; currentProcessName = strtok(strdup(requiredProcessNames), ", "); while (currentProcessName != NULL) { should_print = (strcmp(processName, currentProcessName) == 0); if (should_print) break; currentProcessName = strtok(NULL, ", "); } free(processName); } if (requiredRegex != NULL) { char *message = malloc(length + 1); memcpy(message, buffer, length + 1); message[length + 1] = '\0'; if (regexec(requiredRegex, message, 0, NULL, 0) == REG_NOMATCH) should_print = 0; } // More filtering options can be added here and return 0 when they won't meed filter criteria return should_print; }
static unsigned char should_print_message(const char *buffer, size_t length) { if (length < 3) return 0; // don't want blank lines size_t space_offsets[3]; find_space_offsets(buffer, length, space_offsets); // Check whether process name matches the one passed to -p option and filter if needed if (requiredProcessName != NULL) { int nameLength = space_offsets[1] - space_offsets[0]; //This size includes the NULL terminator. char *processName = malloc(nameLength); processName[nameLength - 1] = '\0'; memcpy(processName, buffer + space_offsets[0] + 1, nameLength - 1); for (int i = strlen(processName); i != 0; i--) if (processName[i] == '[') processName[i] = '\0'; if (strcmp(processName, requiredProcessName) != 0){ free(processName); return 0; } free(processName); } // Check whether message matches regex filter if (requiredRegexPattern != NULL) { int status = regnexec(&requiredRegex, buffer, length, 0, NULL, 0); if (status != 0) { // Not a match return 0; } } // More filtering options can be added here and return 0 when they won't meed filter criteria return 1; }
static void write_colored(int fd, const char *buffer, size_t length) { if (length < 16) { write_fully(fd, buffer, length); return; } size_t space_offsets[3]; int o = find_space_offsets(buffer, length, space_offsets); if (o == 3) { // Log date and device name write_const(fd, COLOR_DARK_WHITE); write_fully(fd, buffer, space_offsets[0]); // Log process name int pos = 0; for (int i = space_offsets[0]; i < space_offsets[0]; i++) { if (buffer[i] == '[') { pos = i; break; } } write_const(fd, COLOR_CYAN); if (pos && buffer[space_offsets[1]-1] == ']') { write_fully(fd, buffer + space_offsets[0], pos - space_offsets[0]); write_const(fd, COLOR_DARK_CYAN); write_fully(fd, buffer + pos, space_offsets[1] - pos); } else { write_fully(fd, buffer + space_offsets[0], space_offsets[1] - space_offsets[0]); } // Log level size_t levelLength = space_offsets[2] - space_offsets[1]; if (levelLength > 4) { const char *normalColor; const char *darkColor; if (levelLength == 9 && memcmp(buffer + space_offsets[1], " <Debug>:", 9) == 0){ normalColor = COLOR_MAGENTA; darkColor = COLOR_DARK_MAGENTA; } else if (levelLength == 11 && memcmp(buffer + space_offsets[1], " <Warning>:", 11) == 0){ normalColor = COLOR_YELLOW; darkColor = COLOR_DARK_YELLOW; } else if (levelLength == 9 && memcmp(buffer + space_offsets[1], " <Error>:", 9) == 0){ normalColor = COLOR_RED; darkColor = COLOR_DARK_RED; } else if (levelLength == 10 && memcmp(buffer + space_offsets[1], " <Notice>:", 10) == 0) { normalColor = COLOR_GREEN; darkColor = COLOR_DARK_GREEN; } else { goto level_unformatted; } write_string(fd, darkColor); write_fully(fd, buffer + space_offsets[1], 2); write_string(fd, normalColor); write_fully(fd, buffer + space_offsets[1] + 2, levelLength - 4); write_string(fd, darkColor); write_fully(fd, buffer + space_offsets[1] + levelLength - 2, 1); write_const(fd, COLOR_DARK_WHITE); write_fully(fd, buffer + space_offsets[1] + levelLength - 1, 1); } else { level_unformatted: write_const(fd, COLOR_RESET); write_fully(fd, buffer + space_offsets[1], levelLength); } write_const(fd, COLOR_RESET); write_fully(fd, buffer + space_offsets[2], length - space_offsets[2]); } else { write_fully(fd, buffer, length); } }