星期四, 11月 09, 2023

2016-12-26 Log message to IFS file with command LOGTOIFS


Log message to IFS file with command LOGTOIFS



File  : QRPGLESRC
Member: LOGTOIFS
Type  : RPGLE
Usage : CRTBNDRPG PGM(LOGTOIFS) SRCFILE(LIBxx/QRPGLESRC) SRCMBR(LOGTOIFS)		
        




      **===============================================================
      **  Command....... LogToIfs                                     =
      **  CPP........... LogToIfs RPGLE                               =
      **  Description... Log Message to IFS File                      =
      **===============================================================
      **  Date  : 2016/12/26                                          =
      **  Author: Vengoal Chang                                       =
      **===============================================================
      **                                                              =
      **  To compile:                                                 =
      **     CRTBNDRPG LOGTOIFS SRCFILE(lib/QRPGLESRC) DBGVIEW(*LIST) =
      **                                                              =
      **===============================================================

     H DftActGrp(*NO)
     H Debug  Option(*SrcStmt:*NoDebugIo)

      * PGM SDS
     D                SDS
     D psdsPgmName       *Proc
     D psdsPgmLib             81     90                                         * Program library
     D JobNam                244    253
     D JobUsr                254    263
     D JobNbr                264    269
     D CurUsr                358    367

      ** API call to open a stream file
      **
     D open            PR            10I 0 ExtProc('open')
     D  path                           *   value options(*string)
     D  openflags                    10I 0 value
     D  mode                         10U 0 value options(*nopass)
     D  ccsid                        10U 0 value options(*nopass)
     D  txtcreatid                   10U 0 value options(*nopass)

     D**********************************************************************
     D*  Flags for use in open()
     D*
     D* More than one can be used -- add them together.
     D**********************************************************************
     D*                                            Writing Only
     D O_WRONLY        C                   2
     D*                                            Create File if not exist
     D O_CREAT         C                   8
     D*                                            Truncate File to 0 bytes
     D O_TRUNC         C                   64
      *                                            Append to file
     D O_APPEND        C                   256
     D*                                            Convert text by code-page
     D O_CODEPAGE      C                   8388608
     D*                                            Convert text by ccsid
     D O_CCSID         C                        32
     D*                                            Open in text-mode
     D O_TEXTDATA      C                   16777216
      * Note: O_TEXT_CREAT requires all of the following flags to work:
      *           O_CREAT+O_TEXTDATA+(O_CODEPAGE or O_CCSID)
     D O_TEXT_CREAT    C                   33554432
     D*                                         owner authority
     D**********************************************************************
     D*      Mode Flags.
     D*         basically, the mode parm of open(), creat(), chmod(),etc
     D*         uses 9 least significant bits to determine the
     D*         file's mode. (peoples access rights to the file)
     D*
     D*           user:       owner    group    other
     D*           access:     R W X    R W X    R W X
     D*           bit:        8 7 6    5 4 3    2 1 0
     D*
     D* (This is accomplished by adding the flags below to get the mode)
     D**********************************************************************
     D S_IRUSR         C                   256
     D S_IWUSR         C                   128
     D S_IXUSR         C                   64
     D S_IRWXU         C                   448
     D*                                         group authority
     D S_IRGRP         C                   32
     D S_IWGRP         C                   16
     D S_IXGRP         C                   8
     D S_IRWXG         C                   56
     D*                                         other people
     D S_IROTH         C                   4
     D S_IWOTH         C                   2
     D S_IXOTH         C                   1
     D S_IRWXO         C                   7


      ** API call to write data to a stream file
      **
     D write           PR            10I 0 extproc('write')
     D   fildes                      10I 0 value
     D   buf                           *   value
     D   nbyte                       10U 0 value

      ** API call to close a stream file
      **
     D close           PR            10I 0 extproc('close')
     D   fildes                      10I 0 value

     D @__ERRNO        PR              *   EXTPROC('__errno')

     D STRERROR        PR              *   EXTPROC('strerror')
     D    ERRNUM                     10I 0 VALUE

     D ERRNO           PR            10I 0

     D DIE             PR
     D   PEMSG                      256A   CONST

     D GetCaller       PR
     D  CallingPgmNam                10
     D  CallingPgmLib                10

     D fd              S             10I 0
     D data            S           4096A
     D IfsPath         S            256A
     D Curtime         S               Z

     D PgmNam          S             10
     D PgmLib          S             10
     D Proc            S             32

      * Program parameters - title and page length in lines
     D paIfsFile       S             64
     D paPath          S            256
     D paMessage       S           2048
     D paIncludeJob    S              4

      * Program parameters

     C     *Entry        Plist
     C                   Parm                    paIfsFile
     C                   Parm                    paPath
     C                   Parm                    paMessage
     C                   Parm                    paIncludeJob

     c                   eval      *inlr = *on

     C                   eval      IfsPath = %trim(paPath) + '/' +
     C                                        %trim(paIfsFile)

     C* Create an empty file
     c                   eval      fd = open(%trim(IfsPath)
     c                                  : O_CREAT + O_APPEND +  O_WRONLY
     c                                            + O_CCSID + O_TEXT_CREAT
     c                                            + O_TEXTDATA
     c                                  : S_IWUSR+S_IRUSR+S_IRGRP+S_IROTH
     c                                  : 0
     c                                  : 0 )

     c                   if        fd < 0
     c                   callp     die('open(): ' + %Char(ERRNO) + ' ' +
     c                                 %trim(IfsPath) + ' ' +
     c                                              %STR(STRERROR(ERRNO)))
     c                   return
     c                   endif

     C                   Time                    Curtime
     C                   If        paIncludeJob = '*YES'
     C                   callp     GetCaller (  PgmNam
     C                                        : PgmLib
     C                                       )
     C                   eval      data = %SubSt(%Char(Curtime):1:23)+ ' ' +
     C                                    JobNam + ' ' +
     C                                    JobUsr + ' ' +
     C                                    JobNbr + ' ' +
     C                                    PgmLib + ' ' +
     C                                    PgmNam + ' ' +
     C                                    %trimR(paMessage) + x'0D25'
     C                   Else
     C                   eval      data = %SubSt(%Char(Curtime):1:23)+ ' ' +
     C                                    %trimR(paMessage) + x'0D25'
     C                   EndIf
     C
     c                   callp     write(fd: %addr(data): %len(%trim(data)))

     C* Close the file:
     c                   callp     close(fd)

      **********************************************************************
      *  Get Caller with Retrieve Call Stack API
      **********************************************************************

     P GetCaller       B

     D GetCaller       PI
     D  CallingPgmNam                10
     D  CallingPgmLib                10

     D RtvCallStack    PR                  Extpgm('QWVRCSTK')
     D                             2000
     D                               10I 0
     D                                8    CONST
     D                               56
     D                                8    CONST
     D                               15

     D Var             DS          2000
     D  BytAvl                       10I 0
     D  BytRtn                       10I 0
     D  Entries                      10I 0
     D  Offset                       10I 0
     D  EntryCount                   10I 0
     D VarLen          S             10I 0 Inz(%size(Var))
     D ApiErr          S             15

     D JobIdInf        DS
     D  JIDQName                     26    Inz('*')
     D  JIDIntID                     16
     D  JIDRes3                       2    Inz(*loval)
     D  JIDThreadInd                 10I 0 Inz(1)
     D  JIDThread                     8    Inz(*loval)

     D Entry           DS           256
     D  EntryLen                     10I 0
     D  PgmNam                       10    Overlay(Entry:25)
     D  PgmLib                       10    Overlay(Entry:35)

     c                   eval      CallingPgmNam = *blanks
     c                   eval      CallingPgmLib = *blanks
     c                   callp     RtvCallStack (  Var
     c                                           : VarLen
     c                                           : 'CSTK0100'
     c                                           : JobIdInf
     c                                           : 'JIDF0100'
     c                                           : ApiErr
     c                                          )
     C                   Do        EntryCount
     C                   Eval      Entry = %subst(Var:Offset + 1)
     c                   if        CallingPgmNam = *blanks and
     c                             CallingPgmLib = *blanks
     c                   if        PgmNam = psdsPgmName and
     c                             PgmLib = psdsPgmLib
     C                   Else
     c                   eval      CallingPgmNam = Pgmnam
     c                   eval      CallingPgmLib = Pgmlib
     C                   Endif
     C                   Endif
     C                   Eval      Offset = Offset + EntryLen
     C                   Enddo
     C
     C                   Return
     P GetCaller       E

      **********************************************************************
      *  This ends this program abnormally, and sends back an escape.
      *   message explaining the failure.
      **********************************************************************

     P DIE             B

     D DIE             PI
     D   PeMsg                      256A   CONST

     D SndPgmMsg       PR                  ExtPgm('QMHSNDPM')
     D   MessageId                    7A   Const
     D   QualMsgF                    20A   Const
     D   MsgData                    256A   Const
     D   MsgDtaLen                   10I 0 Const
     D   MsgType                     10A   Const
     D   CallStkEnt                  10A   Const
     D   CallStkCct                  10I 0 Const
     D   MessageKey                   4A
     D   ErrorCode                32766A   Options(*VarSize)

     D Dsec            DS
     D  DsecBytesP             1      4I 0 Inz(256)
     D  DsecBytesA             5      8I 0 Inz(0)
     D  DsecMsgId              9     15
     D  DsecReserv            16     16
     D  DsecMsgDta            17    256

     D WWMsgLen        S             10I 0
     D WWTheKey        S              4A

     C                   EVAL      WWMsgLen = %Len(%TrimR(PeMsg))
     C                   IF        WWMsgLen<1
     C                   RETURN
     C                   ENDIF

     C                   Callp     SndPgmMsg('CPF9897': 'QCPFMSG   *LIBL':
     C                               PeMsg: WWMsgLen: '*ESCAPE':
     C                               '*PGMBDY': 1: WWTheKey: Dsec)

     C                   RETURN

     P DIE             E

      **********************************************************************
      *  This procedure return call socket C API errno
      **********************************************************************

     P ErrNo           B

     D ErrNo           PI            10I 0
     D P_EeeNo         S               *
     D WWReturn        S             10I 0 Based(P_Errno)
     C                   EVAL      P_Errno = @__Errno
     C                   RETURN    WWReturn

     P Errno           E




File  : QCMDSRC
Member: LOGTOIFS
Type  : CMD
Usage : CrtCmd      Cmd( LogToIfs  )	
                    Pgm( LogToIfs   )
                    SrcFile( QCMDSRC )					

       


/*  ===============================================================  */
/*  = Command....... LogToIfs                                     =  */
/*  = CPP........... LogToIfs RPGLE                               =  */
/*  = Description... Log Message to IFS File                      =  */
/*  =                                                             =  */
/*  = CrtCmd      Cmd( LogToIfs  )                                =  */
/*  =             Pgm( LogToIfs   )                               =  */
/*  =             SrcFile( QCMDSRC )                              =  */
/*  ===============================================================  */
/*  = Date  : 2016/12/26                                          =  */
/*  = Author: Vengoal Chang                                       =  */
/*  ===============================================================  */

             Cmd        Prompt('Log Message To IFS File')

             Parm       Kwd(ToStmf)                  +
                        Type(*Name) Len(64) Min(1)   +
                        Prompt('To stream file name')

             Parm       Kwd(ToDir)                   +
                        Type(*Pname) LEN(256) MIN(1) +
                        Prompt('To directory')

             Parm       Kwd(LogMsg)                  +
                        Type(*Char) Len(2048) Min(1) +
                        Prompt('Log message')

             Parm       Kwd(InCldJob)                +
                        Type(*Char) Len(4)           +
                        Rstd(*Yes)                   +
                        Dft(*No )                    +
                        Values(*YES *NO)             +
                        Prompt('Log include job info')

						
Usage example:

                       Log Message To IFS File (LOGTOIFS)                       
                                                                                
 Type choices, press Enter.                                                     
                                                                                
 To stream file name  . . . . . .                                               
                                                                                
 To directory . . . . . . . . . .                                               
                                                                                
 Log message  . . . . . . . . . .                                               
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                     ...        
 Log include job info . . . . . .   *NO           *YES, *NO                     
                                                                                

LOGTOIFS TOSTMF(AP1LOG.TXT) TODIR('/tmp') LOGMSG('test 2') INCLDJOB(*YES)
LOGTOIFS TOSTMF(AP1LOG.TXT) TODIR('/tmp') LOGMSG('test 3') INCLDJOB(*YES)
LOGTOIFS TOSTMF(AP1LOG.TXT) TODIR('/tmp') LOGMSG('test 3')
LOGTOIFS TOSTMF(AP1LOG.TXT) TODIR('/tmp') LOGMSG('test 4')

DSPF STMF('/tmp/AP1LOG.TXT')

 Browse : /tmp/AP1LOG.TXT                                                      
 Record :       1   of       4 by  14            Column :    1     59 by  79   
 Control :                                                                     
                                                                               
....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....
 ************Beginning of data**************                                   
2016-12-26-15.25.45.104 QPADEV0047 USRTEST    317148 QSYS       QUOCMD     test 2                    
2016-12-26-15.25.50.561 QPADEV0047 USRTEST    317148 QSYS       QUOCMD     test 3                    
2016-12-26-15.25.57.791 test 3                                                 
2016-12-26-15.26.02.863 test 4                                                 
 ************End of Data********************                                   






沒有留言: