#include "dfeed.h"
void imsg_log(char *s);
static char *site="(main)";
static int id;
static Feed *feed;
void imsg_feed(Feed *f, int xid)
{
	feed = f;
	if (f==NULL) {
		site = "(main)";
		id = 0;
		return;
	}
	site = f->site;
	id = xid;
}

int emsg(const char * arg_list, ...)
{
        va_list arg_ptr;
        char *format;
	char output[2000];
        va_start(arg_ptr, arg_list);
        format = (char *) arg_list;
        vsnprintf(output,1000, format, arg_ptr);
	/*printf("%s",output);*/
	imsg_log(output);
	return 0;
}
int imsg(const char * arg_list, ...)
{
        va_list arg_ptr;
        char *format;
	char output[2000];
        va_start(arg_ptr, arg_list);
        format = (char *) arg_list;
        vsnprintf(output,1000, format, arg_ptr);
	/*printf("%s",output);*/
	imsg_log(output);
	return 0;
}
int dmsg(const char * arg_list, ...)
{
        va_list arg_ptr;
        char *format;
	char output[2000];
        va_start(arg_ptr, arg_list);
        format = (char *) arg_list;
        vsnprintf(output,1000, format, arg_ptr);
/*	if (feed!=NULL) printf("%p %p\n",feed,feed->flog);*/
	if (feed!=NULL) if (feed->flog!=NULL) imsg_flog(feed->flog,output);
	if (ini_true(I_DFEED_DEBUG)) imsg_log(output);
	return 0;
}
static FILE *flog;
static bytes;
void imsg_log(char *s)
{
	time_t t;
        struct tm *tt; 
	char *ccc;
	t = time(NULL);
        tt = localtime(&t);
	ccc = ctime(&t);
	if (ccc==NULL) ccc = "badtimebadtimebadtime";
  
	if (flog==NULL) return;
	bytes += fprintf(flog,"%.19s %s:%d %s",ccc,site,id,s);
#ifdef WIN32
	if (ini_true(I_DFEED_DEBUG)) {
		fclose(flog); flog = NULL;
		imsg_open();
	}
#else
	if (ini_true(I_DFEED_DEBUG)) {
		fflush(flog);
	}
#endif
}
void dmsg_data(char *bf, int len)
{
	if (feed==NULL) return;
	if (!feed->islog) return;
	if (feed->flog==NULL) return;
	fprintf(feed->flog,"Sent %d bytes\n",len);
	if (len<400) {
		fwrite(bf,1,len,feed->flog);
		return;
	} else if (ini_true(I_DFEED_DATA)) {
		fwrite(bf,1,len,feed->flog);
		return;
	}

	fwrite(bf,1,100,feed->flog);
	fprintf(feed->flog,"\n..(data skipped)...\n");
	fwrite(bf+len-50,1,50,feed->flog);  
}
void imsg_flog(FILE *flog, char *s)
{
	time_t t;
        struct tm *tt; 
	char *ccc;
	t = time(NULL);
        tt = localtime(&t);
	ccc = ctime(&t);
	if (ccc==NULL) ccc = "badtimebadtimebadtime";
  
	if (flog==NULL) return;
	fprintf(flog,"%.19s %s:%d %s",ccc,site,id,s);
}
char *lib_showtime(void)
{
	static char bf[BFSZ];
	time_t t;
        struct tm *tt; 
	char *ccc;

	t = time(NULL);
        tt = localtime(&t);

	ccc = ctime(&t);
	if (ccc==NULL) ccc = "badtimebadtimebadtime";
  
	sprintf(bf,"%.19s",ccc);
	return bf;
}
void imsg_open(void)
{
	flog = fopen(file_log("dfeed.log"),"a");
	if (flog==NULL) {
		printf("Unable to open (%s) (%s) \n",file_work("dfeed.log"),strerror(errno));
	} else {
		bytes = file_size(flog);
	}
}
void imsg_flush(void)
{
	if (flog!=NULL) fflush(flog);
	if (bytes>200000) {
		fclose(flog);
		flog = NULL;
		bytes = 0;
		remove(file_log("dfeed.log"));
		imsg_open();
	}
}

