Skip to content

Commit

Permalink
Merge pull request #2160 from dtcenter/feature_METplus-Internal_19_lo…
Browse files Browse the repository at this point in the history
…g_user_id

Feature METplus-Internal #19 log user ID and #21 signal handling
  • Loading branch information
hsoh-u authored Aug 3, 2022
2 parents 1afadcc + 8b0b0c6 commit 5c0804b
Show file tree
Hide file tree
Showing 67 changed files with 727 additions and 406 deletions.
1 change: 1 addition & 0 deletions src/basic/vx_log/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ libvx_log_a_SOURCES = concat_string.cc concat_string.h \
file_fxns.cc file_fxns.h \
indent.cc indent.h \
logger.cc logger.h \
main.cc main.h \
string_array.cc string_array.h \
str_wrappers.cc str_wrappers.h \
vx_log.h
Expand Down
12 changes: 8 additions & 4 deletions src/basic/vx_log/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ am__v_AR_1 =
libvx_log_a_AR = $(AR) $(ARFLAGS)
libvx_log_a_LIBADD =
am_libvx_log_a_OBJECTS = concat_string.$(OBJEXT) file_fxns.$(OBJEXT) \
indent.$(OBJEXT) logger.$(OBJEXT) string_array.$(OBJEXT) \
str_wrappers.$(OBJEXT)
indent.$(OBJEXT) logger.$(OBJEXT) main.$(OBJEXT) \
string_array.$(OBJEXT) str_wrappers.$(OBJEXT)
libvx_log_a_OBJECTS = $(am_libvx_log_a_OBJECTS)
AM_V_P = $(am__v_P_@AM_V@)
am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
Expand All @@ -128,8 +128,8 @@ depcomp = $(SHELL) $(top_srcdir)/depcomp
am__maybe_remake_depfiles = depfiles
am__depfiles_remade = ./$(DEPDIR)/concat_string.Po \
./$(DEPDIR)/file_fxns.Po ./$(DEPDIR)/indent.Po \
./$(DEPDIR)/logger.Po ./$(DEPDIR)/str_wrappers.Po \
./$(DEPDIR)/string_array.Po
./$(DEPDIR)/logger.Po ./$(DEPDIR)/main.Po \
./$(DEPDIR)/str_wrappers.Po ./$(DEPDIR)/string_array.Po
am__mv = mv -f
CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
Expand Down Expand Up @@ -336,6 +336,7 @@ libvx_log_a_SOURCES = concat_string.cc concat_string.h \
file_fxns.cc file_fxns.h \
indent.cc indent.h \
logger.cc logger.h \
main.cc main.h \
string_array.cc string_array.h \
str_wrappers.cc str_wrappers.h \
vx_log.h
Expand Down Expand Up @@ -392,6 +393,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/file_fxns.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/indent.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/logger.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_wrappers.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/string_array.Po@am__quote@ # am--include-marker

Expand Down Expand Up @@ -547,6 +549,7 @@ distclean: distclean-am
-rm -f ./$(DEPDIR)/file_fxns.Po
-rm -f ./$(DEPDIR)/indent.Po
-rm -f ./$(DEPDIR)/logger.Po
-rm -f ./$(DEPDIR)/main.Po
-rm -f ./$(DEPDIR)/str_wrappers.Po
-rm -f ./$(DEPDIR)/string_array.Po
-rm -f Makefile
Expand Down Expand Up @@ -598,6 +601,7 @@ maintainer-clean: maintainer-clean-am
-rm -f ./$(DEPDIR)/file_fxns.Po
-rm -f ./$(DEPDIR)/indent.Po
-rm -f ./$(DEPDIR)/logger.Po
-rm -f ./$(DEPDIR)/main.Po
-rm -f ./$(DEPDIR)/str_wrappers.Po
-rm -f ./$(DEPDIR)/string_array.Po
-rm -f Makefile
Expand Down
185 changes: 185 additions & 0 deletions src/basic/vx_log/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** Copyright UCAR (c) 2022 - 2022
// ** University Corporation for Atmospheric Research (UCAR)
// ** National Center for Atmospheric Research (NCAR)
// ** Research Applications Lab (RAL)
// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

////////////////////////////////////////////////////////////////////////
//
// Filename: main.cc
//
// Description:
// MET has only one main(). The common features like logging with user ID
// for STIG and the signal handling are implemented one place whcih
// prevents from copying or calling the same APIs at the multiple palces.
// The existing "main" at MET tools should be renamed to "met_main" and
// three APIs (get_tool_name, initialize, and process_command_line) must
// be added. Most MET tools have initialize and process_command_line.
//
// int main(int argc, char *argv[])
// ==>
// int met_main(int argc, char *argv[])
//
// string get_tool_name() { return "mode"; }
// void initialize();
// void process_command_line(int argc, char **argv);
//
// Mod# Date Name Description
// ---- ---- ---- -----------
// 000 07/06/22 Howard Soh New

#include <csignal>
#include <pwd.h>

#include "main.h"
#include "concat_string.h"
#include "memory.h"
#include "logger.h"


////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////

static std::string met_cmdline = "";

static int met_start_time;
static int met_end_time;
static uid_t met_user_id;
static string met_user_name;
static string met_tool_name;

////////////////////////////////////////////////////////////////////////

extern string get_tool_name();

extern int met_main(int argc, char *argv[]);

//extern void initialize();
//extern void process_command_line(int argc, char **argv);

void do_post_process();
void do_pre_process(int argc, char *argv[]);
void set_handlers();
void set_user_id();
void store_arguments(int argc, char **argv);

////////////////////////////////////////////////////////////////////////

int main(int argc, char *argv[]) {

do_pre_process(argc, argv);

//initialize();
//process_command_line(argc, argv);

int return_code = met_main(argc, argv);

do_post_process();

return return_code;

}

////////////////////////////////////////////////////////////////////////

void do_pre_process(int argc, char *argv[]) {
ConcatString msg, msg2;

store_arguments(argc, argv);

set_user_id();
met_tool_name = get_tool_name();

msg << "Start " << met_tool_name << " by " << met_user_name
<< "(" << met_user_id << ") at " << get_current_time();
msg2 << " cmd: " << met_cmdline;
mlog << Debug(1) << msg << msg2 << "\n";

set_handlers();
}

////////////////////////////////////////////////////////////////////////

void do_post_process() {
ConcatString msg;
msg << "Finish " << met_tool_name << " by " << met_user_name
<< "(" << met_user_id << ") at " << get_current_time();
mlog << Debug(1) << msg << "\n";
}

////////////////////////////////////////////////////////////////////////

string get_current_time() {
time_t curr_time;
tm * curr_tm;
char date_string[MET_BUF_SIZE];

time(&curr_time);
curr_tm = gmtime (&curr_time);

strftime(date_string, MET_BUF_SIZE, "%Y-%m-%d %TZ", curr_tm);
//string time_str(date_string);

return string(date_string);
}

////////////////////////////////////////////////////////////////////////

/* not working at Docker
// based on blog at http://www.alexonlinux.com/how-to-handle-sigsegv-but-also-generate-core-dump
// NOTE: that comments on the blog indicate the core file generated on red hat or on multi-threaded programs
// might contain unhelpful information.
void segv_handler(int signum) {
char cwdbuffer[MET_BUF_SIZE+1];
string timebuffer = get_current_time();
getcwd(cwdbuffer,MET_BUF_SIZE+1);
fprintf(stderr, "FATAL ERROR (SEGFAULT): Process %d got signal %d @ local time = %s\n", getpid(), signum, timebuffer);
fprintf(stderr, "FATAL ERROR (SEGFAULT): Look for a core file in %s\n",cwdbuffer);
fprintf(stderr, "FATAL ERROR (SEGFAULT): Process command line: %s\n",met_cmdline.c_str());
signal(signum, SIG_DFL);
kill(getpid(), signum);
}
*/

////////////////////////////////////////////////////////////////////////
// Need signal handlers for SIGINT, SIGHUP, SIGTERM, SIGPIPE, and SIGSEGV
// PORTsignal(SIGPIPE, (PORTsigfunc)SIG_IGN);
// PORTsignal(SIGSEGV, segv_handler);

void set_handlers() {

set_new_handler(oom);
}

////////////////////////////////////////////////////////////////////////

void set_user_id() {
met_user_id = geteuid ();
register struct passwd *pw;
pw = getpwuid (met_user_id);
if (pw) met_user_name = string(pw->pw_name);
}

////////////////////////////////////////////////////////////////////////

void store_arguments(int argc, char **argv) {
for (int ix = 0; ix < argc; ix++){
met_cmdline += argv[ix];
met_cmdline += " ";
}
}

////////////////////////////////////////////////////////////////////////

void tidy_and_exit(int signal) {
printf("Exiting %d\n", signal);
exit(signal);
}

////////////////////////////////////////////////////////////////////////
40 changes: 40 additions & 0 deletions src/basic/vx_log/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** Copyright UCAR (c) 1992 - 2022
// ** University Corporation for Atmospheric Research (UCAR)
// ** National Center for Atmospheric Research (NCAR)
// ** Research Applications Lab (RAL)
// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*


////////////////////////////////////////////////////////////////////////


#ifndef __MAIN_H__
#define __MAIN_H__


////////////////////////////////////////////////////////////////////////

#include <ctime>
#include <iostream>
#include <string.h>

using namespace std;

////////////////////////////////////////////////////////////////////////

#define MET_BUF_SIZE 4096

////////////////////////////////////////////////////////////////////////

//extern void log_event(int event_id, string event_msg);
extern string get_current_time();

////////////////////////////////////////////////////////////////////////

#endif // __MAIN_H__


////////////////////////////////////////////////////////////////////////

18 changes: 10 additions & 8 deletions src/tools/core/ensemble_stat/ensemble_stat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,23 @@
// 034 01/14/21 McCabe MET #1695 All members in one file.
// 035 02/15/22 Halley Gotway MET #1583 Add HiRA option.
// 036 02/20/22 Halley Gotway MET #1259 Write probabilistic statistics.
// 037 07/06/22 Howard Soh METplus-Internal #19 Rename main to met_main
//
////////////////////////////////////////////////////////////////////////

using namespace std;

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <ctype.h>
#include <dirent.h>
#include <iostream>
#include <fstream>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "main.h"
#include "ensemble_stat.h"

#include "vx_nc_util.h"
Expand Down Expand Up @@ -186,14 +184,11 @@ static void set_compress(const StringArray &);

////////////////////////////////////////////////////////////////////////

int main(int argc, char *argv[]) {
int met_main(int argc, char *argv[]) {

// Set up OpenMP (if enabled)
init_openmp();

// Set handler to be called for memory allocation error
set_new_handler(oom);

// Process the command line arguments
process_command_line(argc, argv);

Expand All @@ -214,6 +209,13 @@ int main(int argc, char *argv[]) {

////////////////////////////////////////////////////////////////////////

const string get_tool_name()
{
return "ensemble_stat";
}

////////////////////////////////////////////////////////////////////////

void process_command_line(int argc, char **argv) {
int i;
CommandLine cline;
Expand Down
Loading

0 comments on commit 5c0804b

Please sign in to comment.