Commit 3f7f12c1 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Use and update blc_program_init (name and pid) in addition with blc_program_name

parent 9ea2959a
......@@ -97,7 +97,7 @@ void blc_command_add(const char *command_name, type_blc_command_cb callback, con
tmp_command.user_data = user_data;
FOR_INV(i, blc_commands_nb)
if (strcmp(blc_commands[i].name, command_name) == 0) EXIT_ON_ERROR("%s: command '%s' is already defined.", blc_program_name, command_name);
if (strcmp(blc_commands[i].name, command_name) == 0) EXIT_ON_ERROR("%s: command '%s' is already defined.", blc_program_id, command_name);
APPEND_ITEM(&blc_commands, &blc_commands_nb, &tmp_command);
}
......@@ -153,7 +153,7 @@ void blc_command_interpret_string(char const *input_string, size_t size){
}
}
}
if (command == blc_commands+blc_commands_nb) fprintf(stderr, "%s: unknown command in: '%s'\n", blc_program_name, input_string);
if (command == blc_commands+blc_commands_nb) fprintf(stderr, "%s: unknown command in: '%s'\n", blc_program_id, input_string);
}
void blc_command_interpret_block(){
......
......@@ -46,14 +46,14 @@ type_blc_status blc_status=BLC_RUN;
void blc_fprint_stats(FILE *file)
{
if (intermediate_iteration){
fprintf(file, "%-16.16s %4d iterations duration:%8.3fms[%8.3f, %8.3f] frequency:%8.3fHz period:%8.3fms\n", blc_program_name, intermediate_iteration, blc_duration/(1000.f*intermediate_iteration), blc_duration_min/1000., blc_duration_max/1000., 1e6*intermediate_iteration/(double)blc_period, blc_period/(1000.*intermediate_iteration));
fprintf(file, "%-16.16s %4d iterations duration:%8.3fms[%8.3f, %8.3f] frequency:%8.3fHz period:%8.3fms\n", blc_program_id, intermediate_iteration, blc_duration/(1000.f*intermediate_iteration), blc_duration_min/1000., blc_duration_max/1000., 1e6*intermediate_iteration/(double)blc_period, blc_period/(1000.*intermediate_iteration));
intermediate_iteration=0;
blc_period=0;
blc_duration=0;
blc_duration_min=UINT_MAX;
blc_duration_max=0;
}
else fprintf(file, "%s: No new iteration yet\n", blc_program_name);
else fprintf(file, "%s: No new iteration yet\n", blc_program_id);
gettimeofday(&timer, NULL);
}
......@@ -155,11 +155,11 @@ void pause_cb()
if (blc_status==BLC_RUN || blc_status==BLC_STEPS){
blc_status=BLC_PAUSE;
blc_command_add(">", step_forward_cb, "|iterations", "continue for steps nb iterations", NULL);
fprintf(stderr, "=== %s: pause iteration %lld === '' to continue, '>[<nb>]' to step one or run nb iterations ===\n", blc_program_name, blc_loop_iteration);
fprintf(stderr, "=== %s: pause iteration %lld === '' to continue, '>[<nb>]' to step one or run nb iterations ===\n", blc_program_id, blc_loop_iteration);
}else if (blc_status==BLC_PAUSE){
blc_command_remove(">");
blc_status=BLC_RUN;
fprintf(stderr, "=== %s: continue === \n", blc_program_name);
fprintf(stderr, "=== %s: continue === \n", blc_program_id);
BLC_PTHREAD_CHECK(pthread_mutex_unlock(&mutex_lock_keyboard), NULL);
}
else EXIT_ON_ERROR("Unknown status '%d'", blc_status);
......@@ -176,12 +176,12 @@ static void *loop_check_stuck(void*){
sleep(checking_time);
if(blc_status==BLC_RUN){ //in BLC_PAUSE it is normal to block
if (iteration==blc_loop_iteration) {
color_eprintf(BLC_YELLOW, "'%s' seems blocked on iteration '%d'. The loop has been stopped for more than %ds\n", blc_program_name, iteration, checking_time);
color_eprintf(BLC_YELLOW, "'%s' seems blocked on iteration '%d'. The loop has been stopped for more than %ds\n", blc_program_id, iteration, checking_time);
if (blc_profile_file) fflush(blc_profile_file); //It may help for debuggage
was_stucked=1;
}
else if (was_stucked) {
color_eprintf(BLC_GREEN, "'%s' program restart iteration '%d'.\n", blc_program_name, iteration);
color_eprintf(BLC_GREEN, "'%s' program restart iteration '%d'.\n", blc_program_id, iteration);
was_stucked=0;
}
}
......@@ -222,7 +222,7 @@ void blc_command_loop_init(long loop_period){
if (blc_command_loop_period!=-1) { //we lock on keyboard. But if the program lock we still read keyboard
SYSTEM_ERROR_CHECK(pthread_create(&loop_check_stuck_thread, NULL, loop_check_stuck, NULL), -1, NULL);
blc_command_add("s", (type_blc_command_cb)display_stats, NULL, "display time stats", NULL); //We did not do stat on keyboard
fprintf(stderr, "=== %s: running ===\n", blc_program_name);
fprintf(stderr, "=== %s: running ===\n", blc_program_id);
}
blc_command_add("|", (type_blc_command_cb)blc_command_send_to_stdout, "command", "send the command to stdout", NULL);
blc_command_add("&", (type_blc_command_cb)blc_command_send_to_all, "command", "intepret caommdn and send it to stdout", NULL);
......@@ -239,7 +239,7 @@ int blc_command_loop_start(){
//We wait before counting the duration time as the time for waiting does not matter
if (blc_loop_iteration==blc_loop_iteration_limit){
fprintf(stderr, "%s: %d iterations executed in %.3fms\n", blc_program_name, (int)blc_loop_iteration-step_iteration, blc_us_time_diff(&step_timer)/1000.f);
fprintf(stderr, "%s: %d iterations executed in %.3fms\n", blc_program_id, (int)blc_loop_iteration-step_iteration, blc_us_time_diff(&step_timer)/1000.f);
pause_cb();
}
......@@ -289,7 +289,7 @@ void blc_command_loop_end(){
ratio=div(diff, 1e6);
time_left.tv_sec = ratio.quot;
time_left.tv_nsec = ratio.rem*1000;
if (diff < 0) color_fprintf(BLC_YELLOW, stderr, "\r%s: Missing %.3fms in the BLC_COMMAND_LOOP requesting refresh of %.3fms\n", blc_program_name, -diff/1000.f, blc_command_loop_period/1000.f);
if (diff < 0) color_fprintf(BLC_YELLOW, stderr, "\r%s: Missing %.3fms in the BLC_COMMAND_LOOP requesting refresh of %.3fms\n", blc_program_id, -diff/1000.f, blc_command_loop_period/1000.f);
else SYSTEM_ERROR_RETRY_ON_SPECIFIC_ERRNO(nanosleep(&time_left, &tmp_rem), -1, EINTR, "Time left %.3fms", tmp_rem.tv_nsec/1e6f + tmp_rem.tv_sec*1000);
/* Warning the nanosleep can be overtimed of 10ms !!! it is the same for usleep !! */
}
......
......@@ -283,7 +283,7 @@ static void blc_program_interpret_parameters(int *argc, char **argv[]){
parameter_read=getline(&tmp_parameter, &linecap, stdin);
if (parameter_read==-1){
if (errno==ENOTTY) {
color_eprintf(BLC_RED, "Quitting '%s': The standard input is not available. You probably have a program on an input pipe which has crashed\n", blc_program_name);
color_eprintf(BLC_RED, "Quitting '%s': The standard input is not available. You probably have a program on an input pipe which has crashed\n", blc_program_id);
exit(1);
}
else EXIT_ON_SYSTEM_ERROR("Reading input for parameter");
......@@ -327,12 +327,15 @@ static void blc_program_interpret_parameters(int *argc, char **argv[]){
}
void blc_program_parse_args(int *argc, char ***argv){
blc_program_name=basename(*argv[0]);
SYSTEM_ERROR_CHECK(asprintf((char**)&blc_program_id, "%s%d", blc_program_name, getpid()), -1, "Argv0: '%s'", *argv[0]);
blc_program_option_interpret(argc, argv);
blc_program_interpret_parameters(argc, argv);
}
void blc_program_parse_args_and_print_title(int *argc, char ***argv){
underline_fprintf('=', stderr, blc_program_name);
underline_fprintf('=', stderr, basename(*argv[0]));
blc_program_parse_args(argc, argv);
}
......@@ -405,30 +408,29 @@ void blc_program_args_display_help()
}
static void on_sigterm(int){
fprintf(stderr, "%s: receiving SIGTERM (kill)\n", blc_program_name);
fprintf(stderr, "%s: receiving SIGTERM (kill)\n", blc_program_id);
exit(EXIT_SUCCESS);
}
static void on_sigint(int){
if (blc_status==BLC_QUIT){
fprintf(stderr, "%s: receiving SIGINT (Ctrl+C) in quiting mode. Force to quit.\n", blc_program_name);
fprintf(stderr, "%s: receiving SIGINT (Ctrl+C) in quiting mode. Force to quit.\n", blc_program_id);
exit(EXIT_FAILURE);
}
else {
fprintf(stderr, "%s :receiving SIGINT (Ctrl+C). Try to quit. Resend SIGINT if it does not quit.\n", blc_program_name);
fprintf(stderr, "%s :receiving SIGINT (Ctrl+C). Try to quit. Resend SIGINT if it does not quit.\n", blc_program_id);
blc_status=BLC_QUIT;
}
}
static void on_sigsegv(int){
fprintf(stderr, "%s: segmentation fault ( memory access error ! )\n", blc_program_name);
fprintf(stderr, "%s: segmentation fault ( memory access error ! )\n", blc_program_id);
exit(EXIT_FAILURE);
}
//Normal exit is not called with value of 1 in exit
static void on_sigabrt(int ){
fprintf(stderr, "%s: receiving SIGABORT ( error ! )\n", blc_program_name);
exit(EXIT_FAILURE);
fprintf(stderr, "%s: receiving SIGABORT ( error ! )\n", blc_program_id);
}
//At this moment error_exit_cb is the same than normal exit
......@@ -438,8 +440,6 @@ void blc_program_init(int *argc, char ***argv, void (*exit_cb)(void))
struct sigaction action;
struct timeval timer;
SYSTEM_ERROR_CHECK(asprintf(&blc_program_name, "%s%d", basename(*argv[0]), getpid()), -1, "Argv0: '%s'", *argv[0]);
CLEAR(action);
action.sa_handler = on_sigterm;
SYSTEM_SUCCESS_CHECK(sigaction(SIGTERM, &action, 0), 0, NULL);
......@@ -496,9 +496,9 @@ void blc_program_check_full_parsing(int argc, char **argv){
void blc_quit()
{
if (blc_profile_file) fclose(blc_profile_file);
if (blc_program_name) {
fprintf(stderr, "Quitting %s\n", blc_program_name);
FREE(blc_program_name);
if (blc_program_id[0]) { //We test if it is not ""
fprintf(stderr, "Quitting %s\n", blc_program_id);
FREE(blc_program_id);
}
}
......
......@@ -20,7 +20,7 @@ int main( int argc, char **argv){
blc_program_add_parameter(&optional_parameter, "string", 0, "Show how to accept simple text as option", NULL);
//Interprets the arguments, associates variables and print program name as title.
blc_program_parse_args_and_print_title(&argc, &argv);
blc_program_parse_args(&argc, &argv);
//We display on stderr. It is a good habit to use for interaction with the user
fprintf(stderr, "Display help:\n");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment