lurf/area/
lurf/build/testing/
lurf/log/
lurf/player/
lurf/player/backup/
#include <glib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <ctype.h>
#include <cstdio>
#include <string.h>
#include <stdlib.h>
#include <merc.h>
#include <recycle.h>
#include <time.h>

extern char			strArea[MAX_INPUT_LENGTH];
extern FILE *			fpArea;

/*
 * Append a string to a file.
 */
void append_file( CHAR_DATA *ch, char *file, char *str )
{
	FILE *fp;
	
	if ( IS_NPC(ch) || str[0] == '\0' )
		return;
	
	fclose( fpReserve );
	if ( ( fp = fopen( file, "a" ) ) == NULL )
	{
		bug( file, 0 );
		send_to_char( "Could not open the file!\n\r", ch );
	}
	else
	{
		fprintf( fp, "[%5d] %s: %s\n",
			ch->in_room ? ch->in_room->vnum : 0, ch->name->str, str );
		fclose( fp );
	}
	
	fpReserve = fopen( NULL_FILE, "r" );
	return;
}

void logchan(char *string, CHAR_DATA *ch, OBJ_DATA *obj,
	    long flag, long flag_skip, int min_level) 
{
    DESCRIPTOR_DATA *d;
	GSList	*desc_list;
	
    for ( desc_list = descriptor_list; desc_list != NULL; desc_list = g_slist_next(desc_list) )
	{
		d = (DESCRIPTOR_DATA*)desc_list->data;
        if (d->connected == CON_PLAYING
	&&  d->character != NULL
	&&  !IS_NPC(d->character)
	&&  IS_IMMORTAL(d->character) 
	&&  IS_SET(d->character->pcdata->wiznet,WIZ_ON) 
	&&  (!flag || IS_SET(d->character->pcdata->wiznet,flag))
	&&  (!flag_skip || !IS_SET(d->character->pcdata->wiznet,flag_skip))
	&&  get_trust(d->character) >= min_level
	&&  d->character != ch)
        {
	    if (IS_SET(d->character->pcdata->wiznet,WIZ_PREFIX))
	  	send_to_char("--> ",d->character);
            act(string,d->character,obj,ch,TO_CHAR);
        }
    }
 
    return;
}
/*
 * Writes a string to the log.
 */
void log_string( const char *str )
{
	char *strtime;
	char logout [MAX_STRING_LENGTH];
	
	strtime                    = ctime( &current_time );
	strtime[strlen(strtime)-1] = '\0';
	fprintf( stderr, "%s :: %s\n", strtime, str );
	strcpy ( logout, str );
	//logchan( logout );
	return;
}

/*
 * Break point for errors that are in glib.. 
 * so I can track them down.
 */
void glib_crit_handler(const gchar *log_domain,GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{    
	DESCRIPTOR_DATA *d;
	GSList	*desc_list;
	
    for ( desc_list = descriptor_list; desc_list != NULL; desc_list = g_slist_next(desc_list) )
	{
		d = (DESCRIPTOR_DATA*)desc_list->data;
        if (d->connected == CON_PLAYING
	&&  d->character != NULL
	&&  !IS_NPC(d->character)
	&&  IS_IMMORTAL(d->character) 
	&&  IS_SET(d->character->pcdata->wiznet,WIZ_ON) 
	&&  IS_SET(d->character->pcdata->wiznet,WIZ_GLIB)
	&&  get_trust(d->character) >= LEVEL_IMPLEMENTOR)
	{
	    if (IS_SET(d->character->pcdata->wiznet,WIZ_PREFIX))
	  	send_to_char("--> ",d->character);
            act(message,d->character,NULL,NULL,TO_CHAR);
        }
    }
	return;

	return;
}

void my_log_handler(const gchar *log_domain,GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{
	
    DESCRIPTOR_DATA *d;
	GSList	*desc_list;
	
    for ( desc_list = descriptor_list; desc_list != NULL; desc_list = g_slist_next(desc_list) )
	{
		d = (DESCRIPTOR_DATA*)desc_list->data;
        if (d->connected == CON_PLAYING
	&&  d->character != NULL
	&&  !IS_NPC(d->character)
	&&  IS_IMMORTAL(d->character) 
	&&  IS_SET(d->character->pcdata->wiznet,WIZ_ON) 
	&&  IS_SET(d->character->pcdata->wiznet,WIZ_GLIB)
	&&  get_trust(d->character) >= LEVEL_IMPLEMENTOR)
	{
	    if (IS_SET(d->character->pcdata->wiznet,WIZ_PREFIX))
	  	send_to_char("--> ",d->character);
            act(message,d->character,NULL,NULL,TO_CHAR);
        }
    }
	return;
}

/*
 * Reports a bug.
 */
void bug( const char *str, int param )
{
	char buf[MAX_STRING_LENGTH];
	FILE *fp;
    	char *strtime;
	
	str_cpy( buf, "[*****] BUG: " );
	sprintf( buf + strlen(buf), str, param );

    	fclose( fpReserve );
	if ( ( fp = fopen( BUG_FILE, "a" ) ) != NULL )
	{
		strtime                    = ctime( &current_time );
		strtime[strlen(strtime)-1] = '\0';
		fprintf( fp, "%s : %s Last_Proc_Logged:%d\n", strtime, buf,mudsetting->last_proc_logged ) ;
		fclose( fp );
	}
	fpReserve = fopen( NULL_FILE, "r" );
   	logchan(buf, NULL, NULL,WIZ_DEBUG,0,LEVEL_IMPLEMENTOR);	
	return;
}
/* New log files for every day! */

void log_string2( const char *str )
{
	char *strtime;
	//char logfile[20];
	char buf[10];
	char temp[20];
	time_t tm;
	struct tm now;
	FILE *log_file;
	char logout [MAX_STRING_LENGTH];

	buf[0] = '\0';
	temp[0] = '\0';

	//log_file = NULL;

	time(&tm);
	now = *localtime(&tm);
	sprintf(buf,"%02d_%02d_%02d",now.tm_mon+1,now.tm_mday,now.tm_year);

	sprintf( temp, "../log/" );
	strcat( temp, buf );
	strcat( temp, ".log" );

	if ((log_file = fopen( temp, "a" )) == NULL){
		return;
	}

	strtime = ctime( &current_time );
	strtime[strlen(strtime)-1] = '\0';
	fprintf( log_file, "%s :: %s\n", strtime, str );
	fclose( log_file );

	strcpy ( logout, str );
	//logchan( logout );
	return;
}