#ifndef DEF_TX_ERROR
 #define DEF_TX_ERROR

//------------------------------------------//
//		TKA4 Error Processing module		//
//				02. 11. 2007				//
//------------------------------------------//


//--- Pragma 
#pragma warning(disable:4996) 
//=== Pragma 

//--- Includes
#include <stdio.h>
#include <string.h>
//=== Includes 


#define INT_LENGTH 10


//--- txError ---
class txError {
	public:
		long int time;
		char *head;
		char *text;

	public:
		txError (long int t, char *str1, char *str2) { 
			time = t;
			head = strdup(str1); 
			text = strdup(str2); 
		}

		txError(char *s) {
			time = 0;
			head = strdup(s); 
			text = strdup(" "); 
		}

		~txError () 
		{ /*delete []head; delete []text;*/ }

};


//--- txErrorFile ---
class txErrorFile {
	FILE *f;
	char name[255];
	char AllowWrite;

public:

	//--- Constructor
	txErrorFile(char *str) {
		strcpy(name, str);
			
		f = fopen(name, "w");
			
		if (f==NULL) {
			AllowWrite = 0;
			return;
		}
		else AllowWrite = 1;

		char temp[255] = {"Step         |     Message head     |     Text\n"};
		fwrite(temp, sizeof(char), strlen(temp), f);

		fclose(f);
	}


	//--- Add Error To File
	inline virtual void AddErrorToFile(txError err)	{
		if (AllowWrite==0) return;
		
		f = fopen(name, "a+");
		
		if (f==NULL) { 
			AllowWrite = 0;
			return;
		}
		
		// Time
		char temp[255] = {' '};
		//itoa(err.time, temp, INT_LENGTH);
		sprintf(temp, "%+.10i ", err.time);
		fwrite(temp, sizeof(char), strlen(temp), f);

			fwrite(" - ", 1,3,f);

		// Head 
		fwrite(err.head, sizeof(char), strlen(err.head), f);
		
			fwrite(" :: ", 1,4,f);

		// Text
		fwrite(err.text, sizeof(char), strlen(err.text), f);
		
			fwrite("\n", 1,1,f);

		fclose (f);
	}

	//--- +
	inline virtual void operator + (const txError& err) {
		AddErrorToFile(err);
		return;
	}

};


#endif