src/ai/api/ai_log.cpp
branchnoai
changeset 9851 a5f5a7cf2b61
child 10674 542470cee8a2
equal deleted inserted replaced
9850:d3228c6a1376 9851:a5f5a7cf2b61
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file ai_log.cpp Implementation of AILog. */
       
     4 
       
     5 #include "ai_log.hpp"
       
     6 #include "../../core/alloc_func.hpp"
       
     7 #include "../../player_func.h"
       
     8 #include "../../debug.h"
       
     9 
       
    10 /* static */ void AILog::Info(const char *message)
       
    11 {
       
    12 	AILog::Log(LOG_INFO, message);
       
    13 }
       
    14 
       
    15 /* static */ void AILog::Warning(const char *message)
       
    16 {
       
    17 	AILog::Log(LOG_WARNING, message);
       
    18 }
       
    19 
       
    20 /* static */ void AILog::Error(const char *message)
       
    21 {
       
    22 	AILog::Log(LOG_ERROR, message);
       
    23 }
       
    24 
       
    25 /* static */ void AILog::Log(AILog::AILogType level, const char *message)
       
    26 {
       
    27 	if (AIObject::GetLogPointer() == NULL) {
       
    28 		AIObject::GetLogPointer() = new LogData();
       
    29 		LogData *log = (LogData *)AIObject::GetLogPointer();
       
    30 
       
    31 		log->lines = CallocT<char *>(80);
       
    32 		log->type = CallocT<AILog::AILogType>(80);
       
    33 		log->count = 80;
       
    34 		log->pos = log->count;
       
    35 	}
       
    36 	LogData *log = (LogData *)AIObject::GetLogPointer();
       
    37 
       
    38 	/* Go to the next log-line */
       
    39 	log->pos = (log->pos + 1) % log->count;
       
    40 
       
    41 	/* Free last message, and write new message */
       
    42 	free(log->lines[log->pos]);
       
    43 	log->lines[log->pos] = strdup(message);
       
    44 	log->type[log->pos] = level;
       
    45 
       
    46 	/* Cut string after first \n */
       
    47 	char *p;
       
    48 	while ((p = strchr(log->lines[log->pos], '\n')) != NULL) {
       
    49 		*p = '\0';
       
    50 		break;
       
    51 	}
       
    52 
       
    53 	/* Also still print to debug window */
       
    54 	DEBUG(ai, level, "[%d] %s", (uint)_current_player, log->lines[log->pos]);
       
    55 }
       
    56 
       
    57 /* static */ void AILog::FreeLogPointer()
       
    58 {
       
    59 	LogData *log = (LogData *)AIObject::GetLogPointer();
       
    60 
       
    61 	for (int i = 0; i < log->count; i++) {
       
    62 		free(log->lines[i]);
       
    63 	}
       
    64 
       
    65 	free(log->lines);
       
    66 	free(log->type);
       
    67 	delete log;
       
    68 }