本文转载自公众号文章:https://mp.weixin.qq.com/s/IAf7xeOh5GjhTS1WiCuA8Q

SAP程序运行日志及选择界面的填充值_运行时间

假设你对自己的程序非常在乎,是一个程序优化爱好者,那么以下几个场景可能你会经常遇到:

1、我希望知道程序每一次运行最主要的取数部分用了多长时间
2、我希望知道用户每一次运行程序的时候选择屏幕都怎么填的
3、我希望知道某自开发程序的运行频率如何,每天运行多少次 
4、如果一个账号有多个人在用,我希望知道运行账号机器的机器名
5、找到运行时间比较长的报表,并能使用当初用户的选择界面值进行调试 6、如果是JOB运行的程序,我希望知道这个JOB的信息,最好是双击能跳到这个JOB的界面

如果你有这方面的需求,今天这个程序能帮到你。

首先来看日志查看界面:

SAP程序运行日志及选择界面的填充值_bc_02

F8执行,显示记录的程序日志,可以显示程序名、事务码、登录机器名。如果是JOB执行,则显示JOBNAME和JOBCOUNT和变式,然后是运行日期和运行时间、程序用时、运行人。

SAP程序运行日志及选择界面的填充值_程序运行_03

双击可以显示程序运行时候选择界面的值

SAP程序运行日志及选择界面的填充值_bc_04

点击执行按钮,可以跳到程序界面并填上记录的参数,看起来更直观

SAP程序运行日志及选择界面的填充值_程序运行_05

双击JOBNAME等,可以跳到JOB的界面:

SAP程序运行日志及选择界面的填充值_运行时间_06


下面是实现的步骤:

首先定义表ZREPLOG

SAP程序运行日志及选择界面的填充值_bc_07

MANDT

MANDT

RELID

RELID

GUID

CHAR27

SRTF2

SRTF2

PROG

PROGRAMM

TCODE

TCODE

UTERM

UTERM

JNAME

BTCJOB

JCONT

BTCJOBCNT

JVRNT

BTCVARIANT

ERDAT

ERDAT

ERZET

ERZET

ERNAM

ERNAM

SECDS

DEC8_3

PTABS

CHAR120

CLUSTR

SYBIN2

CLUSTD

SYLRAW

程序ZREPLOG:

*&---------------------------------------------------------------------*
*& ZREPLOG 记录程序运行选择界面值以及指定区间运行时间
*& Baitianzhen
*&---------------------------------------------------------------------*
REPORT zreplog NO STANDARD PAGE HEADING.

TABLES zreplog.
DATA: gt_fldct TYPE lvc_t_fcat,
      gt_fldcs TYPE lvc_t_fcat,
      gs_slayt TYPE lvc_s_layo,
      gs_varnt TYPE disvariant,
      gv_repid TYPE sy-repid.
DATA: BEGIN OF gs_out,
        guid  TYPE zreplog-guid,
        prog  TYPE zreplog-prog , "程序
        tcode TYPE zreplog-tcode, "事务代码
        uterm TYPE zreplog-uterm, "终端
        jname TYPE zreplog-jname, "JobName
        jcont TYPE zreplog-jcont, "JobCount
        jvrnt TYPE zreplog-jvrnt, "JOB变式
        erdat TYPE zreplog-erdat, "运行日期
        erzet TYPE zreplog-erzet, "时间
        secds TYPE zreplog-secds, "用时(秒)
        ernam TYPE zreplog-ernam, "运行人
        seled TYPE char1,
      END OF gs_out.
DATA: gt_out   LIKE TABLE OF gs_out.
DATA: gt_paral TYPE TABLE OF rsparamsl_255.
DATA: gt_eptab TYPE TABLE OF char30.
DATA: gv_stam1 TYPE timestampl,
      gv_stam2 TYPE timestampl,
      gv_sguid TYPE char27.

SELECT-OPTIONS: s_erdat FOR zreplog-erdat DEFAULT sy-datum ,
                s_prog  FOR zreplog-prog,
                s_tcode FOR zreplog-tcode,
                s_ernam FOR zreplog-ernam.

LOAD-OF-PROGRAM.
  APPEND 'GT_PARAL[]' TO gt_eptab.

START-OF-SELECTION.
  PERFORM getdata.
  PERFORM outdata.

*&---------------------------------------------------------------------*
*& getdata
*&---------------------------------------------------------------------*
FORM getdata.
  CLEAR: gt_out.
  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_out
    FROM zreplog
    WHERE prog  IN s_prog  AND
          tcode IN s_tcode AND
          erdat IN s_erdat AND
          ernam IN s_ernam AND
          srtf2 EQ 0.
  IF gt_out IS INITIAL.
    MESSAGE s000(oo) WITH '无数据'.
  ELSE.
    SORT gt_out BY prog erdat.
  ENDIF.
ENDFORM.                    "getdata

*&---------------------------------------------------------------------*
*&  outdata
*&---------------------------------------------------------------------*
FORM outdata.
  gs_slayt-zebra     = 'X'.
  gs_slayt-box_fname = 'SELED'.
  gs_varnt-report    = sy-repid.
  gv_repid           = sy-repid.

  PERFORM catset1 TABLES gt_fldct
                  USING: 'PROG ' 'ZREPLOG' 'PROG ' '程序'(f01),
                         'TCODE' 'ZREPLOG' 'TCODE' '事务代码'(f02),
                         'UTERM' 'ZREPLOG' 'UTERM' '终端'(f03),
                         'JNAME' 'ZREPLOG' 'JNAME' 'JOBNAME'(f04),
                         'JCONT' 'ZREPLOG' 'JCONT' 'JOBCOUNT'(f05),
                         'JVRNT' 'ZREPLOG' 'JVRNT' 'JOB变式'(f06),
                         'ERDAT' 'ZREPLOG' 'ERDAT' '运行日期'(f07),
                         'ERZET' 'ZREPLOG' 'ERZET' '时间'(f08),
                         'SECDS' 'ZREPLOG' 'SECDS' '用时(秒)'(f09),
                         'ERNAM' 'ZREPLOG' 'ERNAM' '运行人'(f10).

  gs_varnt-handle = 1.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      it_fieldcat_lvc          = gt_fldct
      i_save                   = 'A'
      is_variant               = gs_varnt
      is_layout_lvc            = gs_slayt
      i_callback_program       = gv_repid
      i_callback_user_command  = 'USRCOMD1'
      i_callback_pf_status_set = 'DTL_STATUS'
    TABLES
      t_outtab                 = gt_out.
ENDFORM.                    " outdata

*&--------------------------------------------------------------------*
*&      Form
*&--------------------------------------------------------------------*
FORM dtl_status USING rt_extab TYPE slis_t_extab ##CALLED.
  SET PF-STATUS 'STA_DTL' EXCLUDING rt_extab.
ENDFORM.                    "

*&--------------------------------------------------------------------*
*&      Form  dtl_status
*&--------------------------------------------------------------------*
FORM stab_status USING rt_extab TYPE slis_t_extab ##CALLED.
  SET PF-STATUS 'STA_PSHOW' EXCLUDING rt_extab.
ENDFORM.                    "dtl_status

*&--------------------------------------------------------------------*
*& SET Fieldcat
*&--------------------------------------------------------------------*
FORM catset1 TABLES t_fldcat
             USING pv_field pv_reftab pv_reffld pv_text.
  DATA: ls_fldcat TYPE lvc_s_fcat.

  ls_fldcat-fieldname =  pv_field.
  ls_fldcat-scrtext_l =  pv_text.
  ls_fldcat-coltext   =  pv_text.
  ls_fldcat-ref_table =  pv_reftab.
  ls_fldcat-ref_field =  pv_reffld.
  ls_fldcat-col_opt   = 'A'.
  APPEND ls_fldcat TO t_fldcat.
  CLEAR ls_fldcat.
ENDFORM.

*&--------------------------------------------------------------------*
*& USER_COMMAND
*&--------------------------------------------------------------------*
FORM usrcomd1 USING pv_ucomm TYPE sy-ucomm ##CALLED
                    pv_field TYPE slis_selfield.
  CASE pv_ucomm.
    WHEN '&IC1'.
      READ TABLE gt_out INTO gs_out INDEX pv_field-tabindex.
      CASE pv_field-fieldname.
        WHEN 'PROG'.
          CALL FUNCTION 'EDITOR_PROGRAM'
            EXPORTING
              display = 'X'
              program = gs_out-prog
            EXCEPTIONS
              OTHERS  = 1.
        WHEN 'JNAME' OR 'JCONT' OR 'JVRNT'.
          CHECK gs_out-jname IS NOT INITIAL.
          PERFORM show_job_sm37b USING gs_out-jname gs_out-jcont.
        WHEN OTHERS.
          CLEAR: zreplog,gt_paral[].
          IMPORT (gt_eptab) FROM DATABASE zreplog(rp)
                            TO zreplog ID gs_out-guid.

          gs_varnt-handle    = 2.
          gs_slayt-box_fname = ''.
          CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
            EXPORTING
              i_structure_name         = 'RSPARAMS'
              i_save                   = 'A'
              is_variant               = gs_varnt
              is_layout_lvc            = gs_slayt
              i_callback_program       = gv_repid
              i_callback_user_command  = 'USRCOMD2'
              i_callback_pf_status_set = 'STAB_STATUS'
            TABLES
              t_outtab                 = gt_paral.
      ENDCASE.
      RETURN.
    WHEN 'REFRESH'.
      PERFORM getdata.
  ENDCASE.
  pv_field-row_stable = 'X'.
  pv_field-col_stable = 'X'.
  pv_field-refresh    = 'X'.
ENDFORM.

*&---------------------------------------------------------------------*
*& 参数ALV界面USER_COMMAND
*&---------------------------------------------------------------------*
FORM usrcomd2 USING pv_ucomm LIKE sy-ucomm  ##CALLED
                    pv_field TYPE slis_selfield.
  CASE pv_ucomm.
    WHEN 'EXE' OR 'DEBG'.
      CHECK zreplog IS NOT INITIAL.

      IF pv_ucomm = 'DEBG'.
        sy-debug = 'Y'.
      ENDIF.
      SUBMIT (zreplog-prog) WITH SELECTION-TABLE gt_paral
                            VIA SELECTION-SCREEN AND RETURN.
      sy-debug = ''.
  ENDCASE.
ENDFORM.

*&---------------------------------------------------------------------*
*& get_second
*&---------------------------------------------------------------------*
FORM get_second USING stamp1 stamp2 CHANGING second.
  DATA: d1 TYPE d,
        d2 TYPE d,
        t1 TYPE t,
        t2 TYPE t,
        s1 TYPE p DECIMALS 6,
        s2 TYPE p DECIMALS 6.

  CONVERT TIME STAMP stamp1 TIME ZONE sy-zonlo INTO DATE d1 TIME t1.
  s1 = frac( stamp1 ).

  CONVERT TIME STAMP stamp2 TIME ZONE sy-zonlo INTO DATE d2 TIME t2.
  s2 = frac( stamp2 ).

  second = ( ( d2 - d1 ) * 86400 ) + t2 - t1 + s2 - s1.
ENDFORM.                    "get_second

*&---------------------------------------------------------------------*
*& show_job_sm37b
*&---------------------------------------------------------------------*
FORM show_job_sm37b USING jobname LIKE tbtcjob-jobname
                          jobcount LIKE tbtcjob-jobcount.
  DATA: BEGIN OF jobhead.
          INCLUDE STRUCTURE tbtcjob.
  DATA: END OF jobhead.

  DATA: BEGIN OF job_steplist OCCURS 10.
          INCLUDE STRUCTURE tbtcstep.
  DATA: END OF job_steplist.

  CALL FUNCTION 'BP_JOB_READ'
    EXPORTING
      job_read_jobname  = jobname
      job_read_jobcount = jobcount
      job_read_opcode   = '20'
    IMPORTING
      job_read_jobhead  = jobhead
    TABLES
      job_read_steplist = job_steplist
    EXCEPTIONS
      job_doesnt_exist  = 1
      OTHERS            = 99.
  CASE sy-subrc.
    WHEN 0.
    WHEN 1.
      MESSAGE e127(bt) WITH jobname.
    WHEN OTHERS.
      MESSAGE e155(bt) WITH jobname.
  ENDCASE.

  IF jobhead-newflag = 'O'.
    MESSAGE e182(bt) WITH jobname.
  ENDIF.

  CALL FUNCTION 'BP_JOB_EDITOR'
    EXPORTING
      job_editor_dialog = 'Y'
      job_editor_opcode = '12'
      job_head_input    = jobhead
    TABLES
      job_steplist      = job_steplist
    EXCEPTIONS
      OTHERS            = 99.
ENDFORM.

*&---------------------------------------------------------------------*
*&   PERFORM savelog(zreplog) USING sy-repid '' IF FOUND.
*&---------------------------------------------------------------------*
FORM savelog USING VALUE(pv_repid) VALUE(pv_ptabstr).
  DATA: lt_usr   TYPE TABLE OF uinfo WITH HEADER LINE,
        lv_tid   TYPE          sy-index.
  DATA: ls_jinfo TYPE tbtcm,
        ls_tbtcp TYPE tbtcp.
  DATA: lt_paras TYPE TABLE OF rsparams.

  CALL FUNCTION 'TH_USER_INFO'
    IMPORTING
      tid = lv_tid.
  CALL FUNCTION 'THUSRINFO'
    TABLES
      usr_tabl = lt_usr.
  READ TABLE lt_usr WITH KEY tid = lv_tid.

  IF sy-batch = 'X'.
    CALL 'GetRuntimeInfo'
      ID 'JOB' FIELD ls_jinfo.
    SELECT SINGLE * INTO ls_tbtcp
      FROM tbtcp
      WHERE jobname   = ls_jinfo-jobname  AND
            jobcount  = ls_jinfo-jobcount AND
            stepcount = ls_jinfo-stepcount.
  ENDIF.

  CALL FUNCTION 'RS_REFRESH_FROM_SELECTOPTIONS'
    EXPORTING
      curr_report         = pv_repid
    TABLES
      selection_table     = lt_paras
      selection_table_255 = gt_paral
    EXCEPTIONS
      not_found           = 1
      no_report           = 2
      OTHERS              = 3.
  IF sy-subrc = 0.
    GET TIME STAMP FIELD gv_stam1.
    WRITE gv_stam1 TIME ZONE sy-zonlo TO gv_sguid.

    zreplog-prog  = pv_repid.
    zreplog-tcode = sy-tcode.
    zreplog-uterm = lt_usr-term.
    zreplog-jname = ls_jinfo-jobname.
    zreplog-jcont = ls_jinfo-jobcount.
    zreplog-jvrnt = ls_tbtcp-variant.
    zreplog-erdat = sy-datum.
    zreplog-erzet = sy-uzeit.
    zreplog-ernam = sy-uname.
    zreplog-ptabs = pv_ptabstr.

    IF pv_ptabstr IS NOT INITIAL.
      TRANSLATE pv_ptabstr TO UPPER CASE.
      SPLIT pv_ptabstr AT space INTO TABLE gt_eptab.
    ENDIF.

    EXPORT (gt_eptab) TO DATABASE zreplog(rp)
                      FROM zreplog ID gv_sguid .
  ENDIF.
ENDFORM.                    "savelog

*&---------------------------------------------------------------------*
*& PERFORM updatelog(zreplog) IF FOUND.
*&---------------------------------------------------------------------*
FORM updatelog.
  CHECK gv_sguid IS NOT INITIAL.

  GET TIME STAMP FIELD gv_stam2.
  PERFORM get_second USING gv_stam1 gv_stam2
                     CHANGING zreplog-secds.
  UPDATE zreplog SET secds = zreplog-secds WHERE guid = gv_sguid.
ENDFORM.                    "updatelog

SAP程序运行日志及选择界面的填充值_程序运行_08

SAP程序运行日志及选择界面的填充值_bc_09


然后是如何在需要记录的程序添加记录语句。

在需要记录的程序开始执行的时候添加:
  PERFORM savelog(zreplog) USING sy-repid '' IF FOUND.

然后取数结束的时候添加:
  PERFORM updatelog(zreplog) IF FOUND.

则ZREPLOG记录的执行时间就是savelog和updatelog之间的执行时间,如果只有savelog没有updatelog语句,则不记录执行时间。

SAP程序运行日志及选择界面的填充值_bc_10


更多价值文章关注公众号:

SAP程序运行日志及选择界面的填充值_程序运行_11