From 43c7ccdd08bd573cabf3089ccf59dcdf0cac7163 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Fri, 6 Nov 2015 14:59:10 +0200 Subject: [PATCH] Whitespace and indentation changes. Whitespace and indentation changes of log manager test programs. --- log_manager/test/testlog.c | 1022 ++++++++++++++++++---------------- log_manager/test/testorder.c | 173 +++--- 2 files changed, 633 insertions(+), 562 deletions(-) diff --git a/log_manager/test/testlog.c b/log_manager/test/testlog.c index 43b28c02c..886a66c09 100644 --- a/log_manager/test/testlog.c +++ b/log_manager/test/testlog.c @@ -17,8 +17,8 @@ */ -/** @file -@brief (brief description) +/** @file + @brief (brief description) */ #include @@ -27,11 +27,12 @@ #include #include -typedef struct thread_st { - skygw_message_t* mes; - simple_mutex_t* mtx; - size_t* nactive; - pthread_t tid; +typedef struct thread_st +{ + skygw_message_t* mes; + simple_mutex_t* mtx; + size_t* nactive; + pthread_t tid; } thread_t; static void* thr_run(void* data); @@ -53,582 +54,641 @@ static void* thr_run_morelog(void* data); int main(int argc, char* argv[]) { - int err = 0; - char* logstr; - - int i; - bool succp; - skygw_message_t* mes; - simple_mutex_t* mtx; - size_t nactive; - thread_t** thr = NULL; - time_t t; - struct tm tm; - char c; - int nthr = 0; - int log_argc = 0; - char** log_argv = NULL; + int err = 0; + char* logstr; - while ((c = getopt(argc, argv, "t:")) != -1) - { - switch (c) { - case 't': - nthr = atoi(optarg); - break; + int i; + bool succp; + skygw_message_t* mes; + simple_mutex_t* mtx; + size_t nactive; + thread_t** thr = NULL; + time_t t; + struct tm tm; + char c; + int nthr = 0; + int log_argc = 0; + char** log_argv = NULL; - default: - break; - } + while ((c = getopt(argc, argv, "t:")) != -1) + { + switch (c) { + case 't': + nthr = atoi(optarg); + break; + + default: + break; } + } - if (nthr <= 0) - { - fprintf(stderr, "Thread count argument is zero or " - "negative. Exiting.\n"); - err = 1; - goto return_err; - } + if (nthr <= 0) + { + fprintf(stderr, "Thread count argument is zero or " + "negative. Exiting.\n"); + err = 1; + goto return_err; + } - thr = (thread_t **)calloc(1, nthr*sizeof(thread_t*)); - - if (thr == NULL) - { - fprintf(stderr, "Failed to allocate memory for thread " - "structure. Exiting.\n"); - err = 1; - goto return_err; - - } - i = atexit(skygw_logmanager_exit); - - if (i != 0) { - fprintf(stderr, "Couldn't register exit function.\n"); - } - - succp = skygw_logmanager_init("/tmp", log_argc, log_argv); - if(!succp) - fprintf(stderr, "Log manager initialization failed.\n"); - ss_dassert(succp); + thr = (thread_t **)calloc(1, nthr*sizeof(thread_t*)); - t = time(NULL); - tm = *(localtime(&t)); - err = skygw_log_write_flush(LOGFILE_ERROR, - "%04d %02d/%02d %02d.%02d.%02d", - tm.tm_year+1900, - tm.tm_mon+1, - tm.tm_mday, - tm.tm_hour, - tm.tm_min, - tm.tm_sec); - - skygw_logmanager_init("/tmp", log_argc, log_argv); - logstr = ("First write with flush."); - err = skygw_log_write_flush(LOGFILE_ERROR, logstr); + if (thr == NULL) + { + fprintf(stderr, "Failed to allocate memory for thread " + "structure. Exiting.\n"); + err = 1; + goto return_err; + } + i = atexit(skygw_logmanager_exit); - logstr = ("Second write with flush."); - err = skygw_log_write_flush(LOGFILE_ERROR, logstr); + if (i != 0) + { + fprintf(stderr, "Couldn't register exit function.\n"); + } - logstr = ("Third write, no flush."); - err = skygw_log_write(LOGFILE_ERROR, logstr); + succp = skygw_logmanager_init("/tmp", log_argc, log_argv); - logstr = ("Fourth write, no flush. Next flush only."); - err = skygw_log_write(LOGFILE_ERROR, logstr); + if (!succp) + { + fprintf(stderr, "Log manager initialization failed.\n"); + } + ss_dassert(succp); - err = skygw_log_flush(LOGFILE_ERROR); - - logstr = "My name is %s %d years and %d months."; + t = time(NULL); + tm = *(localtime(&t)); + err = skygw_log_write_flush(LOGFILE_ERROR, + "%04d %02d/%02d %02d.%02d.%02d", + tm.tm_year+1900, + tm.tm_mon+1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); + + skygw_logmanager_init("/tmp", log_argc, log_argv); + logstr = ("First write with flush."); + err = skygw_log_write_flush(LOGFILE_ERROR, logstr); + + logstr = ("Second write with flush."); + err = skygw_log_write_flush(LOGFILE_ERROR, logstr); + + logstr = ("Third write, no flush."); + err = skygw_log_write(LOGFILE_ERROR, logstr); + + logstr = ("Fourth write, no flush. Next flush only."); + err = skygw_log_write(LOGFILE_ERROR, logstr); + + err = skygw_log_flush(LOGFILE_ERROR); + + logstr = "My name is %s %d years and %d months."; #if !defined(SS_DEBUG) - skygw_log_enable(LOGFILE_TRACE); + skygw_log_enable(LOGFILE_TRACE); #endif - err = skygw_log_write(LOGFILE_TRACE, logstr, "TraceyTracey", 3, 7); - skygw_log_flush(LOGFILE_TRACE); + err = skygw_log_write(LOGFILE_TRACE, logstr, "TraceyTracey", 3, 7); + skygw_log_flush(LOGFILE_TRACE); #if !defined(SS_DEBUG) - skygw_log_enable(LOGFILE_TRACE); + skygw_log_enable(LOGFILE_TRACE); #endif - logstr = "My name is Tracey Tracey 47 years and 7 months."; - err = skygw_log_write(LOGFILE_TRACE, logstr); + logstr = "My name is Tracey Tracey 47 years and 7 months."; + err = skygw_log_write(LOGFILE_TRACE, logstr); #if !defined(SS_DEBUG) - skygw_log_enable(LOGFILE_TRACE); -#endif - logstr = "My name is Stacey %s"; - err = skygw_log_write_flush(LOGFILE_TRACE, logstr, " "); - skygw_logmanager_done(); -#if !defined(SS_DEBUG) - skygw_log_enable(LOGFILE_TRACE); + skygw_log_enable(LOGFILE_TRACE); #endif - logstr = "My name is Philip"; - err = skygw_log_write(LOGFILE_TRACE, logstr); + logstr = "My name is Stacey %s"; + err = skygw_log_write_flush(LOGFILE_TRACE, logstr, " "); + skygw_logmanager_done(); #if !defined(SS_DEBUG) - skygw_log_enable(LOGFILE_TRACE); + skygw_log_enable(LOGFILE_TRACE); #endif - logstr = "Philip."; - err = skygw_log_write(LOGFILE_TRACE, logstr); + logstr = "My name is Philip"; + err = skygw_log_write(LOGFILE_TRACE, logstr); #if !defined(SS_DEBUG) - skygw_log_enable(LOGFILE_TRACE); + skygw_log_enable(LOGFILE_TRACE); #endif - logstr = "Ph%dlip."; - err = skygw_log_write(LOGFILE_TRACE, logstr, 1); - - skygw_logmanager_init("/tmp", log_argc, log_argv); - logstr = ("A terrible error has occurred!"); - err = skygw_log_write_flush(LOGFILE_ERROR, logstr); + logstr = "Philip."; + err = skygw_log_write(LOGFILE_TRACE, logstr); +#if !defined(SS_DEBUG) + skygw_log_enable(LOGFILE_TRACE); +#endif + logstr = "Ph%dlip."; + err = skygw_log_write(LOGFILE_TRACE, logstr, 1); - logstr = ("Hi, how are you?"); - err = skygw_log_write(LOGFILE_MESSAGE, logstr); + skygw_logmanager_init("/tmp", log_argc, log_argv); + logstr = ("A terrible error has occurred!"); + err = skygw_log_write_flush(LOGFILE_ERROR, logstr); - logstr = ("I'm doing fine!"); - err = skygw_log_write(LOGFILE_MESSAGE, logstr); + logstr = ("Hi, how are you?"); + err = skygw_log_write(LOGFILE_MESSAGE, logstr); - logstr = ("Rather more surprising, at least at first sight, is the fact that a reference to a[i] can also be written as *(a+i). In evaluating a[i], C converts it to *(a+i) immediately; the two forms are equivalent. Applying the operators & to both parts of this equivalence, it follows that &a[i] and a+i are also identical: a+i is the address of the i-th element beyond a."); - err = skygw_log_write(LOGFILE_ERROR, logstr); + logstr = ("I'm doing fine!"); + err = skygw_log_write(LOGFILE_MESSAGE, logstr); - logstr = ("I was wondering, you know, it has been such a lovely weather whole morning and I thought that would you like to come to my place and have a little piece of cheese with us. Just me and my mom - and you, of course. Then, if you wish, we could listen to the radio and keep company for our little Steven, my mom's cat, you know."); - err = skygw_log_write(LOGFILE_MESSAGE, logstr); - skygw_logmanager_done(); + logstr = ("Rather more surprising, at least at first sight, is the fact that a reference to " + "a[i] can also be written as *(a+i). In evaluating a[i], C converts it to *(a+i) " + "immediately; the two forms are equivalent. Applying the operators & to both parts " + "of this equivalence, it follows that &a[i] and a+i are also identical: a+i is the " + "address of the i-th element beyond a."); + err = skygw_log_write(LOGFILE_ERROR, logstr); + + logstr = ("I was wondering, you know, it has been such a lovely weather whole morning and I " + "thought that would you like to come to my place and have a little piece of cheese " + "with us. Just me and my mom - and you, of course. Then, if you wish, we could " + "listen to the radio and keep company for our little Steven, my mom's cat, you know."); + err = skygw_log_write(LOGFILE_MESSAGE, logstr); + skygw_logmanager_done(); #if defined(TEST1) - - mes = skygw_message_init(); - mtx = simple_mutex_init(NULL, "testmtx"); - /** Test starts */ + mes = skygw_message_init(); + mtx = simple_mutex_init(NULL, "testmtx"); + /** Test starts */ - fprintf(stderr, "\nStarting test #1 \n"); - - /** 1 */ - for (i=0; imes = mes; - thr[i]->mtx = mtx; - thr[i]->nactive = &nactive; - } - nactive = nthr; + fprintf(stderr, "\nStarting test #1 \n"); - for (i=0; itid = p; - } + /** 1 */ + for (i = 0; i < nthr; i++) + { + thr[i] = (thread_t*)calloc(1, sizeof(thread_t)); + thr[i]->mes = mes; + thr[i]->mtx = mtx; + thr[i]->nactive = &nactive; + } + nactive = nthr; - do { - skygw_message_wait(mes); - simple_mutex_lock(mtx, true); - if (nactive > 0) { - simple_mutex_unlock(mtx); - continue; - } - break; - } while(true); + for (i = 0; i < nthr; i++) + { + pthread_t p; + pthread_create(&p, NULL, thr_run, thr[i]); + thr[i]->tid = p; + } - for (i=0; itid, NULL); - } - /** This is to release memory */ - skygw_logmanager_done(); - - simple_mutex_unlock(mtx); - - for (i=0; i 0) + { + simple_mutex_unlock(mtx); + continue; } + break; + } while(true); + + for (i = 0; i < nthr; i++) + { + pthread_join(thr[i]->tid, NULL); + } + /** This is to release memory */ + skygw_logmanager_done(); + + simple_mutex_unlock(mtx); + + for (i = 0; i < nthr; i++) + { + free(thr[i]); + } #endif #if defined(TEST2) - fprintf(stderr, "\nStarting test #2 \n"); + fprintf(stderr, "\nStarting test #2 \n"); - /** 2 */ - for (i=0; imes = mes; - thr[i]->mtx = mtx; - thr[i]->nactive = &nactive; + /** 2 */ + for (i = 0; i < nthr; i++) + { + thr[i] = (thread_t*)calloc(1, sizeof(thread_t)); + thr[i]->mes = mes; + thr[i]->mtx = mtx; + thr[i]->nactive = &nactive; + } + nactive = nthr; + + fprintf(stderr, + "\nLaunching %d threads, each iterating %d times.", + nthr, + NITER); + + for (i = 0; i < nthr; i++) + { + pthread_t p; + pthread_create(&p, NULL, thr_run_morelog, thr[i]); + thr[i]->tid = p; + } + + fprintf(stderr, ".. done"); + + fprintf(stderr, "\nStarting to wait threads.\n"); + + do + { + skygw_message_wait(mes); + simple_mutex_lock(mtx, true); + if (nactive > 0) + { + simple_mutex_unlock(mtx); + continue; } - nactive = nthr; + break; + } while(true); - fprintf(stderr, - "\nLaunching %d threads, each iterating %d times.", - nthr, - NITER); - - for (i=0; itid = p; - } + for (i = 0; i < nthr; i++) + { + pthread_join(thr[i]->tid, NULL); + } + /** This is to release memory */ + skygw_logmanager_done(); - fprintf(stderr, ".. done"); + simple_mutex_unlock(mtx); - fprintf(stderr, "\nStarting to wait threads.\n"); - - do { - skygw_message_wait(mes); - simple_mutex_lock(mtx, true); - if (nactive > 0) { - simple_mutex_unlock(mtx); - continue; - } - break; - } while(true); + fprintf(stderr, "\nFreeing thread memory."); - for (i=0; itid, NULL); - } - /** This is to release memory */ - skygw_logmanager_done(); - - simple_mutex_unlock(mtx); + for (i = 0; i < nthr; i++) + { + free(thr[i]); + } - fprintf(stderr, "\nFreeing thread memory."); - - for (i=0; imtx, true); - *td->nactive -= 1; - simple_mutex_unlock(td->mtx); - skygw_message_send(td->mes); - return NULL; + err = skygw_log_write(LOGFILE_TRACE, logstr); + if (err != 0) + { + fprintf(stderr,"Error, log write failed.\n"); + } + ss_dassert(err == 0); + skygw_logmanager_done(); + logstr = ("Testing. One, two, three, four\n"); + err = skygw_log_write(LOGFILE_ERROR, logstr); + if (err != 0) + { + fprintf(stderr,"Error, log write failed.\n"); + } + ss_dassert(err == 0); + skygw_logmanager_init("/tmp", 0, NULL); + logstr = ("Testing. One, two, three, .. where was I?\n"); + err = skygw_log_write(LOGFILE_ERROR, logstr); + if (err != 0) + { + fprintf(stderr,"Error, log write failed.\n"); + } + ss_dassert(err == 0); + skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_init("/tmp", 0, NULL); + skygw_logmanager_done(); + simple_mutex_lock(td->mtx, true); + *td->nactive -= 1; + simple_mutex_unlock(td->mtx); + skygw_message_send(td->mes); + return NULL; } -static int nstr( - char** str_arr) +static int nstr(char** str_arr) { - int i; + int i; - for (i=0; str_arr[i] != NULL; i++) { - } - return i; + for (i = 0; str_arr[i] != NULL; i++) + { + } + return i; } char* logs[] = { - "foo", - "bar", - "done", - "critical test logging", - "longer test l o g g g i n g", - "reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line", - "shoorter one", - "two", - "scrap : 834nuft984pnw8ynup4598yp8wup8upwn48t5gpn45", - "more the same : f98uft5p8ut2p44449upnt5", - "asdasd987987asdasd987987asdasd987987asdasd987987asdasd987987asdasd987987asdasd987987asdasd98987", - NULL + "foo", + "bar", + "done", + "critical test logging", + "longer test l o g g g i n g", + "reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally looooooooooooooooooooooooooooooooooooooo" + "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line", + "shoorter one", + "two", + "scrap : 834nuft984pnw8ynup4598yp8wup8upwn48t5gpn45", + "more the same : f98uft5p8ut2p44449upnt5", + "asdasd987987asdasd987987asdasd987987asdasd987987asdasd987987asdasd987987asdasd987987asdasd98987", + NULL }; - -static void* thr_run_morelog( - void* data) +static void* thr_run_morelog(void* data) { - thread_t* td = (thread_t *)data; - int err; - int i; - int nmsg; + thread_t* td = (thread_t *)data; + int err; + int i; + int nmsg; - nmsg = nstr(logs); - - for (i=0; imtx, true); - *td->nactive -= 1; - simple_mutex_unlock(td->mtx); - skygw_message_send(td->mes); - return NULL; + } + + simple_mutex_lock(td->mtx, true); + *td->nactive -= 1; + simple_mutex_unlock(td->mtx); + skygw_message_send(td->mes); + return NULL; } diff --git a/log_manager/test/testorder.c b/log_manager/test/testorder.c index d9bd0fc6e..edb51fec3 100644 --- a/log_manager/test/testorder.c +++ b/log_manager/test/testorder.c @@ -25,88 +25,99 @@ int main(int argc, char** argv) { - int iterations = 0, i, interval = 10; - int block_size; - int succp, err = 0; - char cwd[1024]; - char tmp[2048]; - char *message; - char** optstr; - long msg_index = 1; - struct timespec ts1; - ts1.tv_sec = 0; - - memset(cwd,0,1024); - if( argc <4){ - fprintf(stderr, - "Log Manager Log Order Test\n" - "Writes an ascending number into the error log to determine if log writes are in order.\n" - "Usage:\t testorder \n"); - return 1; - } - - block_size = atoi(argv[3]); - if(block_size < 1 || block_size > 1024){ - fprintf(stderr,"Message size too small or large, must be at least 1 byte long and must not exceed 1024 bytes."); - return 1; - } + int iterations = 0, i, interval = 10; + int block_size; + int succp, err = 0; + char cwd[1024]; + char tmp[2048]; + char *message; + char** optstr; + long msg_index = 1; + struct timespec ts1; + ts1.tv_sec = 0; - - if(getcwd(cwd,sizeof(cwd)) == NULL || - (optstr = (char**)malloc(sizeof(char*)*4)) == NULL || - (message = (char*)malloc(sizeof(char)*block_size))== NULL){ - fprintf(stderr,"Fatal Error, exiting..."); - return 1; - } - - memset(tmp,0,1024); - - sprintf(tmp,"%s",cwd); - optstr[0] = strdup("log_manager"); - optstr[1] = NULL; - - iterations = atoi(argv[1]); - interval = atoi(argv[2]); - - succp = skygw_logmanager_init(tmp, 1, optstr); - if(!succp) - fprintf(stderr,"Error, log manager initialization failed.\n"); - ss_dassert(succp); - - skygw_log_disable(LOGFILE_TRACE); - skygw_log_disable(LOGFILE_MESSAGE); - skygw_log_disable(LOGFILE_DEBUG); - - for(i = 0;i 8192){ - fprintf(stderr,"Error: Message too long"); - break; - } - memset(message + strlen(message), ' ', msgsize); - memset(message + block_size - 1,'\0',1); - if(interval > 0 && i % interval == 0){ - err = skygw_log_write_flush(LOGFILE_ERROR, message); - }else{ - err = skygw_log_write(LOGFILE_ERROR, message); + memset(cwd, 0, 1024); + if (argc < 4) + { + fprintf(stderr, + "Log Manager Log Order Test\n" + "Writes an ascending number into the error log to determine if log writes are in order.\n" + "Usage:\t testorder \n"); + return 1; } - if(err){ - fprintf(stderr,"Error: log_manager returned %d",err); - break; - } - ts1.tv_nsec = 100*1000000; - nanosleep(&ts1, NULL); - } - skygw_log_flush(LOGFILE_ERROR); - skygw_logmanager_done(); - free(message); - free(optstr[0]); - free(optstr[1]); - free(optstr[2]); - free(optstr[3]); - free(optstr); - return 0; + block_size = atoi(argv[3]); + if (block_size < 1 || block_size > 1024) + { + fprintf(stderr,"Message size too small or large, must be at least 1 byte long and " + "must not exceed 1024 bytes."); + return 1; + } + + if (getcwd(cwd, sizeof(cwd)) == NULL || + (optstr = (char**)malloc(sizeof(char*) * 4)) == NULL || + (message = (char*)malloc(sizeof(char) * block_size)) == NULL) + { + fprintf(stderr,"Fatal Error, exiting..."); + return 1; + } + + memset(tmp, 0, 1024); + + sprintf(tmp, "%s", cwd); + optstr[0] = strdup("log_manager"); + optstr[1] = NULL; + + iterations = atoi(argv[1]); + interval = atoi(argv[2]); + + succp = skygw_logmanager_init(tmp, 1, optstr); + + if (!succp) + { + fprintf(stderr,"Error, log manager initialization failed.\n"); + } + ss_dassert(succp); + + skygw_log_disable(LOGFILE_TRACE); + skygw_log_disable(LOGFILE_MESSAGE); + skygw_log_disable(LOGFILE_DEBUG); + + for (i = 0; i < iterations; i++) + { + sprintf(message, "message|%ld", msg_index++); + int msgsize = block_size - strlen(message); + if (msgsize < 0 || msgsize > 8192) + { + fprintf(stderr,"Error: Message too long"); + break; + } + memset(message + strlen(message), ' ', msgsize); + memset(message + block_size - 1, '\0', 1); + if (interval > 0 && i % interval == 0) + { + err = skygw_log_write_flush(LOGFILE_ERROR, message); + } + else + { + err = skygw_log_write(LOGFILE_ERROR, message); + } + if (err) + { + fprintf(stderr,"Error: log_manager returned %d",err); + break; + } + ts1.tv_nsec = 100 * 1000000; + nanosleep(&ts1, NULL); + } + + skygw_log_flush(LOGFILE_ERROR); + skygw_logmanager_done(); + free(message); + free(optstr[0]); + free(optstr[1]); + free(optstr[2]); + free(optstr[3]); + free(optstr); + return 0; }