Freeciv-3.1
Loading...
Searching...
No Matches
srv_log.c
Go to the documentation of this file.
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)
6 any later version.
7
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***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdarg.h>
19
20/* utility */
21#include "astring.h"
22#include "log.h"
23#include "shared.h"
24#include "support.h"
25#include "timing.h"
26
27/* common */
28#include "ai.h"
29#include "city.h"
30#include "game.h"
31#include "map.h"
32#include "unit.h"
33
34/* server */
35#include "notify.h"
36#include "srv_main.h"
37
38/* server/advisors */
39#include "advdata.h"
40
41#include "srv_log.h"
42
43static struct timer *aitimer[AIT_LAST][2];
44static int recursion[AIT_LAST];
45
46/* General AI logging functions */
47
48/**********************************************************************/
52void 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, ...)
55{
56 char buffer[500];
57 char buffer2[500];
58 va_list ap;
59 char aibuf[500] = "\0";
60
61 CALL_PLR_AI_FUNC(log_fragment_city, city_owner(pcity), aibuf, sizeof(aibuf), pcity);
62
63 fc_snprintf(buffer, sizeof(buffer), "%s %s(%d,%d) (s%d) {%s} ",
65 city_name_get(pcity),
66 TILE_XY(pcity->tile), city_size_get(pcity),
67 aibuf);
68
69 va_start(ap, msg);
70 fc_vsnprintf(buffer2, sizeof(buffer2), msg, ap);
71 va_end(ap);
72
73 cat_snprintf(buffer, sizeof(buffer), "%s", buffer2);
74 if (notify) {
75 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer);
76 }
77 do_log(file, function, line, FALSE, level, "%s", buffer);
78}
79
80/**********************************************************************/
86void 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, ...)
89{
90 char buffer[500];
91 char buffer2[500];
92 va_list ap;
93 int gx, gy;
94 char aibuf[500] = "\0";
95
96 CALL_PLR_AI_FUNC(log_fragment_unit, unit_owner(punit), aibuf, sizeof(aibuf), punit);
97
98 if (punit->goto_tile) {
100 } else {
101 gx = gy = -1;
102 }
103
104 fc_snprintf(buffer, sizeof(buffer),
105 "%s %s(%d) %s (%d,%d)->(%d,%d){%s} ",
108 punit->id,
111 gx, gy, aibuf);
112
113 va_start(ap, msg);
114 fc_vsnprintf(buffer2, sizeof(buffer2), msg, ap);
115 va_end(ap);
116
117 cat_snprintf(buffer, sizeof(buffer), "%s", buffer2);
118 if (notify) {
119 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer);
120 }
121 do_log(file, function, line, FALSE, level, "%s", buffer);
122}
123
124/**********************************************************************/
129{
130 static int turn = -1;
131
132 if (game.info.turn != turn) {
133 int i;
134
135 turn = game.info.turn;
136 for (i = 0; i < AIT_LAST; i++) {
137 timer_clear(aitimer[i][0]);
138 }
139 fc_assert(activity == TIMER_START);
140 }
141
142 if (activity == TIMER_START && recursion[timer] == 0) {
145 recursion[timer]++;
146 } else if (activity == TIMER_STOP && recursion[timer] == 1) {
149 recursion[timer]--;
150 }
151}
152
153/**********************************************************************/
157{
158 char buf[200];
159
160#ifdef LOG_TIMERS
161
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);
168
169 log_test(" --- AI timing results ---");
170
171#else /* LOG_TIMERS */
172
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);
178
179#endif /* LOG_TIMERS */
180
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);
212}
213
214/**********************************************************************/
218{
219 int i;
220
221 for (i = 0; i < AIT_LAST; i++) {
224 recursion[i] = 0;
225 }
226}
227
228/**********************************************************************/
232{
233 int i;
234
235 for (i = 0; i < AIT_LAST; i++) {
236 timer_destroy(aitimer[i][0]);
237 timer_destroy(aitimer[i][1]);
238 }
239}
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:374
const char * city_name_get(const struct city *pcity)
Definition city.c:1115
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
#define city_owner(_pcity_)
Definition city.h:543
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
const struct ft_color ftc_log
struct civ_game game
Definition game.c:57
void do_log(const char *file, const char *function, int line, bool print_from_where, enum log_level level, const char *message,...)
Definition log.c:514
#define log_test
Definition log.h:136
#define fc_assert(condition)
Definition log.h:176
log_level
Definition log.h:28
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:227
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:137
struct nation_type * nation_of_unit(const struct unit *punit)
Definition nation.c:462
struct nation_type * nation_of_city(const struct city *pcity)
Definition nation.c:453
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:238
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
void timing_log_init(void)
Definition srv_log.c:217
void real_city_log(const char *file, const char *function, int line, enum log_level level, bool notify, const struct city *pcity, const char *msg,...)
Definition srv_log.c:52
static struct timer * aitimer[AIT_LAST][2]
Definition srv_log.c:43
static int recursion[AIT_LAST]
Definition srv_log.c:44
void real_unit_log(const char *file, const char *function, int line, enum log_level level, bool notify, const struct unit *punit, const char *msg,...)
Definition srv_log.c:86
void timing_log_free(void)
Definition srv_log.c:231
void timing_results_real(void)
Definition srv_log.c:156
#define AILOG_OUT(text, which)
void timing_log_real(enum ai_timer timer, enum ai_timer_activity activity)
Definition srv_log.c:128
ai_timer
Definition srv_log.h:40
@ AIT_RECOVER
Definition srv_log.h:68
@ AIT_FSTK
Definition srv_log.h:54
@ AIT_GOVERNMENT
Definition srv_log.h:47
@ AIT_CARAVAN
Definition srv_log.h:56
@ AIT_RAMPAGE
Definition srv_log.h:71
@ AIT_CITY_MILITARY
Definition srv_log.h:63
@ AIT_BUILDINGS
Definition srv_log.h:51
@ AIT_TAXES
Definition srv_log.h:48
@ AIT_ATTACK
Definition srv_log.h:66
@ AIT_HUNTER
Definition srv_log.h:57
@ AIT_BODYGUARD
Definition srv_log.h:69
@ AIT_DEFENDERS
Definition srv_log.h:55
@ AIT_SETTLERS
Definition srv_log.h:44
@ AIT_UNITS
Definition srv_log.h:43
@ AIT_CITIES
Definition srv_log.h:49
@ AIT_AIRUNIT
Definition srv_log.h:60
@ AIT_EXPLORER
Definition srv_log.h:61
@ AIT_TECH
Definition srv_log.h:53
@ AIT_CITY_TERRAIN
Definition srv_log.h:64
@ AIT_FERRY
Definition srv_log.h:70
@ AIT_MILITARY
Definition srv_log.h:67
@ AIT_ALL
Definition srv_log.h:41
@ AIT_MOVEMAP
Definition srv_log.h:42
@ AIT_WORKERS
Definition srv_log.h:45
@ AIT_DIPLOMAT
Definition srv_log.h:59
@ AIT_LAST
Definition srv_log.h:72
@ AIT_AIRLIFT
Definition srv_log.h:58
@ AIT_CITIZEN_ARRANGE
Definition srv_log.h:50
@ AIT_CITY_SETTLERS
Definition srv_log.h:65
@ AIT_DANGER
Definition srv_log.h:52
ai_timer_activity
Definition srv_log.h:75
@ TIMER_STOP
Definition srv_log.h:76
@ TIMER_START
Definition srv_log.h:76
Definition city.h:309
struct tile * tile
Definition city.h:311
struct packet_game_info info
Definition game.h:89
Definition timing.c:81
Definition unit.h:138
enum unit_activity activity
Definition unit.h:157
int id
Definition unit.h:145
struct tile * goto_tile
Definition unit.h:155
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
int cat_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:995
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:896
#define FALSE
Definition support.h:47
#define tile_index(_pt_)
Definition tile.h:87
#define TILE_XY(ptile)
Definition tile.h:42
struct timer * timer_new(enum timer_timetype type, enum timer_use use)
Definition timing.c:157
void timer_clear(struct timer *t)
Definition timing.c:212
void timer_destroy(struct timer *t)
Definition timing.c:191
void timer_start(struct timer *t)
Definition timing.c:224
void timer_stop(struct timer *t)
Definition timing.c:268
@ TIMER_ACTIVE
Definition timing.h:45
@ TIMER_CPU
Definition timing.h:40
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:625
#define unit_tile(_pu)
Definition unit.h:395
#define unit_owner(_pu)
Definition unit.h:394
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1639