Reputation: 177
I am using this code for generating log files with timestamp so that each time when the procedure called by one process it will generate unique log filename. But the concern is that when two processes calling the same procedure at a time then the log files got over written. I don't want this. I want to find when procedures called by multiple processes and need to generate different log file name.
DEFINE VARIABLE TimeStamp AS CHARACTER NO-UNDO.
DEFINE VARIABLE cPath AS CHARACTER NO-UNDO.
ASSIGN
TimeStamp = string(month(today),"99") + string(day(today),"99") +~
string(year(today),"9999") + substring(string(time,"hh:mm:ss"),1,2) +~
substring(string(time,"hh:mm:ss"),4,2) +~
substring(string(time,"hh:mm:ss"),7,2)
cPath = "cim_" + STRING(TimeStamp) + ".log".
PROCEDURE generatecimlogfile:
OUTPUT TO VALUE(cPath).
MESSAGE "Inside a file".
END PROCEDURE.
RUN generatecimlogfile. /*(This procedure called by multiple processes at a time).*/
Upvotes: 1
Views: 662
Reputation: 1217
Instead of building a timestamp down to the second, you can build it to the millisecond. The NOW
function will give you a datetime to the millisecond. This code will return such a timestamp:
DEFINE VARIABLE cTimestamp AS CHARACTER NO-UNDO.
RUN createTimestamp (OUTPUT cTimestamp).
MESSAGE cTimestamp VIEW-AS ALERT-BOX INFORMATION.
PROCEDURE createTimestamp :
DEFINE OUTPUT PARAMETER pcTimeStr AS CHARACTER NO-UNDO.
DEFINE VARIABLE cNow AS CHARACTER NO-UNDO.
DEFINE VARIABLE dtDate AS DATE NO-UNDO.
DEFINE VARIABLE cDate AS CHARACTER NO-UNDO.
DEFINE VARIABLE cTime AS CHARACTER NO-UNDO.
cNow = STRING(NOW, "99/99/9999 HH:MM:SS.SSS").
cDate = ENTRY(1, cNow, " ").
ASSIGN dtDate = DATE(cDate) NO-ERROR.
cTime = ENTRY(2, cNow, " ").
cTime = REPLACE(cTime, ":", "").
cTime = REPLACE(cTime, ".", "").
cTime = REPLACE(cTime, ",", "").
ASSIGN pcTimeStr = STRING(YEAR(dtDate), "9999") + STRING(MONTH(dtDate), "99") + STRING(DAY(dtDate), "99") + cTime NO-ERROR.
IF pcTimeStr = ? THEN pcTimeStr = "00000000000000000".
END PROCEDURE.
If that's not good enough, you can guarantee a unique filename by adding a GUID to the timestamp:
DEFINE VARIABLE cTimestamp AS CHARACTER NO-UNDO.
DEFINE VARIABLE cGUID AS CHARACTER NO-UNDO.
cGUID = GUID(GENERATE-UUID).
cTimestamp = "20200802123456789" + cGUID.
MESSAGE cTimestamp VIEW-AS ALERT-BOX INFORMATION.
Upvotes: 1