/*
*========================================================================
*
*   SYSTEM   : AZN_PLAZA_LANE LIBRARY
*   FILE     : lb_err_log.c
*   Progrmer : JBY (baarong@neighbor21.co.kr)
*   Date     : 2018.09.05
*   Modified :
*   Remark   :
 *              
 *========================================================================
*/

/* Standard includes */
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>

#include <errno.h>
#include <sys/stat.h>
#include <time.h>

/* User includes */
#include "def_set.h"
#include "dir_path.h"
#include "file_no.h"
#include "file_lb.h"  /* 공통 Library Prototypes */
 
#include "errlog.h"
#include "path_def.h"
#include "lb_func_list.h"
/* Global Declarations */

#define   DBG_CRT   1
#define   BEEP            printf ("\07")

/*
 *========================================================================
 *
 *   Function : lb_debug_log_dump()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : char *fmt; //-- ex: "%s:%d:%x", "abc", 1, 20
 *
 *========================================================================
*/

void lb_debug_log_dump (char *fmt, ...)
{
  va_list ap;

  if (DBG_CRT != 1) return;
  fprintf (stdout, "%s ", exeFileName);
  va_start (ap, fmt);
  vfprintf (stdout, fmt, ap);
  va_end (ap);
  fflush (stdout);
}

/*
 *========================================================================
 *
 *   Function : lb_core_dump()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : char *fmt; //-- ex: "%s:%d:%x", "abc", 1, 20
 *              shell command is infocmp
 *========================================================================
*/

void lb_core_dump (char *fmt, ...)
{
  va_list ap;

  BEEP;
  printf ("[7m");
  fflush (stdout);
  fprintf (stdout, "%s ", exeFileName);
  va_start (ap, fmt);
  vfprintf (stdout, fmt, ap);
  va_end (ap);
  printf ("[0m");
  fflush (stdout);

  exit (-1);
}

/*
 *========================================================================
 *
 *   Function : lb_error_exit()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   :
 *
 *========================================================================
*/

void lb_error_exit (buf)
char *buf;
{
  perror (buf);
  exit (-1);
}


/*******************************************************************************
** 모듈 목록
** Function Prototypes 선언
void  lb_err_log(char *,char *,int);
*******************************************************************************/

/*
 *========================================================================
 *
 *   Function : lb_err_log()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.09.05
 *   Modified :
 *   Remark   : SVR PROGRAM ERROR LOGGING.
 *
 *========================================================================
*/

void  lb_err_log(icp_pgm_id, icp_err_msg, in_where)
char    *icp_pgm_id;
char  *icp_err_msg;
int    in_where;
{
    int       nfd;
    char      dir_path[128];
    char      log_path[128];
    char      dir_year[4+1];
    char      dir_mon[2+1];
    char      dir_day[2+1];
    time_t    current_time;

    struct    tm            *struct_time;
    struct    stat           st_stat;
    struct    write_info     wf;
    struct    error_log      err_log;

    FILE       *fp_Log;

    current_time   = time(0);
    struct_time = local_time(&current_time);

    memset(&wf, 0, sizeof(struct write_info));
    sprintf((char *)wf.year, "%04d", struct_time->tm_year + 1900);
    sprintf((char *)wf.mon, "%02d", struct_time->tm_mon + 1);
    sprintf((char *)wf.day, "%02d", struct_time->tm_mday);

    sprintf(dir_year, "%04d", struct_time->tm_year + 1900);
    sprintf(dir_mon, "%02d", struct_time->tm_mon + 1);
    sprintf(dir_day, "%02d", struct_time->tm_mday);

    sprintf(dir_path, "%s/%.4s%.2s%.2s", 
             LOG_DIR, dir_year, dir_mon, dir_day);

    if ((stat(dir_path, &st_stat) != 0) && (errno == ENOENT))
        mkdir(dir_path, 0755);

    if (in_where | LOG0)
    {
        if (in_where & LOG1)
        {

            sprintf(log_path, "%s/%.4s%.2s%.2s/ELOG.txt", LOG_DIR,
                    dir_year, dir_mon, dir_day);
   
            fp_Log = fopen(log_path, "a");

            if (fp_Log != NULL)
            {
                fprintf(fp_Log,"%02d:%02d:%02d %-8s %s\n",struct_time->tm_hour,
                        struct_time->tm_min, struct_time->tm_sec, icp_pgm_id, icp_err_msg);
   
                fclose(fp_Log);
            }
        }
       
        if (in_where & LOG2)
        {
            memset(&err_log, 0, sizeof(struct error_log));
            err_log.time = current_time;
            if (strlen(icp_pgm_id) >= sizeof(err_log.app_pg_job))
                memcpy(err_log.app_pg_job, icp_pgm_id, sizeof(err_log.app_pg_job));
            else
            {
                memset(err_log.app_pg_job, ' ', sizeof(err_log.app_pg_job));
                memcpy(err_log.app_pg_job, icp_pgm_id, strlen(icp_pgm_id));
            }
   
            if (strlen(icp_err_msg) > sizeof(err_log.log_msg))
                memcpy(err_log.log_msg, icp_err_msg, sizeof(err_log.log_msg));
            else
                strcpy(err_log.log_msg, icp_err_msg);

            nfd = lb_fopen(&wf, FNO_ERRORLOG, FINOUT+FC_MANULOCK);
            if (nfd == -ENOENT)
                nfd = lb_fbuild(&wf, FNO_ERRORLOG, FINOUT+FC_MANULOCK);
   
            if (nfd >= 0)
                lb_fwrite(nfd, FNO_ERRORLOG, &err_log, 0);
              
            lb_close(nfd);
        }
       
        if (in_where & LOG4)
        {
            sprintf(log_path, "%s/%s/%s_%04d%02d%02d.log", DIR_TCS, DIR_LOG, icp_pgm_id, struct_time->tm_year+1900, struct_time->tm_mon+1, struct_time->tm_mday);
   
            fp_Log = fopen(log_path, "a");
            if (fp_Log != NULL)
            {
                fprintf(fp_Log,"%04d/%02d/%02d %02d:%02d:%02d %s\n",
                        struct_time->tm_year+1900, struct_time->tm_mon+1, struct_time->tm_mday,
                        struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec, icp_err_msg);
   
                fclose(fp_Log);
            }
        }

        if (in_where & 0x08)
        {

            sprintf(log_path, "%s/%s/%s_%04d%02d%02d.log", DIR_TCS, DIR_LOG, icp_pgm_id, struct_time->tm_year+1900, struct_time->tm_mon+1, struct_time->tm_mday);
   
            fp_Log = fopen(log_path, "w");
            if (fp_Log != NULL)
            {
                fprintf(fp_Log,"%04d/%02d/%02d %02d:%02d:%02d %s\n",
                        struct_time->tm_year+1900, struct_time->tm_mon+1, struct_time->tm_mday,
                        struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec, icp_err_msg);
   
                fclose(fp_Log);
            }
        }
    }
}
