146 lines
3.5 KiB
C++
146 lines
3.5 KiB
C++
#include "StdAfx.h"
|
|
#include "_log.h"
|
|
#include "_error.h"
|
|
#include "_app.h"
|
|
//***********************************************************************
|
|
C_log::C_log(void)
|
|
{
|
|
nombre[0]=0;
|
|
path[0]=0;
|
|
archivo_log=-1;
|
|
|
|
log_ext=NULL;
|
|
l_lisener=NULL;
|
|
}
|
|
//***********************************************************************
|
|
C_log::~C_log(void)
|
|
{
|
|
}
|
|
//***********************************************************************
|
|
|
|
void C_log::loguea(__time64_t tiemp, char *modulo, char *fmt)
|
|
{
|
|
char nfile[256], str[2048];
|
|
int k;
|
|
char mod[31];
|
|
CTime tiempo;
|
|
OFSTRUCT sarchivo;
|
|
CString texto;
|
|
char tiem[20];
|
|
|
|
if (log_ext)
|
|
log_ext(tiemp,modulo,fmt);
|
|
if (l_lisener)
|
|
l_lisener->log_ext(tiemp,modulo,fmt);
|
|
if (!nombre[0])
|
|
return;
|
|
|
|
cerrojo.entro();
|
|
//******************************
|
|
//TODO
|
|
/*va_start(arg_ptr, fmt);
|
|
_vsnprintf(buf,4090, fmt, arg_ptr);
|
|
va_end(arg_ptr);*/
|
|
|
|
texto = fmt;
|
|
tiempo = CTime::GetCurrentTime();
|
|
// Actualizar el fichero de LOG
|
|
//EnterCriticalSection(&sincro);
|
|
if (archivo_log < 0)
|
|
{
|
|
// Abrir fichero de LOG
|
|
nb=0;
|
|
if(path[0])
|
|
sprintf (nfile, "%s\\%s_%04ld_%02ld_%02ld_h%02ld.log", path,nombre,
|
|
tiempo.GetYear(),tiempo.GetMonth(),tiempo.GetDay(),tiempo.GetHour());
|
|
else
|
|
sprintf (nfile, "%s_%04ld_%02ld_%02ld_h%02ld.log", nombre,
|
|
tiempo.GetYear(),tiempo.GetMonth(),tiempo.GetDay(),tiempo.GetHour());
|
|
archivo_log = OpenFile( nfile, &sarchivo, OF_EXIST);
|
|
if ( archivo_log != -1)
|
|
{
|
|
archivo_log = OpenFile( nfile, &sarchivo, OF_WRITE);
|
|
SetFilePointer((HANDLE) (__int64) archivo_log,0,0,FILE_END);
|
|
}
|
|
else
|
|
{
|
|
archivo_log = OpenFile( nfile, &sarchivo, OF_CREATE);
|
|
}
|
|
}
|
|
if (archivo_log >= 0)
|
|
{
|
|
k = (int) strlen(texto);
|
|
int max = 2048 - 128;
|
|
if (k > (2048 - 128))
|
|
texto.SetAt(2048 - 128,0);
|
|
|
|
int st=(int)strlen(modulo);
|
|
for (int i=0; i<30; i++)
|
|
{
|
|
if (i<st)
|
|
mod[i]=*(modulo+i);
|
|
else
|
|
mod[i]=' ';
|
|
}
|
|
mod[30]='\0';
|
|
|
|
strftime(tiem, 20, "%Y-%m-%d %H:%M:%S", localtime(&tiemp));
|
|
sprintf_s(str,2048, "%s %-12s %s\r\n",
|
|
(char *) LPCTSTR (tiem),
|
|
(char *) LPCTSTR (mod),
|
|
(char *) LPCTSTR (texto));
|
|
nb+=(int)strlen(str);
|
|
_lwrite(archivo_log,str,(int)strlen(str));
|
|
|
|
if (nb>52428800)//mallor de 50 megas
|
|
{
|
|
strcpy(str,"Cierre de archivo, se creara uno nuevo para logear\r\n");
|
|
_lwrite(archivo_log,str,(int)strlen(str));
|
|
k = GetFileSize ((HANDLE) (__int64) archivo_log, NULL);
|
|
if (k > 200)
|
|
{
|
|
_lclose(archivo_log);
|
|
archivo_log = -1;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
path[0]=0;
|
|
cerrojo.salgo();
|
|
}
|
|
//***********************************************************************
|
|
void C_log::loguea( char *modulo, char *fmt )
|
|
{
|
|
loguea(_time64(NULL),modulo,fmt);
|
|
}
|
|
//***********************************************************************
|
|
void C_log::loguea( C_error *er )
|
|
{
|
|
loguea(_time64(NULL),er->modulo,er->msg);
|
|
}
|
|
//***********************************************************************
|
|
void C_log::log( char *modulo, char *fmt,... )
|
|
{
|
|
if (C_app::GetApp())
|
|
{
|
|
char buf[1024];
|
|
int k;
|
|
va_list arg_ptr;
|
|
va_start(arg_ptr, fmt);
|
|
k = _vsnprintf(buf, 1020, fmt, arg_ptr);
|
|
va_end(arg_ptr);
|
|
if (k<0)
|
|
strcpy(&buf[1020], "...");
|
|
C_app::GetApp()->log.loguea(modulo, buf);
|
|
}
|
|
}
|
|
//***********************************************************************
|
|
void C_log::log( C_error *er )
|
|
{
|
|
if (C_app::GetApp())
|
|
{
|
|
C_app::GetApp()->log.loguea(_time64(NULL),er->modulo,er->msg);
|
|
}
|
|
}
|
|
//***********************************************************************
|