# ai_log.cpp

Go to the documentation of this file.

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