1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
43 static struct timer
*aitimer
[AIT_LAST
][2];
44 static int recursion
[AIT_LAST
];
46 /* General AI logging functions */
48 /**************************************************************************
49 Log city messages, they will appear like this
50 2: Polish Romenna(5,35) [s1 d106 u11 g1] must have Archers ...
51 **************************************************************************/
52 void real_city_log(const char *file
, const char *function
, int line
,
53 enum log_level level
, bool notify
,
54 const struct city
*pcity
, const char *msg
, ...)
59 char aibuf
[500] = "\0";
61 CALL_PLR_AI_FUNC(log_fragment_city
, city_owner(pcity
), aibuf
, sizeof(aibuf
), pcity
);
63 fc_snprintf(buffer
, sizeof(buffer
), "%s %s(%d,%d) [s%d] {%s} ",
64 nation_rule_name(nation_of_city(pcity
)),
66 TILE_XY(pcity
->tile
), city_size_get(pcity
),
70 fc_vsnprintf(buffer2
, sizeof(buffer2
), msg
, ap
);
73 cat_snprintf(buffer
, sizeof(buffer
), "%s", buffer2
);
75 notify_conn(NULL
, NULL
, E_AI_DEBUG
, ftc_log
, "%s", buffer
);
77 do_log(file
, function
, line
, FALSE
, level
, "%s", buffer
);
80 /**************************************************************************
81 Log unit messages, they will appear like this
82 2: Polish Archers[139] (5,35)->(0,0){0,0} stays to defend city
83 where [] is unit id, ()->() are coordinates present and goto, and
84 {,} contains bodyguard and ferryboat ids.
85 **************************************************************************/
86 void real_unit_log(const char *file
, const char *function
, int line
,
87 enum log_level level
, bool notify
,
88 const struct unit
*punit
, const char *msg
, ...)
94 char aibuf
[500] = "\0";
96 CALL_PLR_AI_FUNC(log_fragment_unit
, unit_owner(punit
), aibuf
, sizeof(aibuf
), punit
);
98 if (punit
->goto_tile
) {
99 index_to_map_pos(&gx
, &gy
, tile_index(punit
->goto_tile
));
104 fc_snprintf(buffer
, sizeof(buffer
),
105 "%s %s[%d] %s (%d,%d)->(%d,%d){%s} ",
106 nation_rule_name(nation_of_unit(punit
)),
107 unit_rule_name(punit
),
109 get_activity_text(punit
->activity
),
110 TILE_XY(unit_tile(punit
)),
114 fc_vsnprintf(buffer2
, sizeof(buffer2
), msg
, ap
);
117 cat_snprintf(buffer
, sizeof(buffer
), "%s", buffer2
);
119 notify_conn(NULL
, NULL
, E_AI_DEBUG
, ftc_log
, "%s", buffer
);
121 do_log(file
, function
, line
, FALSE
, level
, "%s", buffer
);
124 /**************************************************************************
125 Measure the time between the calls. Used to see where in the AI too
126 much CPU is being used.
127 **************************************************************************/
128 void timing_log_real(enum ai_timer timer
, enum ai_timer_activity activity
)
130 static int turn
= -1;
132 if (game
.info
.turn
!= turn
) {
135 turn
= game
.info
.turn
;
136 for (i
= 0; i
< AIT_LAST
; i
++) {
137 timer_clear(aitimer
[i
][0]);
139 fc_assert(activity
== TIMER_START
);
142 if (activity
== TIMER_START
&& recursion
[timer
] == 0) {
143 timer_start(aitimer
[timer
][0]);
144 timer_start(aitimer
[timer
][1]);
146 } else if (activity
== TIMER_STOP
&& recursion
[timer
] == 1) {
147 timer_stop(aitimer
[timer
][0]);
148 timer_stop(aitimer
[timer
][1]);
153 /**************************************************************************
155 **************************************************************************/
156 void timing_results_real(void)
162 #define AILOG_OUT(text, which) \
163 fc_snprintf(buf, sizeof(buf), " %s: %g sec turn, %g sec game", text, \
164 timer_read_seconds(aitimer[which][0]), \
165 timer_read_seconds(aitimer[which][1])); \
166 log_test("%s", buf); \
167 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buf);
169 log_test(" --- AI timing results ---");
171 #else /* LOG_TIMERS */
173 #define AILOG_OUT(text, which) \
174 fc_snprintf(buf, sizeof(buf), " %s: %g sec turn, %g sec game", text, \
175 timer_read_seconds(aitimer[which][0]), \
176 timer_read_seconds(aitimer[which][1])); \
177 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buf);
179 #endif /* LOG_TIMERS */
181 notify_conn(NULL
, NULL
, E_AI_DEBUG
, ftc_log
,
182 " --- AI timing results ---");
183 AILOG_OUT("Total AI time", AIT_ALL
);
184 AILOG_OUT("Movemap", AIT_MOVEMAP
);
185 AILOG_OUT("Units", AIT_UNITS
);
186 AILOG_OUT(" - Military", AIT_MILITARY
);
187 AILOG_OUT(" - Attack", AIT_ATTACK
);
188 AILOG_OUT(" - Defense", AIT_DEFENDERS
);
189 AILOG_OUT(" - Ferry", AIT_FERRY
);
190 AILOG_OUT(" - Rampage", AIT_RAMPAGE
);
191 AILOG_OUT(" - Bodyguard", AIT_BODYGUARD
);
192 AILOG_OUT(" - Recover", AIT_RECOVER
);
193 AILOG_OUT(" - Caravan", AIT_CARAVAN
);
194 AILOG_OUT(" - Hunter", AIT_HUNTER
);
195 AILOG_OUT(" - Airlift", AIT_AIRLIFT
);
196 AILOG_OUT(" - Diplomat", AIT_DIPLOMAT
);
197 AILOG_OUT(" - Air", AIT_AIRUNIT
);
198 AILOG_OUT(" - Explore", AIT_EXPLORER
);
199 AILOG_OUT("fstk", AIT_FSTK
);
200 AILOG_OUT("Settlers", AIT_SETTLERS
);
201 AILOG_OUT("Workers", AIT_WORKERS
);
202 AILOG_OUT("Government", AIT_GOVERNMENT
);
203 AILOG_OUT("Taxes", AIT_TAXES
);
204 AILOG_OUT("Cities", AIT_CITIES
);
205 AILOG_OUT(" - Buildings", AIT_BUILDINGS
);
206 AILOG_OUT(" - Danger", AIT_DANGER
);
207 AILOG_OUT(" - Worker want", AIT_CITY_TERRAIN
);
208 AILOG_OUT(" - Military want", AIT_CITY_MILITARY
);
209 AILOG_OUT(" - Settler want", AIT_CITY_SETTLERS
);
210 AILOG_OUT("Citizen arrange", AIT_CITIZEN_ARRANGE
);
211 AILOG_OUT("Tech", AIT_TECH
);
214 /**************************************************************************
215 Initialize AI timing system
216 **************************************************************************/
217 void timing_log_init(void)
221 for (i
= 0; i
< AIT_LAST
; i
++) {
222 aitimer
[i
][0] = timer_new(TIMER_CPU
, TIMER_ACTIVE
);
223 aitimer
[i
][1] = timer_new(TIMER_CPU
, TIMER_ACTIVE
);
228 /**************************************************************************
229 Free AI timing system resources
230 **************************************************************************/
231 void timing_log_free(void)
235 for (i
= 0; i
< AIT_LAST
; i
++) {
236 timer_destroy(aitimer
[i
][0]);
237 timer_destroy(aitimer
[i
][1]);