diff --git a/frontend/bulk_monitor_scfe.cxx b/frontend/bulk_monitor_scfe.cxx new file mode 100644 index 0000000..785ee18 --- /dev/null +++ b/frontend/bulk_monitor_scfe.cxx @@ -0,0 +1,6811 @@ +/* *******************************************************************\ + + Name: mon_do.c + Created by: RA95 (Stefan Ritt's sample Slow Control Front end program) + + Contents: Slow Control Frontend program for muSR monitor & logging. + Originally being used without device driver and equipment name. + Using bulk_monitor.c device driver and adding slow control + equipment mon1 with frontend_loop() now to integrate it into Midas + as a frontend program. + + Revision 1.1 2017/01/26 20:49:35 raselli + Original bulk muSR Midas 1.9.5 version + + Revision 1.5 2016/12/20 15:53:55 raselli + Integrated Agilent agva1 + + Revision 1.4 2016/12/02 13:34:43 raselli + Modified equipment name to mon1 + + Revision 1.3 2016/11/16 13:31:47 raselli + Added additional equipment to be omitted + + Revision 1.2 2016/11/11 12:47:36 raselli + Mercury iTC devices heliox0 and variox0 added as equipment to monitor + + Revision 1.1 2016/09/09 13:15:29 raselli + Bulk musr 09-SEP-2016 + + + + Date Name Modification + ----------------------------------------------------------------- + 11-FEB-2002 RA95 SUCCESS replaced by CM_SUCCESS to avoid interference + with onc rpc definition + NOTE: definitions of SUCCESS and RPC_SUCCESS are + opposite to definitions in midas.h + 19-APR-2002 RA95 count number of TC channels if reloading TC + 25-APR-2002 RA95 renamed include gen_tc_dd.h to gen_tc.h for 8.1.3 + 25-APR-2002 RA95 renamed include cd_tc.h to tc.h for 8.1.3 + 15-MAY-2002 RA95 tested on VMS VAX + 10-OCT-2003 RA95 modified for usage with DVM + 06-NOV-2003 RA95 modified for usage with MON + 20-NOV-2003 RA95 gnchans & gmondevs added + 08-DEC-2003 RA95 EOB & EOC markers added + 08-DEC-2003 RA95 Now reading monitor/device_list from ODB + 09-DEC-2003 RA95 Modified reading device_list + 10-DEC-2003 RA95 title of old statistics in mon_do BEGIN_RUN + 18-MAY-2004 RA35 New writing active_list to ODB + 12-AUG-2004 RA35 LOOP in mon_do changed to avoid MON_INIT when device falls off + 12-AUG-2004 RA35 meantemp & tempdev in ODB limited to TEMPLISTSTR + 15-NOV-2004 RA35 release of (all) class drivers before swapping mode types + to avoid memory leaks + 12-JAN-2010 RA36 added handling templtc equipment + 08-APR-2016 RA36 renamed frontend.c to mon_do.c and removed monc overhead and + /muSR/ dependencies + 09-APR-2016 RA36 added handling of templs3361 equipment + -APR-2016 RA36 added handling of dmm270x1 equipment + 13-JUN-2016 RA36 added external n_mon_devices and n_reg_devices for + bulk_monitor device driver + 10-NOV-2016 RA36 heliox0 and variox0 equipment added + 01-FEB-2018 RA36 changed equipment templtc to templtc0 + 16-OCT-2018 RA36 added hvdistr1 and hvdistr2 + NOTE: hvdistr1 and hvdistr2 are updated every second + without checking when last updated + 13-JUN-2019 RA36 PROPOSAL: added to run title information + 13-AUG-2019 RA36 heliox0 and variox0 may now be used at the same time + lckinamp0, lckinamp1 and lckinamp2 equipment added + 14-APR-2020 RA36 hvdistr1 and hvdistr2 with kV units were replaced by + hvdistr3 and hvdistr4 with V units + 15-AUG-2025 RA36 added mitchlxprc0 to code of handled but not used equipment. + warning about unknown equipment is suppressed for + equipment containing string vms. + + Revision 1.3 1998/10/23 08:46:26 midas + Added #include "null.h" + + Revision 1.2 1998/10/12 12:18:59 midas + Added Log tag in header + +\********************************************************************/ + +/*! \def MAXNDEVS + \brief max. number of devices + */ +#define MAXNDEVS 10 /* max number of devices */ +#define LIST_LENGTH (4*NAME_LENGTH) /* size of Device_list and Active_list in DD */ + /* REMEMBER: also modify BULK_MONITOR_SETTINGS + * in drivers/device/bulk_monitor.c */ +#include +#include +#include +#include "midas.h" + +#include +#ifdef OS_VMS +#include +#else +#include +#endif +#include + +extern int errno; + +#include "midas.h" +#include "../frontend/musr_midas.h" +#include "class/generic.h" +#include "device/bulk_monitor.h" + +#ifdef FORTIFY +#include "fortify.h" +#endif + +//#define MIDEBUG /* essential DEBUG information */ +//#define MIDEBUG0 /* slow down sleeping 10sec (this blocks timed logging) */ +//#define MIDEBUG1 /* more DEBUG information */ +//#define MIDEBUG2 /* even more DEBUG information */ + +/*-- Globals -------------------------------------------------------*/ + +/* The frontend name (client name) as seen by other MIDAS clients */ +char *frontend_name = "MONITOR mon1"; + +/* The frontend file name, don't change it */ +char *frontend_file_name = __FILE__; + +/*! \var frontend_call_loop + \brief frontend_loop is called periodically if this variable is TRUE + */ +BOOL frontend_call_loop = TRUE; + +/*! \var display_period + \brief the frontend status page is displayed with this frequency in ms + */ +INT display_period = 1000; + +/* maximum event size produced by this frontend */ +INT max_event_size = 10000; + +/* maximum event size for fragmented events (EQ_FRAGMENTED) */ +INT max_event_size_frag = 5*1024*1024; + +/* buffer size to hold events */ +INT event_buffer_size = 10*10000; + +BOOL connected; /* dummy value for musr_midas routines */ + +/* mon() and mon_do() specific definitions */ + +extern INT n_mon_devices; /* number of monitored devices in bulk_monitor DD */ +extern INT n_reg_devices; /* number of registered devices in bulk_monitor DD */ + +static INT n_avail_devices; /* number of available devices of registered dev */ +static INT gnchans; /* number of total channels */ +static char gmondevs[LIST_LENGTH]; /* list of monitored devices */ + +typedef struct _MonChannelRec { /* Channel info */ + + char Label[NAME_LENGTH]; /* Channel label */ + + INT Nvals; /* Number of values */ + double Offset; /* Offset to increase accuracy */ + double Sum; /* Sum of all (values-offset) */ + double Sum2; /* Sum of all (values-offset)**2 */ + double Mean; /* Mean value (offset added) */ + double Devn; /* Standard deviation */ + +} MonChannelRec, *MonChannelPtr; + +typedef struct _MonDeviceRec{ /* Device info */ + + char Name[NAME_LENGTH]; /* Device name */ + INT Nchans; /* Number of channels */ + MonChannelPtr *pChannels; + MonChannelPtr *pRawChannels; + double *pSetpointChannels; + +} MonDeviceRec, *MonDevicePtr; + +typedef struct _MonMonRec { /* Monitor info */ + + INT Ndevs; /* Number of devices */ + MonDevicePtr *pDevices; + INT Interval; /* Interval [msec] */ + +} MonMonRec, *MonMonPtr; + +/* --------------------------------------------- */ + +static MonMonRec smon = {0, NULL, 1000}; + +static INT mon_old_number = -1; +static INT mon_old_state = 0; + +static INT mon_cur_number = -1; +static INT mon_cur_state = 0; + +static INT mon_last_checked = 0; +static INT mon_last_loop = 0; +static INT mon_elapsed_loop = 0; +static INT mon_last_title = 0; + +static BOOL mon_tc_enabled = FALSE; +static BOOL mon_dvm_enabled = FALSE; +static BOOL mon_ltc1_enabled = FALSE; +static BOOL mon_ls3360_enabled = FALSE; +static BOOL mon_ls3361_enabled = FALSE; +static BOOL mon_heliox0_enabled = FALSE; +static BOOL mon_variox0_enabled = FALSE; +static BOOL mon_dmm270x1_enabled = FALSE; +static BOOL mon_hvdistr3_enabled = FALSE; +static BOOL mon_hvdistr4_enabled = FALSE; +static BOOL mon_lckinamp0_enabled = FALSE; +static BOOL mon_lckinamp1_enabled = FALSE; +static BOOL mon_lckinamp2_enabled = FALSE; + +static BOOL mon_tc_active = FALSE; +static BOOL mon_dvm_active = FALSE; +static BOOL mon_ltc1_active = FALSE; +static BOOL mon_ls3360_active = FALSE; +static BOOL mon_ls3361_active = FALSE; +static BOOL mon_heliox0_active = FALSE; +static BOOL mon_variox0_active = FALSE; +static BOOL mon_dmm270x1_active = FALSE; +static BOOL mon_hvdistr3_active = FALSE; +static BOOL mon_hvdistr4_active = FALSE; +static BOOL mon_lckinamp0_active = FALSE; +static BOOL mon_lckinamp1_active = FALSE; +static BOOL mon_lckinamp2_active = FALSE; + +static BOOL mon_update_old_rns = FALSE; + +static BOOL mon_do_statistics = FALSE; + +static char mon_monfile_name[MAX_STRING_LENGTH]; +static char mon_rlogfile_name[MAX_STRING_LENGTH]; + +static INT old_year = -1; +static INT old_month = -1; +static INT old_day = -1; + +static time_t start_of_run_time = 0; + +static INT dmm270x1_channel_used[22]; + +INT mon (char *, INT, INT, char *); +INT mon_do (char *, INT, INT, char *); +INT mon_device_list(void); +INT mon_active_list(char *); + +/*-- Equipment list ------------------------------------------------*/ + +/* device driver list */ +DEVICE_DRIVER mon_driver[] = { + { "MON", bulk_monitor, 2, NULL, DF_READ_ONLY }, + { "" } +}; + +EQUIPMENT equipment[] = { + + { "mon1", /* equipment name */ + {6, 0, /* event ID, trigger mask */ + "SYSTEM", /* event buffer */ + EQ_SLOW, /* equipment type */ + 0, /* event source */ + "FIXED", /* format */ + TRUE, /* NOTE: equipment is NOT enabled */ + RO_RUNNING | + RO_TRANSITIONS, /* read when running and on transitions */ + 1000, /* read every 5 sec */ + 0, /* stop run after this event limit */ + 0, /* number of events in super event */ + 1, /* log history every event */ + "", "", ""}, + cd_gen_read, /* readout routine */ + cd_gen, /* class driver main routine */ + mon_driver, /* device driver list */ + NULL, /* init string */ + }, + + { "" } +}; + + +/*-- Dummy routines ------------------------------------------------*/ + +INT poll_event(INT source[], INT count, BOOL test) {return 1;} +INT interrupt_configure(INT cmd, INT source[], PTYPE adr) {return 1;} + +/*-- Frontend Init -------------------------------------------------*/ + +INT frontend_init() +{ +#ifdef FORTIFY + Fortify_CheckAllMemory(); +#endif + +#ifdef MIDEBUG1 + printf("frontend_init()\n"); +#endif + + mon("MON_INIT",-1,-1,"MON_INIT"); + + return CM_SUCCESS; +} + +/*-- Frontend Exit -------------------------------------------------*/ + +INT frontend_exit() +{ + +#ifdef FORTIFY + Fortify_CheckAllMemory(); + Fortify_OutputStatistics(); +#endif + +#ifdef MIDEBUG1 + printf("frontend_exit()\n"); +#endif + + mon("MON_EXIT",-1,-1,"MON_EXIT"); + mon_active_list("NONE"); + + return CM_SUCCESS; +} + +/*-- Frontend Loop -------------------------------------------------*/ + +//OBS static DWORD last_time_cmd = 0; +static DWORD last_time_loop = 0; +#ifdef FORTIFY +static DWORD last_time_fortify = 0; +#endif + +INT frontend_loop() { + INT status; + DWORD curtime; + HNDLE hDB; + +#ifdef MIDEBUG2 + printf(">>>frontend_loop()\n"); +#endif + +#ifdef FORTIFY + Fortify_CheckAllMemory(); +#endif + + curtime = ss_millitime(); + + cm_get_experiment_database(&hDB,NULL); + + status = cm_client_in_list(); + if (status == SS_ABORT) return status; + + /* --- check for cmd variable in frontend part of ODB ---- */ +#ifdef OBSOLETE + curtime = ss_millitime(); + + /* time to check? */ + if (curtime-last_time_cmd > 5000) { + INT size; + char odbtmp[MAX_ODB_PATH]; /* front end type specific abs. ODB path */ + char odbstr[MAX_ODB_PATH]; + + /* cmd is located in frontend specific part */ + sprintf(odbtmp,"%scmd",feodbpath); + size = sizeof(odbstr); + if ((status = db_get_value(hDB,0,odbtmp,odbstr,&size,TID_STRING,FALSE))==DB_SUCCESS){ + if (!equal_ustring(odbstr,"NONE")) { + int exitflag; + +#ifdef MIDEBUG + printf("cmd = %s\n",odbstr); +#endif + exitflag = FALSE; + + /* execute command */ + if (equal_ustring(odbstr,"MON_INIT")) { + mon("MON_INIT",-1,-1,"MON_INIT"); /* initialise MONITOR */ + } else if (equal_ustring(odbstr,"EXIT")) { + mon("MON_EXIT",-1,-1,"MON_EXIT"); + mon_active_list("NONE"); + exitflag = TRUE; + } + + /* overwrite command */ + strcpy(odbstr,"None"); + db_set_value(hDB,0,odbtmp,odbstr,strlen(odbstr)+1,1,TID_STRING); + + if (exitflag) return RPC_SHUTDOWN; + } + } + + last_time_cmd = ss_millitime(); + } +#endif /* #ifdef OBSOLETE */ + + /* slow down, this is still slow control */ + curtime=ss_millitime(); + + /* only log data once per monitor interval */ + if (curtime-mon_last_loop+mon_elapsed_loop >= smon.Interval) { + mon("LOOP",-1,-1,NULL); + mon_last_loop = ss_millitime(); + mon_elapsed_loop = mon_last_loop - curtime; + } + +#ifdef MIDEBUG2 + printf("elapsed %d ms\n",curtime-last_time_loop); +#endif + + /* take shorter naps if the monitor interval is almost reached */ + if (smon.Interval -curtime+mon_last_loop-mon_elapsed_loop > 500) { + if (curtime-last_time_loop<500) { + ss_sleep(500-curtime+last_time_loop); + } + } else { + if (curtime-last_time_loop<50) { + ss_sleep(50-curtime+last_time_loop); + } + } + + last_time_loop = ss_millitime(); + +#ifdef MIDEBUG0 + ss_sleep(10000); /* slow down to be able to see processing */ +#endif /* NOTE: this will reduce logging rate */ + +#ifdef FORTIFY + /* time to display fortify output? */ + if (curtime-last_time_fortify > 600000) { + Fortify_OutputStatistics(); + last_time_fortify = ss_millitime(); + } +#endif + +#ifdef MIDEBUG2 + printf("<<> mon (%s,%d,%d,%s)\n",(cmd==NULL)?"NULL":cmd,runnumber,runstate, + (icmd==NULL)?"NULL":icmd); +#endif + status = CM_SUCCESS; + + cm_get_experiment_database(&hDB,NULL); + + if ((cmd != NULL) && (strlen(cmd)>0)) { + + /* check if mon structure is initialised */ + if ((strcmp(cmd,"MON_INIT") != 0) && (strcmp(cmd,"INIT")!=0)){ + if ((smon.Ndevs == 0) && (smon.pDevices == NULL)) + mon_do("INIT",-1,-1,NULL); + } + + /* update mon_last_checked to avoid flipping of state until + * ODB is updated */ + if ((icmd != NULL) && (strcmp(icmd,"RCHANGE")==0)) + mon_last_checked = ss_millitime(); + + /* if not called by special command, check run number and state periodically */ + if (((icmd == NULL) || ((strcmp(icmd,"MON_INIT")!=0) && + (strcmp(icmd,"RCHANGE") !=0) && + (strcmp(icmd,"MON_EXIT")!=0) )) && + ((ss_millitime()-mon_last_checked) > 5000) ) { + /* RA36 11-NOV-2016 was 30000 now 5000 */ + + /* get current run number and run state */ + if (mon_cur_number == -1) mon_cur_number = mon_old_number; + current_runnumber(hDB,&mon_cur_number); +#ifdef MIDEBUG + printf(" current_runnumber(%d) \n", mon_cur_number); +#endif + if (mon_old_number == -1) { + mon_old_number = mon_cur_number; + mon_update_old_rns = TRUE; + } + + if (mon_cur_state == 0) mon_cur_state = mon_old_state; + current_runstate(hDB, &mon_cur_state); +#ifdef MIDEBUG + printf(" current_runstate(%d) \n", mon_cur_state); +#endif + if (mon_old_state == 0) { + mon_old_state = mon_cur_state; + mon_update_old_rns = TRUE; + } + + if (mon_old_number != mon_cur_number) { + INT moncurnumber,moncurstate; + + /* save new number and state as they will be overwritten */ + moncurnumber = mon_cur_number; + moncurstate = mon_cur_state; + + if ((mon_old_state == STATE_RUNNING) ||(mon_old_state == STATE_PAUSED)) + mon("END_RUN",mon_old_number,mon_old_state,"RCHANGE"); + + if ((moncurstate == STATE_RUNNING)||(moncurstate == STATE_PAUSED)) + mon("BEGIN_RUN",moncurnumber,moncurstate,"RCHANGE"); + + if (moncurstate == STATE_PAUSED) + mon("PAUSE_RUN",moncurnumber,moncurstate,"RCHANGE"); + + mon_cur_number = moncurnumber; /* restore */ + mon_cur_state = moncurstate; + mon_update_old_rns = TRUE; + + } else if (mon_old_state != mon_cur_state) { + + if (mon_cur_state == STATE_PAUSED) { + + if (mon_old_state != STATE_RUNNING) + mon("BEGIN_RUN", mon_cur_number, mon_cur_state, "RCHANGE"); + + mon("PAUSE_RUN", mon_cur_number, mon_cur_state,"RCHANGE"); + + } else if (mon_cur_state == STATE_RUNNING) { + + if (mon_old_state == STATE_STOPPED) + mon("BEGIN_RUN", mon_cur_number, mon_cur_state, "RCHANGE"); + else if (mon_old_state == STATE_PAUSED) + mon("RESUME_RUN", mon_cur_number, mon_cur_state, "RCHANGE"); + + } else if (mon_cur_state == STATE_STOPPED) { + if ((mon_old_state == STATE_RUNNING)||(mon_old_state == STATE_PAUSED)) + mon("END_RUN", mon_cur_number, mon_cur_state,"RCHANGE"); + } + mon_update_old_rns = TRUE; + } + + mon_last_checked = ss_millitime(); + + } /* if not special cmd, check run number and run state periodically */ + + /* -- run number was not specified */ + if (runnumber == -1) { + runnumber = mon_cur_number; + /* get current number if not initialised */ + if (runnumber == -1) current_runnumber(hDB,&runnumber); + } + + /* -- run state was not specified */ + if (runstate == -1) { + runstate = mon_cur_state; + /* get current state if not initialised */ + if (runstate == 0) current_runstate(hDB,&runstate); + } + + /* -- execute command */ + if (strcmp(cmd,"MON_INIT") == 0) { + + mon_do("MON_INIT",runnumber,runstate,"MON_INIT"); + + if ((runstate == STATE_RUNNING) || (runstate == STATE_PAUSED)) + mon("BEGIN_RUN",runnumber,runstate,"MON_INIT"); + + if (runstate == STATE_PAUSED) + mon("PAUSE_RUN",runnumber,runstate,"MON_INIT"); + + } else if (strcmp(cmd,"MON_EXIT") == 0) { + + mon("END_RUN",runnumber,runstate,"MON_EXIT"); + mon_do("MON_EXIT",runnumber,runstate,"MON_EXIT"); + + } else if (strcmp(cmd,"BEGIN_RUN") == 0) { + + mon_do("BEGIN_RUN",runnumber,runstate,icmd); + + } else if (strcmp(cmd,"END_RUN") == 0) { + + mon_do("END_RUN",runnumber,runstate,icmd); + + } else if (strcmp(cmd,"PAUSE_RUN") == 0) { + + mon_do("PAUSE_RUN",runnumber, runstate, icmd); + + } else if (strcmp(cmd,"RESUME_RUN") == 0) { + + mon_do("RESUME_RUN",runnumber, runstate, icmd); + + } else if (strcmp(cmd,"LOOP") == 0) { + + mon_do("LOOP",runnumber, runstate, icmd); + + } else { + cm_msg(MERROR,"mon","Unknown command %s",cmd); + status = CM_SET_ERROR; + } + + /* run number has changed */ + if ((icmd != NULL) && (strcmp(icmd,"RCHANGE")==0)) { + mon_cur_number = runnumber; + mon_cur_state = runstate; + mon_update_old_rns = TRUE; + } + + /* if update flag update old run number and state */ + if (mon_update_old_rns) { + mon_old_number = mon_cur_number; + mon_old_state = mon_cur_state; + mon_update_old_rns = FALSE; + } + + + } else { + cm_msg(MERROR,"mon","NULL pointer or empty string supplied as command"); + } +#ifdef MIDEBUG + printf("<< mon(%s)\n",(cmd==NULL)?"NULL":cmd); +#endif + return status; +} + +/*------------------------------------------------------------------*/ + +BOOL file_exists(char *fname){ +/********************************************************************\ + + Routine: file_exists + + Purpose: check if file exists + + Input: + char *fname file name + + Output: + NONE + + Function value: + BOOL TRUE : File exists + FALSE : File does not exist or error + +\********************************************************************/ + + BOOL status; + struct stat statbuf; + + status = FALSE; + +#ifdef MIDEBUG1 + printf(" >>file_exists(%s)\n",fname); +#endif + + if (stat(fname,&statbuf) != -1) { /* use stat to get file info */ + +#ifdef MIDEBUG1 + printf(" file %s exists\n",fname); +#endif + status = TRUE; + + } else { + +#ifdef MIDEBUG2 + printf(" errno = %d \n", errno); +#endif + if (errno != ENOENT) { /* file not found */ + cm_msg(MERROR,"file_exists","stat() returned errno = %d\n",errno); + } else { +#ifdef MIDEBUG1 + printf(" file %s does not exist\n",fname); +#endif + } + + } + + return status; +} + +/*------------------------------------------------------------------*/ +BOOL mon_file_exists(INT cyear, INT cmonth, INT cday){ +/********************************************************************\ + + Routine: mon_file_exists + + Purpose: check if mon file exists + when file exists mon_monfile_name will be updated + + Input: + INT cyear year + INT cmonth month + INT cday day + + Output: + NONE + + Function value: + BOOL TRUE : File exists + FALSE : File does not exist or error + +\********************************************************************/ + + BOOL status; + char fname[NAME_LENGTH]; + + sprintf(fname,"%4.4d_%2.2d_%2.2d.mon",cyear,cmonth,cday); +#ifdef MIDEBUG1 + printf(" >>mon_file_exists(%s)\n",fname); +#endif + + if (strcmp(fname,mon_monfile_name) != 0) { /* check if current */ + status = file_exists(fname); + if (status) strcpy(mon_monfile_name,fname); /* exists->now current */ + } else { + status = TRUE; + } + return status; +} + +/*------------------------------------------------------------------*/ +BOOL rlog_file_exists(INT runnumber){ +/********************************************************************\ + + Routine: rlog_file_exists + + Purpose: check if run specific mon log file exists + when file exists mon_rlogfile_name will be updated + + Input: + INT runnumber run number + + Output: + NONE + + Function value: + BOOL TRUE : File exists + FALSE : File does not exist or error + +\********************************************************************/ + + BOOL status; + char fname[NAME_LENGTH]; + + sprintf(fname,"run_%4.4d.mon",runnumber); +#ifdef MIDEBUG1 + printf(" >>rlog_file_exists(%s)\n",fname); +#endif + if (strcmp(fname,mon_rlogfile_name) != 0) { /* check if current */ + status = file_exists(fname); + if (status) strcpy(mon_rlogfile_name,fname); /* exists->now current */ + } else { + status = TRUE; + } + return status; +} + +/*------------------------------------------------------------------*/ +FILE *mon_monfile_open(INT cyear, INT cmonth, INT cday){ +/********************************************************************\ + + Routine: mon_monfile_open + + Purpose: open mon file to append text + + Input: + INT cyear year + INT cmonth month + INT cday day + + Output: + NONE + + Function value: + FILE* <> NULL : File opened for append + NULL : ERROR + +\********************************************************************/ + + FILE *pfile; + char fname[NAME_LENGTH]; + INT itry; + + sprintf(fname,"%4.4d_%2.2d_%2.2d.mon",cyear,cmonth,cday); +#ifdef MIDEBUG1 + printf(" >> mon_monfile_open(%s)\n",fname); +#endif + + itry = 0; + +try_again: ; + if ((pfile = fopen(fname,"a")) != NULL) { + strcpy(mon_monfile_name,fname); /* now current */ + } else { + itry++; + if (itry < 10) { + ss_sleep(100); + goto try_again; + } + cm_msg(MERROR,"mon_monfile_open","ERROR Opening mon file %s",fname); + } +#ifdef MIDEBUG1 + printf(" << mon_monfile_open()\n"); +#endif + return pfile; +} + +/*------------------------------------------------------------------*/ +FILE *mon_rlogfile_open(INT runnumber){ +/********************************************************************\ + + Routine: mon_rlogfile_open + + Purpose: open run specific mon file to append text + + Input: + INT runnumber run number + + Output: + NONE + + Function value: + FILE* <>NULL : File openend for append + NULL : Error + +\********************************************************************/ + + FILE *pfile; + char fname[NAME_LENGTH]; + INT itry; + + sprintf(fname,"run_%4.4d.mon",runnumber); +#ifdef MIDEBUG1 + printf(" >> mon_rlogfile_open(%s)\n",fname); +#endif + + itry = 0; + +try_again: ; + if ((pfile = fopen(fname,"a")) != NULL) { + strcpy(mon_rlogfile_name,fname); /* now current */ + } else { + itry++; + if (itry < 10) { + ss_sleep(100); + goto try_again; + } + cm_msg(MERROR,"mon_rlogfile_open","ERROR Opening rlog file %s",fname); + } +#ifdef MIDEBUG1 + printf(" << mon_rlogfile_open()\n"); +#endif + return pfile; +} + +/*------------------------------------------------------------------*/ +INT mon_file_text(INT cyear, INT cmonth, INT cday, char *msg, + INT rnum){ +/********************************************************************\ + + Routine: mon_file_text + + Purpose: write text to mon and rlog log files + + Input: + INT cyear year of log file + INT cmonth month of log file + INT cday day of log file + char *msg message to be appended + INT rnum run number of rlog log file + + Output: + NONE + + Function value: + INT SS_SUCCESS : Success + +\********************************************************************/ + + INT status; + +#ifdef MIDEBUG1 + printf(" >> mon_file_text(%4.4d,%2.2d,%2.2d,%4.4d)\n",cyear,cmonth,cday,rnum); +#endif + if ((msg != NULL) && (strlen(msg)>0)) { + time_t ti; + struct tm* lt; + + time(&ti); + if ((lt = localtime(&ti)) != NULL) { + char line[1024]; + + if ((cyear != 0) && (cmonth != 0) && (cday != 0)) { + FILE *pfile; + + if (!mon_file_exists(cyear,cmonth,cday)) { + + if ((pfile=mon_monfile_open(cyear,cmonth,cday)) != NULL) { + + /* write file header */ + sprintf(line,"!M File = %s\n",mon_monfile_name); + fputs(line,pfile); + fputs("!\n",pfile); + fputs("! Record format is : \n",pfile); + fputs("!\n",pfile); + fputs("!