Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature dtcenter/METplus-Internal#19 log user ID and dtcenter/METplus-Internal#21 signal handling #2160

Merged
merged 13 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
90 changes: 90 additions & 0 deletions src/basic/vx_log/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** 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:
// The main() should be inplemented here and the exisyting main() should be renamed to met_main.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The main() should be inplemented here and the exisyting main() should be renamed to met_main.
// The main() should be implemented here and the existing main()
// should be renamed to met_main().
//
////////////////////////////////////////////////////////////////////////


#include "main.h"
#include <unistd.h>
//#include <pwd.h>

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

static const int tmp_buf_size = 512;

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

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

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

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

return time_str;
}

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

uid_t get_user_id()
{
register uid_t uid = geteuid ();
/*
register struct passwd *pw;
pw = getpwuid (uid);
if (pw) {
puts (pw->pw_name);
exit (EXIT_SUCCESS);
}
fprintf (stderr,"%s: cannot find username for UID %u\n",
_PROGRAM_NAME, (unsigned) uid);
exit (EXIT_FAILURE);
*/
return uid;
}

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

void do_pre_process(uid_t user_id, const char *tool_name) {
cout << " Started " << tool_name << " by user " << user_id << " at " << get_current_time() << "\n";
cout << " Do signal handling here and other pre-processing\n";
}


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

void do_post_preocess(uid_t user_id, const char *tool_name) {
cout << " Do post-processing\n";
cout << " Done " << tool_name << " by user " << user_id << " at " << get_current_time() << "\n";
}

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

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

uid_t user_id = get_user_id();
const char * tool_name = get_tool_name();

do_pre_process(user_id, tool_name);

int return_code = met_main(argc, argv);

do_post_preocess(user_id, tool_name);

return return_code;

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

41 changes: 41 additions & 0 deletions src/basic/vx_log/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
// ** 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>
using namespace std;

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


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


extern const char *get_tool_name();

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


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


#endif /* MAIN_H */


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

15 changes: 11 additions & 4 deletions src/tools/other/ascii2nc/ascii2nc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ using namespace std;
#include <unistd.h>
#include <vector>

#include "main.h"

#include <netcdf>
using namespace netCDF;

Expand Down Expand Up @@ -147,7 +149,7 @@ static void setup_wrapper_path();

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

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

//
Expand Down Expand Up @@ -189,7 +191,7 @@ int main(int argc, char *argv[]) {
// Check for error. There should be at least two arguments left:
// the ascii input filenames and the netCDF output filename
//
if(cline.n() < 2) usage();
if(cline.n() < 2) { usage(); return 0; }

//
// Store the input ASCII file name and the output NetCDF file name
Expand Down Expand Up @@ -239,7 +241,7 @@ int main(int argc, char *argv[]) {
if(!file_handler->readAsciiFiles(asfile_list)) {
mlog << Error << "\n" << program_name << "-> "
<< "encountered an error while reading input files!\n\n";
exit(1);
return 1;
}

//
Expand All @@ -262,6 +264,12 @@ int main(int argc, char *argv[]) {

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

const char *get_tool_name() {
return program_name;
}

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

FileHandler *create_file_handler(const ASCIIFormat format, const ConcatString &ascii_filename) {

#ifdef ENABLE_PYTHON
Expand Down Expand Up @@ -497,7 +505,6 @@ void usage() {

<< flush;

exit (1);
}

////////////////////////////////////////////////////////////////////////
Expand Down