Skip to content

Commit

Permalink
Merge pull request #16 from pulp-platform/warnings
Browse files Browse the repository at this point in the history
Improve warnings and log file creation
  • Loading branch information
bluewww authored Mar 16, 2024
2 parents 84e65d9 + faf19ae commit 9de29fb
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 46 deletions.
5 changes: 3 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tests_LDADD = $(TRDB_ALL_LINKER_LIBS)

TESTS=tests
LOG_DRIVER=./tests

# Benchmarks
benchmarks_SOURCES = src/trace_debugger.c src/utils.c src/serialize.c \
src/error.c src/disassembly.c benchmark/benchmarks.c
Expand All @@ -40,8 +41,8 @@ libtrdb_la_LIBADD = $(TRDB_ALL_LINKER_LIBS)

include_HEADERS = include/disassembly.h include/serialize.h include/trace_debugger.h

AM_CFLAGS = -std=gnu11 -Wall -Wextra -Wno-missing-field-initializers -Wno-unused-function -Wno-missing-braces -fdiagnostics-color
AM_CPPFLAGS = -D_GNU_SOURCE -I. -Iinclude -Iinternal $(TRDB_LINKER_INCLUDES)
AM_CFLAGS = -std=gnu11 -Wall -Wextra -Werror=format-security -Wno-missing-field-initializers -Wno-unused-function -Wno-missing-braces -fdiagnostics-color
AM_CPPFLAGS = -D_GNU_SOURCE -Iinclude -Iinternal $(TRDB_LINKER_INCLUDES) -D_GLIBCXX_ASSERTIONS

# workaround for shared source files
# https://www.gnu.org/software/automake/manual/html_node/Objects-created-both-with-libtool-and-without.html
Expand Down
144 changes: 100 additions & 44 deletions test/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,17 +1151,13 @@ static int test_decompress_trace_differential(const char *bin_path,
}

/* make any directory in path if it doesn't exist*/
static void mkdir_p(char *path)
static int mkdir_p(char *path)
{
struct stat stat_buf = {0};
char *str;
char *s;

s = strdup(path);
if (!s) {
perror("strdup");
exit(EXIT_FAILURE);
}
s = path;

while ((str = strtok(s, "/")) != NULL) {
if (str != s) {
Expand All @@ -1170,22 +1166,26 @@ static void mkdir_p(char *path)
if (stat(path, &stat_buf) == -1) {
if (mkdir(path, 0777) == -1) {
perror("mkdir");
exit(EXIT_FAILURE);
return 1;
}
} else {
if (!S_ISDIR(stat_buf.st_mode)) {
LOG_ERRT("could not create directory %s\n", path);
exit(EXIT_FAILURE);
return 1;
}
}
s = NULL;
}

if (s)
free(s);
return 0;
}

static bool is_valid_dir(const char *name)
{
return *name != '/';
}

static bool is_valid_name(char *name)
static bool is_valid_name(const char *name)
{
return !(*name == '.' || *name == '/');
}
Expand Down Expand Up @@ -1369,53 +1369,109 @@ int main(int argc, char *argv[argc + 1])

/* create directory structure to logfile */
if (arguments.logfile) {
char *dname = dirname(arguments.logfile);
if (is_valid_name(dname))
mkdir_p(dname);
char *argdup = strdup(arguments.logfile);
if (!argdup) {
perror("strdup");
return EXIT_FAILURE;
}

char *dname = dirname(argdup);
if (!is_valid_dir(dname)) {
fprintf(stderr, "error: `%s' is not a valid path to a file name\n",
arguments.logfile);
return EXIT_FAILURE;
}

if (mkdir_p(dname)) {
fprintf(stderr, "error: mkdir_p\n");
return EXIT_FAILURE;
}

free(argdup);

/* redirect stdout to logfile */
char *bname = basename(arguments.logfile);
if (is_valid_name(bname)) {
/* spawn tee */
char teecmd[PATH_MAX];
argdup = strdup(arguments.logfile);
if (!argdup) {
perror("strdup");
return EXIT_FAILURE;
}

snprintf(teecmd, sizeof(teecmd), "tee %s", arguments.logfile);
char *bname = basename(argdup);
if (!is_valid_name(bname)) {
fprintf(stderr, "error: `%s' is not a valid path to a file name\n",
arguments.logfile);
return EXIT_FAILURE;
}

if ((tee_fp = popen(teecmd, "w")) == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
/* spawn tee */
char teecmd[PATH_MAX];

/* redirect stdout to pipe */
if (dup2(fileno(tee_fp), fileno(stdout)) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
snprintf(teecmd, sizeof(teecmd), "tee %s", arguments.logfile);

/* enable line buffering so that we can tail logs efficienctly as
* for redirect output buffers are not flushed on a newline */
setlinebuf(tee_fp);
setlinebuf(stdout);
if ((tee_fp = popen(teecmd, "w")) == NULL) {
perror("popen");
return EXIT_FAILURE;
}

/* redirect stdout to pipe */
if (dup2(fileno(tee_fp), fileno(stdout)) == -1) {
perror("dup2");
return EXIT_FAILURE;
}

/* enable line buffering so that we can tail logs efficienctly as
* for redirect output buffers are not flushed on a newline */
setlinebuf(tee_fp);
setlinebuf(stdout);

free(argdup);
}

/* create directory structure to trsfile */
if (arguments.trsfile) {
char *dname = dirname(arguments.trsfile);
if (is_valid_name(dname))
mkdir_p(dname);
char *argdup = strdup(arguments.trsfile);
if (!argdup) {
perror("strdup");
return EXIT_FAILURE;
}

char *dname = dirname(argdup);
if (!is_valid_dir(dname)) {
fprintf(stderr, "error: `%s' is not a valid path to a file name\n",
arguments.trsfile);
return EXIT_FAILURE;
}

if (mkdir_p(dname)) {
fprintf(stderr, "error: mkdir_p\n");
return EXIT_FAILURE;
}

free(argdup);

/* prepare trsfile */
char *bname = basename(arguments.trsfile);
if (is_valid_name(bname)) {
if ((trs_fp = fopen(bname, "w+")) == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
/* enable line buffering so that we can tail logs efficienctly as
* for redirect output buffers are not flushed on a newline */
setlinebuf(trs_fp);
argdup = strdup(arguments.trsfile);
if (!argdup) {
perror("strdup");
return EXIT_FAILURE;
}

char *bname = basename(argdup);
if (!is_valid_name(bname)) {
fprintf(stderr, "error: `%s' is not a valid path to a file name\n",
arguments.trsfile);
return EXIT_FAILURE;
}

if ((trs_fp = fopen(bname, "w+")) == NULL) {
perror("fopen");
return EXIT_FAILURE;
}
/* enable line buffering so that we can tail logs efficienctly as
* for redirect output buffers are not flushed on a newline */
setlinebuf(trs_fp);

free(argdup);
}

/* const char *tv_cvs[] = {"data/cvs/pmp.spike_trace"}; */
Expand Down

0 comments on commit 9de29fb

Please sign in to comment.