# ai_log.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_log.cpp 17248 2009-08-21 20:21:05Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD 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, version 2.
    00006  * OpenTTD 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.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "ai_log.hpp"
    00013 #include "../../core/alloc_func.hpp"
    00014 #include "../../company_func.h"
    00015 #include "../../debug.h"
    00016 #include "../../window_func.h"
    00017
    00018 /* static */ void AILog::Info(const char *message)
    00019 {
    00020   AILog::Log(LOG_INFO, message);
    00021 }
    00022
    00023 /* static */ void AILog::Warning(const char *message)
    00024 {
    00025   AILog::Log(LOG_WARNING, message);
    00026 }
    00027
    00028 /* static */ void AILog::Error(const char *message)
    00029 {
    00030   AILog::Log(LOG_ERROR, message);
    00031 }
    00032
    00033 /* static */ void AILog::Log(AILog::AILogType level, const char *message)
    00034 {
    00035   if (AIObject::GetLogPointer() == NULL) {
    00036     AIObject::GetLogPointer() = new LogData();
    00037     LogData *log = (LogData *)AIObject::GetLogPointer();
    00038
    00039     log->lines = CallocT<char *>(80);
    00040     log->type = CallocT<AILog::AILogType>(80);
    00041     log->count = 80;
    00042     log->pos = log->count - 1;
    00043     log->used = 0;
    00044   }
    00045   LogData *log = (LogData *)AIObject::GetLogPointer();
    00046
    00047   /* Go to the next log-line */
    00048   log->pos = (log->pos + 1) % log->count;
    00049
    00050   if (log->used != log->count) log->used++;
    00051
    00052   /* Free last message, and write new message */
    00053   free(log->lines[log->pos]);
    00054   log->lines[log->pos] = strdup(message);
    00055   log->type[log->pos] = level;
    00056
    00057   /* Cut string after first \n */
    00058   char *p;
    00059   while ((p = strchr(log->lines[log->pos], '\n')) != NULL) {
    00060     *p = '\0';
    00061     break;
    00062   }
    00063
    00064   char logc;
    00065
    00066   switch (level) {
    00067     case LOG_SQ_ERROR: logc = 'S'; break;
    00068     case LOG_ERROR:    logc = 'E'; break;
    00069     case LOG_SQ_INFO:  logc = 'P'; break;
    00070     case LOG_WARNING:  logc = 'W'; break;
    00071     case LOG_INFO:     logc = 'I'; break;
    00072     default:           logc = '?'; break;
    00073   }
    00074
    00075   /* Also still print to debug window */
    00076   DEBUG(ai, level, "[%d] [%c] %s", (uint)_current_company, logc, log->lines[log->pos]);
    00077   InvalidateWindowData(WC_AI_DEBUG, 0, _current_company);
    00078 }
    00079
    00080 /* static */ void AILog::FreeLogPointer()
    00081 {
    00082   LogData *log = (LogData *)AIObject::GetLogPointer();
    00083
    00084   for (int i = 0; i < log->count; i++) {
    00085     free(log->lines[i]);
    00086   }
    00087
    00088   free(log->lines);
    00089   free(log->type);
    00090   delete log;
    00091 }