Commit 5ddeb016 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Add profiling change blc_program_name

parent ed5ededa
......@@ -15,6 +15,7 @@
#include "blc_program.h"
#include "program.hpp" // internal to blc
#include <sys/time.h>
#include <stdio.h>
#include <sys/ioctl.h>
......@@ -76,6 +77,10 @@ struct program_parameter{
char const *default_value;
};
FILE *blc_profile_file=NULL;
int blc_input_terminal, blc_output_terminal;
// Static means it is only existing in this file
static char const *program_description=NULL;
static type_program_option *blc_program_options=NULL;
......@@ -83,9 +88,6 @@ static int blc_program_options_nb=0;
static struct program_parameter *blc_program_parameters=NULL;
static int blc_program_parameters_nb;
int blc_input_terminal, blc_output_terminal;
START_EXTERN_C
void blc_add_arg( int *argc, char ***argv, char const *arg, char const *value){
APPEND_ITEM(argv, argc, &arg);
......@@ -128,7 +130,7 @@ char * const*blc_create_argv_from_command_line(char const *command_line)
void blc_program_set_description(char const *description)
{
program_description=strdup(description);
program_description=description;
}
void blc_program_add_parameter(char const**result, char const *name, int required, char const *help, char const *default_value)
......@@ -220,9 +222,11 @@ static void blc_program_option_interpret(int *argc, char **argv[])
{
optstring[optstring_size++]=program_option->letter;
tmp_option.name = (const char*)program_option->name;
if (program_option->parameter) tmp_option.has_arg=required_argument;
if (program_option->parameter){
tmp_option.has_arg=required_argument; //optional_argument does not work on OSX !!
}
else tmp_option.has_arg=no_argument;
if (tmp_option.has_arg !=0) optstring[optstring_size++]=':';
if (tmp_option.has_arg==required_argument) optstring[optstring_size++]=':'; //optional_argument does not work on OSX !!
tmp_option.flag = NULL;
tmp_option.val=program_option->letter;
APPEND_ITEM(&long_options, &options_nb, &tmp_option);
......@@ -242,12 +246,17 @@ static void blc_program_option_interpret(int *argc, char **argv[])
{
if (ret == program_option->letter) //TODO see what to do with special char '?' and ':'
{
if (optarg == NULL) *program_option->string_pt="1";
if (optarg == NULL) {
if (program_option->default_value) *program_option->string_pt=program_option->default_value;
else *program_option->string_pt="1";
}
else{
switch (program_option->type){
case STRING:
*program_option->string_pt = optarg;
break;
//Pipe not in use anymore
case INPUT_PIPE:
SPRINTF(pipe_name, "/tmp/blc_pipes/%s", optarg);
SYSTEM_ERROR_CHECK(freopen(optarg, "r", program_option->pipe), NULL, NULL);
......@@ -311,7 +320,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, "\nQuitting '%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_name);
exit(1);
}
else EXIT_ON_SYSTEM_ERROR("Reading input for parameter");
......@@ -406,7 +415,7 @@ void blc_program_args_display_help()
option_length_max+=10;
if (blc_program_parameters_nb) fprintf(stderr, POSITIONAL_ARGUMENTS_TITLE);
FOR_EACH(parameter, blc_program_parameters, blc_program_parameters_nb){
fprintf(stderr, " %-*s %s", option_length_max-2, parameter->name, parameter->help);
fprintf(stderr, " %-*s %s\n", option_length_max-2, parameter->name, parameter->help);
if (parameter->default_value) fprintf(stderr, " (default: %s)\n", parameter->default_value);
}
......@@ -433,37 +442,40 @@ void blc_program_args_display_help()
}
static void on_sigterm(int){
fprintf(stderr, "\n%s: receiving SIGTERM (kill)\n", blc_program_name);
fprintf(stderr, "%s: receiving SIGTERM (kill)\n", blc_program_name);
exit(EXIT_SUCCESS);
}
static void on_sigint(int){
if (blc_status==BLC_QUIT){
fprintf(stderr, "\n%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_name);
exit(EXIT_FAILURE);
}
else {
fprintf(stderr, "\n%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_name);
blc_status=BLC_QUIT;
}
}
static void on_sigsegv(int){
fprintf(stderr, "\n%s: segmentation fault ( memory access error ! )\n", blc_program_name);
fprintf(stderr, "%s: segmentation fault ( memory access error ! )\n", blc_program_name);
exit(EXIT_FAILURE);
}
//Normal exit is not called with value of 1 in exit
static void on_sigabrt(int ){
fprintf(stderr, "\n%s: receiving SIGABORT ( error ! )\n", blc_program_name);
fprintf(stderr, "%s: receiving SIGABORT ( error ! )\n", blc_program_name);
exit(EXIT_FAILURE);
}
//At this moment error_exit_cb is the same than normal exit
void blc_program_init(int *argc, char ***argv, void (*exit_cb)(void))
{
char const*help;
char const*help, *profile;
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;
......@@ -486,8 +498,17 @@ void blc_program_init(int *argc, char ***argv, void (*exit_cb)(void))
if (!blc_stderr_ansi) fprintf(stderr, "o ansi terminal. There is no color.\n"); //The n of no is displayed byt he terminale itself.
}
blc_program_add_option(&profile, '~', "profile", "filename", "profile the execution of the loop", NULL);
blc_program_add_option(&help, 'h', "help", 0, "display this help", NULL);
blc_program_parse_args_and_print_title(argc, argv);
if (profile){
SYSTEM_ERROR_CHECK(blc_profile_file=fopen(profile, "w"), NULL, "filename: %s", profile);
fprintf(blc_profile_file, "#start_time\tduration\t\n");
gettimeofday(&timer, NULL);
fprintf(blc_profile_file, "%lu\t0\t0\t\n", timer.tv_sec*1000000+timer.tv_usec);
}
if (help){
blc_program_args_display_help();
exit(EXIT_SUCCESS);
......@@ -511,6 +532,11 @@ void blc_program_check_full_parsing(int argc, char **argv){
void blc_quit()
{
fprintf(stderr, "Quitting %s\n", blc_program_name);
if (blc_profile_file) fclose(blc_profile_file);
if (blc_program_name) {
fprintf(stderr, "Quitting %s\n", blc_program_name);
FREE(blc_program_name);
}
}
END_EXTERN_C
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