Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --regex filter option #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sys/stat.h>
#include <pwd.h>
#include <CoreFoundation/CoreFoundation.h>
#include <regex.h>
#include "MobileDevice.h"

#include "extern.h"
Expand All @@ -21,6 +22,8 @@ static bool force_color = false;
static char *simulatorLogPath;
static CFStringRef requiredDeviceId;
static char *requiredProcessName;
static char *requiredRegexPattern;
static regex_t requiredRegex;
static void (*printMessage)(int fd, const char *, size_t);
static void (*printSeparator)(int fd);

Expand Down Expand Up @@ -83,6 +86,15 @@ static unsigned char should_print_message(const char *buffer, size_t length)
}
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

Expand Down Expand Up @@ -432,6 +444,7 @@ int main (int argc, char * const argv[])
{"udid", required_argument, NULL, 'u'},
{"simulator", required_argument, NULL, 's'},
{"process", required_argument, NULL, 'p'},
{"regex", required_argument, NULL, 'r'},
{"help", no_argument, NULL, 'h'},
{"debug", no_argument, (int*)&debug, 1},
{"use-separators", no_argument, (int*)&use_separators, 1},
Expand All @@ -441,7 +454,7 @@ int main (int argc, char * const argv[])

int option_index = 0;

while((c = getopt_long(argc, argv, "u:s:p:", long_options, &option_index)) != -1){
while((c = getopt_long(argc, argv, "u:s:p:r:", long_options, &option_index)) != -1){
switch (c){
case 0:
break;
Expand All @@ -468,6 +481,22 @@ int main (int argc, char * const argv[])

strcpy(requiredProcessName, optarg);
break;
case 'r':
{
requiredRegexPattern = malloc(strlen(optarg) + 1);
requiredRegexPattern[strlen(optarg)] = '\0';

strcpy(requiredRegexPattern, optarg);

int status = regcomp(&requiredRegex, requiredRegexPattern, REG_EXTENDED);
if (status != 0) {
char errorMessage[2048];
regerror(status, &requiredRegex, errorMessage, 2048);
fprintf(stderr, "Error: Regex error compiling '%s': %s\n", requiredRegexPattern, errorMessage);
return 1;
}
break;
}
case 'h':
case '?':
goto usage;
Expand Down Expand Up @@ -506,6 +535,16 @@ int main (int argc, char * const argv[])
return 0;

usage:
fprintf(stderr, "Usage: %s [options]\nOptions:\n --udid <udid> Show only logs from a specific device\n --simulator <version> Show logs from iOS Simulator\n --debug Include connect/disconnect messages in standard out\n --use-separators Skip a line between each line.\n --process Filter by process name.\n --force-color Force colored text.\nControl-C to disconnect\nMail bug reports and suggestions to <[email protected]>\n", argv[0]);
fprintf(stderr,
"Usage: %s [options]\nOptions:\n"
" --udid <udid> Show only logs from a specific device\n"
" --simulator <version> Show logs from iOS Simulator\n"
" --debug Include connect/disconnect messages in standard out\n"
" --use-separators Skip a line between each line.\n"
" --process Filter by process name.\n"
" --regex Filter by regular expression.\n"
" --force-color Force colored text.\n"
"Control-C to disconnect\n"
"Mail bug reports and suggestions to <[email protected]>\n", argv[0]);
return 1;
}