blob: 0d090aa80970ba8708d2450002403620b981d063 [file] [log] [blame]
#include "installOS.h"
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static FILE *file = NULL;
static _TCHAR* logMessage = _T_INSTALL("Log file created ");
static _TCHAR* timeSepatator = _T_INSTALL(" | ");
static _TCHAR* lineSepatator = _T_INSTALL("\n");
static _TCHAR* sectionSepatator = _T_INSTALL("################################################################################");
/*
* Create and open log file
*/
int createLog( _TCHAR* logfile )
{
_TCHAR buffer[50];
_TCHAR datetime[30];
time_t ltime = time(NULL);
_tcsftime(datetime,sizeof(datetime),_T_INSTALL("%m/%d/%Y"),localtime( &ltime ));
file = _tfopen(logfile, _T_INSTALL("wt"));
if (file == NULL) return -1;
_tcscpy(buffer,logMessage);
_tcscat(buffer,datetime);
return writeLog(buffer) == 0 && writeLog(sectionSepatator) == 0 ? 0 : -1;
}
/*
* Write string with time and newline to log file
*/
int writeLog( _TCHAR* string )
{
_TCHAR datetime[30];
time_t ltime = time(NULL);
if ( file == NULL ) return -2;
_tcsftime(datetime,sizeof(datetime),_T_INSTALL("%H:%M:%S"),localtime( &ltime ));
if ( _fputts(datetime,file) < 0 )
return -1;
if ( _fputts(timeSepatator,file) < 0 )
return -1;
if ( _fputts(string,file) < 0 )
return -1;
if ( _fputts(lineSepatator,file) < 0 )
return -1;
return fflush(file);
}
/*
* Close log file
*/
void closeLog()
{
if ( file != NULL ) {
fclose(file);
file = NULL;
}
}
/* Helpers */
void logFormatS( _TCHAR* format, _TCHAR* string )
{
_TCHAR *buffer = malloc((_tcslen(format)+_tcslen(string))*sizeof(_TCHAR));
_stprintf(buffer, format, string );
writeLog(buffer);
free(buffer);
}
void logFormatI( _TCHAR* format, int i )
{
_TCHAR *buffer = malloc((_tcslen(format)+15)*sizeof(_TCHAR));
_stprintf(buffer, format, i );
writeLog(buffer);
free(buffer);
}
/* EOF */