ai_log.cpp

Go to the documentation of this file.
00001 /* $Id: ai_log.cpp 16121 2009-04-22 09:00:19Z rubidium $ */
00002 
00005 #include "ai_log.hpp"
00006 #include "../../core/alloc_func.hpp"
00007 #include "../../company_func.h"
00008 #include "../../debug.h"
00009 #include "../../window_func.h"
00010 
00011 /* static */ void AILog::Info(const char *message)
00012 {
00013   AILog::Log(LOG_INFO, message);
00014 }
00015 
00016 /* static */ void AILog::Warning(const char *message)
00017 {
00018   AILog::Log(LOG_WARNING, message);
00019 }
00020 
00021 /* static */ void AILog::Error(const char *message)
00022 {
00023   AILog::Log(LOG_ERROR, message);
00024 }
00025 
00026 /* static */ void AILog::Log(AILog::AILogType level, const char *message)
00027 {
00028   if (AIObject::GetLogPointer() == NULL) {
00029     AIObject::GetLogPointer() = new LogData();
00030     LogData *log = (LogData *)AIObject::GetLogPointer();
00031 
00032     log->lines = CallocT<char *>(80);
00033     log->type = CallocT<AILog::AILogType>(80);
00034     log->count = 80;
00035     log->pos = log->count - 1;
00036     log->used = 0;
00037   }
00038   LogData *log = (LogData *)AIObject::GetLogPointer();
00039 
00040   /* Go to the next log-line */
00041   log->pos = (log->pos + 1) % log->count;
00042 
00043   if (log->used != log->count) log->used++;
00044 
00045   /* Free last message, and write new message */
00046   free(log->lines[log->pos]);
00047   log->lines[log->pos] = strdup(message);
00048   log->type[log->pos] = level;
00049 
00050   /* Cut string after first \n */
00051   char *p;
00052   while ((p = strchr(log->lines[log->pos], '\n')) != NULL) {
00053     *p = '\0';
00054     break;
00055   }
00056 
00057   char logc;
00058 
00059   switch (level) {
00060     case LOG_SQ_ERROR: logc = 'S'; break;
00061     case LOG_ERROR:    logc = 'E'; break;
00062     case LOG_SQ_INFO:  logc = 'P'; break;
00063     case LOG_WARNING:  logc = 'W'; break;
00064     case LOG_INFO:     logc = 'I'; break;
00065     default:           logc = '?'; break;
00066   }
00067 
00068   /* Also still print to debug window */
00069   DEBUG(ai, level, "[%d] [%c] %s", (uint)_current_company, logc, log->lines[log->pos]);
00070   InvalidateWindowData(WC_AI_DEBUG, 0, _current_company);
00071 }
00072 
00073 /* static */ void AILog::FreeLogPointer()
00074 {
00075   LogData *log = (LogData *)AIObject::GetLogPointer();
00076 
00077   for (int i = 0; i < log->count; i++) {
00078     free(log->lines[i]);
00079   }
00080 
00081   free(log->lines);
00082   free(log->type);
00083   delete log;
00084 }

Generated on Mon May 11 15:48:01 2009 for OpenTTD by  doxygen 1.5.6