~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/ai/api/ai_log.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: ai_log.cpp 15060 2009-01-13 15:44:36Z smatz $ */
 
2
 
 
3
/** @file ai_log.cpp Implementation of AILog. */
 
4
 
 
5
#include "ai_log.hpp"
 
6
#include "../../core/alloc_func.hpp"
 
7
#include "../../company_func.h"
 
8
#include "../../debug.h"
 
9
#include "../../window_func.h"
 
10
 
 
11
/* static */ void AILog::Info(const char *message)
 
12
{
 
13
        AILog::Log(LOG_INFO, message);
 
14
}
 
15
 
 
16
/* static */ void AILog::Warning(const char *message)
 
17
{
 
18
        AILog::Log(LOG_WARNING, message);
 
19
}
 
20
 
 
21
/* static */ void AILog::Error(const char *message)
 
22
{
 
23
        AILog::Log(LOG_ERROR, message);
 
24
}
 
25
 
 
26
/* static */ void AILog::Log(AILog::AILogType level, const char *message)
 
27
{
 
28
        if (AIObject::GetLogPointer() == NULL) {
 
29
                AIObject::GetLogPointer() = new LogData();
 
30
                LogData *log = (LogData *)AIObject::GetLogPointer();
 
31
 
 
32
                log->lines = CallocT<char *>(80);
 
33
                log->type = CallocT<AILog::AILogType>(80);
 
34
                log->count = 80;
 
35
                log->pos = log->count;
 
36
                log->used = 0;
 
37
        }
 
38
        LogData *log = (LogData *)AIObject::GetLogPointer();
 
39
 
 
40
        /* Go to the next log-line */
 
41
        log->pos = (log->pos + 1) % log->count;
 
42
 
 
43
        if (log->used != log->count) log->used++;
 
44
 
 
45
        /* Free last message, and write new message */
 
46
        free(log->lines[log->pos]);
 
47
        log->lines[log->pos] = strdup(message);
 
48
        log->type[log->pos] = level;
 
49
 
 
50
        /* Cut string after first \n */
 
51
        char *p;
 
52
        while ((p = strchr(log->lines[log->pos], '\n')) != NULL) {
 
53
                *p = '\0';
 
54
                break;
 
55
        }
 
56
 
 
57
        char logc;
 
58
 
 
59
        switch (level) {
 
60
                case LOG_SQ_ERROR: logc = 'S'; break;
 
61
                case LOG_ERROR:    logc = 'E'; break;
 
62
                case LOG_SQ_INFO:  logc = 'P'; break;
 
63
                case LOG_WARNING:  logc = 'W'; break;
 
64
                case LOG_INFO:     logc = 'I'; break;
 
65
                default:           logc = '?'; break;
 
66
        }
 
67
 
 
68
        /* Also still print to debug window */
 
69
        DEBUG(ai, level, "[%d] [%c] %s", (uint)_current_company, logc, log->lines[log->pos]);
 
70
        InvalidateWindowData(WC_AI_DEBUG, 0, _current_company);
 
71
}
 
72
 
 
73
/* static */ void AILog::FreeLogPointer()
 
74
{
 
75
        LogData *log = (LogData *)AIObject::GetLogPointer();
 
76
 
 
77
        for (int i = 0; i < log->count; i++) {
 
78
                free(log->lines[i]);
 
79
        }
 
80
 
 
81
        free(log->lines);
 
82
        free(log->type);
 
83
        delete log;
 
84
}