create or replace package SvrTrace is
/*=======================================================================================================================*\
|* Package:     SvrTrace (SPECIFICATION)
|*-------------------------------------------------------------------------------------------------------------------------*
|* Descrizione: Produzione file di trace da RDBMS
|*-------------------------------------------------------------------------------------------------------------------------*
*/

   /*====================================================================================================================*\
   |* Funzione:    OpenFile
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Apertura del file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Parametri:   logdir  [IN] [varchar2]: path per il file di trace
   |*              logfile [IN] [varchar2]: nome del file di trace (valore di default 'trace')
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure OpenFile (logdir varchar2, logfile varchar2 default 'trace', logmode varchar2 default 'a', logdate boolean default TRUE);

   /*====================================================================================================================*\
   |* Funzione:    CloseFile
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Chiusura del file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Parametri:
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure CloseFile;

   /*====================================================================================================================*\
   |* Funzione:    Flush
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Chiusura del file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Parametri:   Flush buffer
   |*--------------------------------------------------------------------------------------------------------------------*
   */

   procedure Flush;

   procedure newline;


   /*====================================================================================================================*\
   |* Funzione:    Write
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Scrittura di una stringa sul file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Parametri:   message [IN] [varchar2]: stringa da scrivere sul file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure Write    (message varchar2, timestamp boolean default true);

   /*====================================================================================================================*\
   |* Funzione:    TraceError
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Tracciamento errori
   |*--------------------------------------------------------------------------------------------------------------------*
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure TraceError (module varchar2, details varchar2 default null, errcode number default sqlcode, errmsg varchar2 default sqlerrm, timestamp boolean default true);

end SvrTrace;
/
create or replace package body SvrTrace is
/*=======================================================================================================================*\
|* Package:     SvrTrace (SPECIFICATION)
|*-------------------------------------------------------------------------------------------------------------------------*
|* Descrizione: Produzione file di trace da RDBMS
|*-------------------------------------------------------------------------------------------------------------------------*


   /*=======================================================================================================================*\
   |* Oggetti privati
   \*=========================================================================================================================*/
   log_fname varchar2 (1000);   -- Nome del file di trace
   log_dname varchar2 (1000);    -- Path del file di trace
   log_fpntr UTL_FILE.File_Type; -- Puntatore al file di trace
   head_sep  varchar2 (120)  := Lpad ('=', 120, '=');
   logf_sep  varchar2 (120)  := Lpad ('-', 120, '-');
   logf_ind  varchar2 (30)   := '                    ';
   opn_form  varchar2 (2000) := NULL;

   /*====================================================================================================================*\
   |* Funzione:    GetTimeStamp
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Ritorna un timestamp nel formato 'yyyymmdd-hh24:mi:ss.cc' (dove cc = centesimi di secondo)
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   function GetTimeStamp return varchar2 is
      vdate         date;
      hsecs         number := 0;
   begin
      hsecs := Mod (Dbms_Utility.Get_Time, 100);
      vdate := sysdate;

      if hsecs < 0 then
         hsecs := hsecs + 100;
      end if;
      return To_Char (vdate, 'yyyymmdd-hh24:mi:ss') || '.' || To_Char (hsecs, 'FM09');
   end;

   /*====================================================================================================================*\
   |* Procedure:   WriteHeader
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Stampa l'header del file di log
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Parametri:
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure WriteHeader is
   begin
      if UTL_FILE.Is_Open (log_fpntr) then
         UTL_FILE.Putf (log_fpntr, '<<%s>>\n', To_Char (sysdate, 'yyyy.mm.dd'));
         UTL_FILE.Putf (log_fpntr, '%s\n', head_sep);
         UTL_FILE.Putf (log_fpntr, 'Trace file:             %s\n', log_fname);
         UTL_FILE.Putf (log_fpntr, 'Data di apertura:       %s\n', GetTimeStamp);
         UTL_FILE.Putf (log_fpntr, '%s\n', logf_sep);
      end if;
   end;

   /*====================================================================================================================*\
   |* Funzione:    OpenFile
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Apertura del file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Parametri:   logdir  [IN] [varchar2]: path per il file di trace
   |*              logfile [IN] [varchar2]: nome del file di trace (valore di default 'trace')
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure OpenFile (logdir varchar2, logfile varchar2 default 'trace', logmode varchar2 default 'a', logdate boolean default TRUE) is

      lfnsuf varchar2 (30) := '.log'; -- suffisso del nome del file di trace
   begin
      if logdate then
         null;
      elsif Instr (logfile, '.') = 0 then
         lfnsuf := '.log';
      end if;

      if logfile is not null then
         log_fname := logfile || lfnsuf;

         -------------------------------------------------------------------------------------------------------------------
         -- Se log_fname h impostato apro il file di log e Stampo l'intestazione nel file di trace
         -------------------------------------------------------------------------------------------------------------------
         if Not UTL_FILE.Is_Open (log_fpntr) then
         begin


            log_fpntr := UTL_FILE.Fopen (logdir, log_fname, logmode);
            WriteHeader;
         exception
            when UTL_FILE.INVALID_PATH then
               DBMS_OUTPUT.Put_Line ('Exception: UTL_FILE.INVALID_PATH');
            when UTL_FILE.INVALID_MODE then
               DBMS_OUTPUT.Put_Line ('Exception: UTL_FILE.INVALID_MODE');
            when UTL_FILE.INVALID_FILEHANDLE then
               DBMS_OUTPUT.Put_Line ('Exception: UTL_FILE.INVALID_FILEHANDLE');
            when UTL_FILE.INVALID_OPERATION then
               DBMS_OUTPUT.Put_Line ('Exception: UTL_FILE.INVALID_OPERATION');
            when UTL_FILE.INTERNAL_ERROR then
               DBMS_OUTPUT.Put_Line ('Exception: UTL_FILE.INTERNAL_ERROR');
            when others then
               DBMS_OUTPUT.Put_Line (To_Char (sqlcode) || '-' || sqlerrm);
               log_fname := null;
               if UTL_FILE.Is_Open (log_fpntr) then
                  UTL_FILE.Fclose (log_fpntr);
               end if;
         end;
         end if;
      end if;
   end OpenFile;
   -----------------------------------------------------------------------------------------------------------------------

   /*====================================================================================================================*\
   |* Funzione:    CloseFile
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Chiusura del file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure CloseFile is
   begin
      UTL_FILE.Putf (log_fpntr, '[%s] - %s\n', GetTimeStamp, 'Chiusura file di trace');
      UTL_FILE.Fclose (log_fpntr);
      log_fname := null;
   end CloseFile;
   -----------------------------------------------------------------------------------------------------------------------


    /*====================================================================================================================*\
   |* Funzione:    Flush
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Chiusura del file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   */

   procedure Flush is
   begin
      UTL_FILE.fflush(log_fpntr);
   end Flush;

   -----------------------------new ----------------( not necessary )

   procedure newline is
   begin
      UTL_FILE.new_line(log_fpntr,6);
   end newline;



   /*====================================================================================================================*\
   |* Funzione:    Write
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Scrittura di una stringa sul file di trace
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure Write    (message varchar2, timestamp boolean default true) is
   begin
      if UTL_FILE.Is_Open (log_fpntr) then
         if timestamp then
            UTL_FILE.Putf (log_fpntr, '[%s] - %s\n', GetTimeStamp, message);
         else
            UTL_FILE.Putf (log_fpntr, ' %s  - %s\n', logf_ind,     message);
         end if;
      end if;
   end Write;
   -----------------------------------------------------------------------------------------------------------------------

   /*====================================================================================================================*\
   |* Funzione:    TraceError
   |*--------------------------------------------------------------------------------------------------------------------*
   |* Descrizione: Tracciamento errori
   |*--------------------------------------------------------------------------------------------------------------------*
   */
   procedure TraceError (module varchar2, details varchar2 default null, errcode number default sqlcode, errmsg varchar2 default sqlerrm, timestamp boolean default true) is
   begin
      if UTL_FILE.Is_Open (log_fpntr) then
         if timestamp then
            UTL_FILE.Putf (log_fpntr, '[%s] - ECCEZIONE: %s - %s [modulo %s (%s)]\n', GetTimeStamp, To_Char (errcode, '999990'), errmsg, module, details);
         else
            UTL_FILE.Putf (log_fpntr, 'ECCEZIONE: %s - %s [modulo %s (%s)]\n', To_Char (errcode, '999990'), errmsg, module, details);
         end if;
      end if;
   end TraceError;
   -----------------------------------------------------------------------------------------------------------------------

end SvrTrace;
/
