diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/CommandLine.c | 134 | ||||
-rw-r--r-- | src/main/ProcessCommandLine.c | 112 |
2 files changed, 177 insertions, 69 deletions
diff --git a/src/main/CommandLine.c b/src/main/CommandLine.c index 84e94801..fac58368 100644 --- a/src/main/CommandLine.c +++ b/src/main/CommandLine.c @@ -16,10 +16,12 @@ #include "cctk_Flesh.h" #include "cGH.h" +#include "util_String.h" #include "cctk_Version.h" #include "cctk_ActiveThorns.h" #include "cctk_CommandLine.h" #include "cctk_Comm.h" +#include "cctk_File.h" #include "cctk_Misc.h" #include "cctk_ParamCheck.h" #include "cctk_WarnLevel.h" @@ -46,7 +48,9 @@ static void CommandLinePrintParameter (const cParamData *properties); /******************************************************************** ********************* Local Data ***************************** ********************************************************************/ -static int already_redirected_stdout = 0; +static char* logdir = NULL; +static int requested_stdout_redirection = 0; +static int requested_stderr_redirection = 0; static int paramchecking = 0; @@ -67,9 +71,10 @@ int cctki_onlyprintschedule = 0; * places you have to update if you add a new command-line option. */ #define CACTUS_COMMANDLINE_OPTIONS \ - "[-h] [-O] [-o paramname] [-L n] [-W n] [-E n] [-r[o|e|oe|eo]] " \ - "[-b [no|line|full]] " \ - "[-S] [-T] [-t name] [--parameter-level <level>] [-v] " \ + "[-h] [-O] [-o paramname] [-L n] [-W n] [-E n] " \ + "[-r[o|e|oe|eo]] [--logdir <dir>] " \ + "[-b <no|line|full>] " \ + "[-S] [-T] [-t name] [--parameter-level <level>] [-v] " \ "<parameter_file_name>" @@ -213,13 +218,13 @@ void CCTKi_CommandLineDescribeParameter (const char *argument) cthorn = CCTK_ImplementationThorn (thorn); properties = CCTK_ParameterData (param, cthorn); } - + free (thorn); free (param); } - + if(properties) - { + { if (CCTK_MyProc (NULL) == 0) { CommandLinePrintParameter (properties); @@ -234,7 +239,7 @@ void CCTKi_CommandLineDescribeParameter (const char *argument) } retcode = 1; } - + CCTK_Exit (NULL, retcode); } @@ -430,7 +435,10 @@ void CCTKi_CommandLineParameterLevel (const char *argument) @date Fri Jul 23 11:32:46 1999 @author Tom Goodale @desc - Redirect standard output on non-root processors into a file + Redirect stdout/stderr on non-root processors into a file + + Redirection is defered until all command line options + (including a possible '-logdir <dir>') have been processed. @enddesc @var argument @@ -441,26 +449,35 @@ void CCTKi_CommandLineParameterLevel (const char *argument) @@*/ void CCTKi_CommandLineRedirect (const char *argument) { - int myproc; - char fname[24]; - - - myproc = CCTK_MyProc (NULL); - if (myproc) + if (!argument || strchr(argument,'o')) /* redirect stdout */ { - if (!argument || strchr(argument,'o')) /* redirect stdout */ - { - sprintf (fname, "CCTK_Proc%u.out", myproc); - freopen (fname, "w", stdout); - already_redirected_stdout = 1; - } - if (argument && strchr(argument,'e')) /* redirect stderr */ - { - sprintf (fname, "CCTK_Proc%u.err", myproc); - freopen (fname, "w", stderr); - } + requested_stdout_redirection = 1; + } + if (argument && strchr(argument,'e')) /* redirect stderr */ + { + requested_stderr_redirection = 1; } } + + + /*@@ + @routine CCTKi_CommandLineLogDir + @date Thu 22 July 2006 + @author Thomas Radke + @desc + Set the output directory for redirected stdout/stderr logfiles. + @enddesc + + @var argument + @vdesc option argument + @vtype const char * + @vio in + @endvar +@@*/ +void CCTKi_CommandLineLogDir (const char *argument) +{ + logdir = Util_Strdup (argument); +} /*@@ @routine CCTKi_CommandLineSetBuffering @date 2006-05-27 @@ -596,8 +613,10 @@ void CCTKi_CommandLineHelp (void) "-L, --logging-level <n> : Sets the logging level to n.\n" "-W, --warning-level <n> : Sets the warning level to n.\n" "-E, --error-level <n> : Sets the error level to n.\n" - "-r, --redirect [o|e|oe|eo] : Redirects standard output and/or\n" + "-r, --redirect[o|e|oe|eo] : Redirects standard output and/or\n" " standard error to files.\n" + " --logdir <dir> : Sets the output directory for logfiles\n" + " created by the '-r' option\n" "-b, --buffering <no|line|full> : Set stdout buffering mode.\n" "-S, --print-schedule : Print the schedule tree, then exit.\n" "-T, --list-thorns : Lists the compiled-in thorns.\n" @@ -663,18 +682,71 @@ void CCTKi_CommandLineUsage (void) @@*/ void CCTKi_CommandLineFinished (void) { + int myproc; + char *logfilename; + + /* Are we in a paramcheck run ? */ if (! paramchecking) { cctki_paramchecking = 0; } - /* if no redirect was requested on the command line - send stdout messages on non-root processors to /dev/null */ - if (! already_redirected_stdout && CCTK_MyProc (NULL) != 0) + /* redirect stdout/stderr on non-root processors */ + if (logdir && !(requested_stdout_redirection || requested_stderr_redirection)) { - freopen (NULL_DEVICE, "w", stdout); + CCTK_VWarn (CCTK_WARN_PICKY, __LINE__, __FILE__, "Cactus", + "Specifying the '-logdir' option without the '-r' option " + "is a no-op and will be ignored."); + } + myproc = CCTK_MyProc (NULL); + if (myproc) + { + /* if specified on the command line, create the output directory + for redirected stdout/stderr logfiles */ + if (logdir) + { + if (requested_stdout_redirection || requested_stderr_redirection) + { + if (CCTK_CreateDirectory (0755, logdir) < 0) + { + CCTK_VWarn (1, __LINE__, __FILE__, "Cactus", + "Could not create output directory '%s' for " + "stdout/stderr logfiles ! Falling back to using the " + "current working directory...", logdir); + free (logdir); + logdir = Util_Strdup ("."); + } + } + } + else + { + /* make cwd the default logdir */ + logdir = Util_Strdup ("."); + } + + /* if redirection was requested on the command line + send stdout/stderr messages to <logdir>/CCTK_Proc<id>.{out,err} + otherwise redirect stdout to the NULL device */ + logfilename = malloc (strlen (logdir) + 32); + if (requested_stdout_redirection) + { + sprintf (logfilename, "%s/CCTK_Proc%u.out", logdir, myproc); + freopen (logfilename, "w", stdout); + } + else + { + freopen (NULL_DEVICE, "w", stdout); + } + + if (requested_stderr_redirection) + { + sprintf (logfilename, "%s/CCTK_Proc%u.err", logdir, myproc); + freopen (logfilename, "w", stderr); + } + free (logfilename); } + free (logdir); } diff --git a/src/main/ProcessCommandLine.c b/src/main/ProcessCommandLine.c index 57543700..d47ad721 100644 --- a/src/main/ProcessCommandLine.c +++ b/src/main/ProcessCommandLine.c @@ -110,40 +110,60 @@ int CCTKi_ProcessCommandLine(int *inargc, char ***inargv, tFleshConfig *ConfigDa int option_index = 0; int c; int ignore; + /* constants for identifying each options by the return value + from getopt_long_only() */ + enum + { + help_option = 'h', + describe_all_paramters_option = 'O', + describe_parameter_option = 'o', + test_parameters_option = 'x', + logging_level_option = 'L', + warning_level_option = 'W', + error_level_option = 'E', + parameter_level_option = 256, /* no short option */ + redirect_option = 'r', + logdir_option = 257, /* no short option */ + buffering_option = 'b', + print_schedule_option = 'S', + list_thorns_option = 'T', + test_thorns_compiled_option = 't', + version_option = 'v', + ignore_next_option = 'i' + }; + /* the longopts argument passed into getopt_long_only() */ + const struct option long_options[] = + { + {"help", no_argument, NULL, help_option}, + {"describe-all-parameters", optional_argument, NULL, describe_all_paramters_option}, + {"describe-parameter", required_argument, NULL, describe_parameter_option}, + /*{"test-parameters", optional_argument, NULL, test_parameters_option},*/ + {"logging-level", required_argument, NULL, logging_level_option}, + {"warning-level", required_argument, NULL, warning_level_option}, + {"error-level", required_argument, NULL, error_level_option}, + {"parameter-level", required_argument, NULL, parameter_level_option}, + {"redirect", optional_argument, NULL, redirect_option}, + {"logdir", required_argument, NULL, logdir_option}, + {"buffering", required_argument, NULL, buffering_option}, + {"print-schedule", no_argument, NULL, print_schedule_option}, + {"list-thorns", no_argument, NULL, list_thorns_option}, + {"test-thorn-compiled", required_argument, NULL, test_thorns_compiled_option}, + {"version", no_argument, NULL, version_option}, + {"ignore-next", no_argument, NULL, ignore_next_option}, + {0, 0, 0, 0} + }; + /* Store the command line */ argc = *inargc; - argv = *inargv; ignore = 0; - /* Process the command line - needs some work !*/ - if(argc>1) { while (1) { - struct option long_options[] = - { - {"help", no_argument, NULL, 'h'}, - {"describe-all-parameters", optional_argument, NULL, 'O'}, - {"describe-parameter", required_argument, NULL, 'o'}, - /*{"test-parameters", optional_argument, NULL, 'x'},*/ - {"logging-level", required_argument, NULL, 'L'}, - {"warning-level", required_argument, NULL, 'W'}, - {"error-level", required_argument, NULL, 'E'}, - {"parameter-level", required_argument, NULL, 256}, - {"redirect", optional_argument, NULL, 'r'}, - {"buffering", required_argument, NULL, 'b'}, - {"print-schedule", no_argument, NULL, 'S'}, - {"list-thorns", no_argument, NULL, 'T'}, - {"test-thorn-compiled", required_argument, NULL, 't'}, - {"version", no_argument, NULL, 'v'}, - {"ignore-next", no_argument, NULL, 'i'}, - {0, 0, 0, 0} - }; - c = getopt_long_only (argc, argv, "hO::o:x::L:W:E:r::b:STt:vi", long_options, &option_index); if (c == -1) @@ -153,21 +173,37 @@ int CCTKi_ProcessCommandLine(int *inargc, char ***inargv, tFleshConfig *ConfigDa { switch (c) { - case 't': CCTKi_CommandLineTestThornCompiled(optarg); break; - case 'O': CCTKi_CommandLineDescribeAllParameters(optarg); break; - case 'o': CCTKi_CommandLineDescribeParameter(optarg); break; - case 'x': CCTKi_CommandLineTestParameters(optarg); break; - case 'L': CCTKi_CommandLineLoggingLevel(optarg); break; - case 'W': CCTKi_CommandLineWarningLevel(optarg); break; - case 'E': CCTKi_CommandLineErrorLevel(optarg); break; - case 256: CCTKi_CommandLineParameterLevel(optarg); break; - case 'r': CCTKi_CommandLineRedirect(optarg); break; - case 'b': CCTKi_CommandLineSetBuffering(optarg); break; - case 'S': CCTKi_CommandLinePrintSchedule(); break; - case 'T': CCTKi_CommandLineListThorns(); break; - case 'v': CCTKi_CommandLineVersion(); break; - case 'i': ignore = 1; break; - case 'h': + case describe_all_paramters_option: + CCTKi_CommandLineDescribeAllParameters(optarg); break; + case describe_parameter_option: + CCTKi_CommandLineDescribeParameter(optarg); break; + case test_parameters_option: + CCTKi_CommandLineTestParameters(optarg); break; + case logging_level_option: + CCTKi_CommandLineLoggingLevel(optarg); break; + case warning_level_option: + CCTKi_CommandLineWarningLevel(optarg); break; + case error_level_option: + CCTKi_CommandLineErrorLevel(optarg); break; + case parameter_level_option: + CCTKi_CommandLineParameterLevel(optarg); break; + case redirect_option: + CCTKi_CommandLineRedirect(optarg); break; + case logdir_option: + CCTKi_CommandLineLogDir(optarg); break; + case buffering_option: + CCTKi_CommandLineSetBuffering(optarg); break; + case print_schedule_option: + CCTKi_CommandLinePrintSchedule(); break; + case list_thorns_option: + CCTKi_CommandLineListThorns(); break; + case test_thorns_compiled_option: + CCTKi_CommandLineTestThornCompiled(optarg); break; + case version_option: + CCTKi_CommandLineVersion(); break; + case ignore_next_option: + ignore = 1; break; + case help_option: case '?': CCTKi_CommandLineHelp(); break; default: |