/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License.
 *
 *
 *                      ********************
 *
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "Debug.h"
#include "Log.h"
#include "Com/Commun.h"
#include "Com/Initialisation_Com.h"
#include "Macro.h"

#ifdef __LOG_ENABLE   
extern struct Struct_Categorie Liste_Categorie[];
#endif

void debug_printf (int Niveau_Debug, const char *file, const char *Fonction, int line, char *fmt, ...)
{
	if (Debug >= Niveau_Debug) {
		PROTECT_CODE(Semaphore_Debug);
		struct struct_Retour_Message Message;
#ifdef __LOG_ENABLE
		struct struct_Cle_Privee* pCle = NULL;
		struct Struct_Liste_Equipement* Equipement = NULL;
		gchar	*Texte_Log = NULL;
#endif
		char      buffer[255+1]; 

		va_list   args;
		va_start (args, fmt);
		vsnprintf (buffer, 255, fmt, args);
#ifdef __LOG_ENABLE
		
		if ((pCle = pthread_getspecific (Cle_privee_thread)) != NULL) {
			if (pCle->Categorie==CATEG_THREAD_SCAN)
				Equipement = pCle->Pointeur_Data;
		}
		else
			pCle = Donnee_Cle_Main;
#endif
		switch (Niveau_Debug) {
			case D_SEG_FAULT:
				printf("[D_SEG_FAULT]\n");
				printf("[D_SEG_FAULT] [%s](%s:%d): %s\n",Fonction, file, line, buffer);
#ifdef __LOG_ENABLE
				
				Texte_Log = g_strdup_printf("[D_SEG_FAULT] [%s](%s:%d): %s\n",Fonction, file, line, buffer);
				Log(pCle,Texte_Log);
				MYFREE(Texte_Log);
				display_backtrace(pCle,NULL,0);
				Message_Erreur_Critique(pCle,D_SEG_FAULT);
				Arret_Demande(NULL);
			    break;
#endif    
			case D_ERRO:
				printf ("[A] [ERROR] [%s](%s:%d): %s\n",Fonction, file, line, buffer);
#ifdef __LOG_ENABLE
				
				Texte_Log = g_strdup_printf("[ERROR] [%s](%s:%d): %s\n",Fonction, file, line, buffer);
				Log(pCle,Texte_Log);
				MYFREE(Texte_Log);
#endif
#ifdef __LOG_ENABLE				
				Message_Erreur_Critique(pCle,D_ERRO);
#else	
				if (Demarrage_Complet_Agent){
			    	bzero (&Message, sizeof (struct struct_Retour_Message));
			    	Message.ID_Reseau = Info_Demarrage.ID_Reseau;
			    	Message.Type_message = D_ERRO;
			    	strcpy (Message.File, file);
					strcpy (Message.Fonction,Fonction);
			    	Message.Line = line;
			    	if (strlen (buffer) >= 255)
					    buffer[255] = 0;
			    	strcpy (Message.Message, buffer);
		    		Communication_Envoie_Donnee (RETOUR_MESSAGE, (gpointer *) & Message, sizeof (Message));
				}
#endif			
		   		Arret_Demande(NULL);
			    break;
		    case D_WARN_USER:
#ifdef __LOG_ENABLE
				
				Texte_Log = g_strdup_printf("[WARN_USER] [%s](%s:%d): %s\n",Fonction, file, line, buffer);
				Log(pCle,Texte_Log);
				MYFREE(Texte_Log);
#endif
				if (Demarrage_Complet_Agent){
			    	bzero (&Message, sizeof (struct struct_Retour_Message));
			    	Message.ID_Reseau = Info_Demarrage.ID_Reseau;
			    	Message.Type_message = D_WARN_USER;
			    	strcpy (Message.File, file);
					strcpy (Message.Fonction,Fonction);
			    	Message.Line = line;
			    	if (strlen (buffer) >= 255)
				    	buffer[255] = 0;
			    	strcpy (Message.Message, buffer);
				    Communication_Envoie_Donnee (RETOUR_MESSAGE, (gpointer *) & Message, sizeof (Message));
				}	
			    break;
		    case D_WARN:
#ifdef __LOG_ENABLE
				
				Texte_Log = g_strdup_printf("[WARN] [%s](%s:%d): %s\n",Fonction, file, line, buffer);
				Log(pCle,Texte_Log);
				MYFREE(Texte_Log);
#endif
			    break;
		    case D_INFO_USER:
#ifdef __LOG_ENABLE
				
				Texte_Log = g_strdup_printf("[D_INFO_USER] [%s]: %s\n",Fonction, buffer);
				Log(pCle,Texte_Log);
				MYFREE(Texte_Log);
#endif		   
			   	if (Demarrage_Complet_Agent){
 		    		bzero (&Message, sizeof (struct struct_Retour_Message));
			    	Message.ID_Reseau = Info_Demarrage.ID_Reseau;
			    	Message.Type_message = D_INFO_USER;
			    	strcpy (Message.File, file);
					strcpy (Message.Fonction,Fonction);
			    	Message.Line = line;
			    	if (strlen (buffer) >= 255)
					    buffer[255] = 0;
			    	strcpy (Message.Message, buffer);
			    	Communication_Envoie_Donnee (RETOUR_MESSAGE, (gpointer *) & Message, sizeof (Message));
			 	}
				break;
		    case D_INFO:
#ifdef __LOG_ENABLE
				
				Texte_Log = g_strdup_printf("[INF] [%s]: %s\n",Fonction, buffer);
				Log(pCle,Texte_Log);
				MYFREE(Texte_Log);
#endif
			    break;
		}
		va_end (args);
		DEPROTECT_CODE(Semaphore_Debug);
	}
}

void Initialisation_Debug (void)
{
	Debug = D_INFO;
#ifdef __LOG_ENABLE
	Initialisation_Repertoire_Log();
	Initialisation_Log();
#endif	
}
