View Single Post
Re: Show off your finest piece of code!
Old
  (#9)
Cpl. Shrike
ShrikeBot Coder/Moderator
 
Cpl. Shrike's Avatar
 
Status: Offline
Posts: 550
Join Date: Mar 2004
Location: The Netherlands
Default Re: Show off your finest piece of code! - 30-12-2004

Well im proud of this cos i figured it out my self.
Not that it is some kind of rocket science.
But it just works so damn fast that lotsa time i forget i got debug logging on. Winding up enourmous megabitous log files with out lag.

Althoug the Win32 is not yet system logging. (but it is almost as fast as the linux method.)
It just compose the messages like linux sysloging is doing it. And it doesn't open close file after each log action.

Code:
   ///////////////////////////////////////////////////
   //
   //	sb_syslog.cpp
   //
   // linux sysloging and win32 file loggin 
   //
   //
   // TODO make it also win32 eventlog output.
   // 
   ///////////////////////////////////////////////////
   
   #include <extdll.h>
   #include "bot.h"
   #include "sb_syslog.h"
   
   #ifndef __linux__
   
   #include <time.h>
   static FILE *sb_log_file;
   char sb_log_msg[256];
   char * s_severity[] = {
   	"none",	// not used
   	"none",	// not used
   	"none",	// not used
   	"error",	// not used
   	"warn",	// not used
      "notice",
      "info",
      "debug"};
   
   #endif
   
   bool g_LogPrint_enabled = FALSE;
   
   // opens a Linux syslog entry or opens a windows file using date as file name
   void LogOpen()
   {
   	if (g_LogPrint_enabled)
   	{
   #ifndef __linux__
   		char dateStr [9];
   	  _strdate(dateStr);
   		int max = strlen(dateStr);
   		for (int x = 0; x <= max; x++)
   		{
   			if (dateStr[x] == '/')
   				dateStr[x] = '-';
   		}
   		strcpy(sb_log_msg, "log_");
   		strcat(sb_log_msg, dateStr);
   		strcat(sb_log_msg, ".txt");
   		sb_log_file=fopen(sb_log_msg, "a"); 
   #else	
   		openlog("shrikebot", LOG_DEBUG | LOG_INFO | LOG_NOTICE, LOG_USER);
   #endif
   	}
   }
   
   // write the log messages
   void LogPrint(int severity, char *message)
   {
   	if (g_LogPrint_enabled)
   	{
   #ifndef __linux__
   		char timeStr [9];
   		_strtime( timeStr );
   		if (sb_log_file != NULL)
   		{
   			if (severity != LOG_DEBUG)
   			{
   				strcpy(sb_log_msg, timeStr);
   				strcat(sb_log_msg, " - ");
 				strcat(sb_log_msg, s_severity[severity]);
   				strcat(sb_log_msg, ": ");
   				strcat(sb_log_msg, message);
   			}
   			else
   				strcpy(sb_log_msg, message);
   
   			strcat(sb_log_msg, "\n");
   			fprintf(sb_log_file, sb_log_msg);
   		} 
   #else
   		syslog(severity, message);
   #endif	
   	}
   }
   
   // close logging.  although this does not happen when it crashes.
 // but no dataloss because of not closed windows files so far.
   void LogClose()
   {
   	if (g_LogPrint_enabled)
   	{
   #ifndef __linux__
   		fclose(sb_log_file);
   #else
   		closelog();
   #endif
   	}
   }
Form the rest of the code i use LogPrint(LOG_LEVEL, msg);
In stead of the HPB_Bot log routines. Where LOG_LEVEL could be one of the levels defined by Linux syslog.h. But i derived it to sb_syslog.h to make it shorter LogPrint(); fuctions. and to make it work with Win32.

Last edited by Cpl. Shrike; 30-12-2004 at 16:24..
  
Reply With Quote