Freeciv-3.3: /fast/freeciv/nightly/main/server/srv_log.c Source File

Freeciv-3.3

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 "nation.h"
33#include "unit.h"
34
35/* server */
36#include "notify.h"
37#include "srv_main.h"
38
39/* server/advisors */
40#include "advdata.h"
41
42#include "srv_log.h"
43
44 static struct timer *aitimer[AIT_LAST][2];
45 static int recursion[AIT_LAST];
46
47/* General AI logging functions */
48
49/**********************************************************************/
53 void real_city_log(const char *file, const char *function, int line,
54 enum log_level level, bool notify,
55 const struct city *pcity, const char *msg, ...)
56{
57 char buffer[500];
58 char buffer2[500];
59 va_list ap;
60 char aibuf[500] = "\0";
61
62 CALL_PLR_AI_FUNC(log_fragment_city, city_owner(pcity), aibuf, sizeof(aibuf), pcity);
63
64 fc_snprintf(buffer, sizeof(buffer), "%s %s(%d,%d) (s%d) {%s} ",
65 nation_rule_name(nation_of_city(pcity)),
66 city_name_get(pcity),
67 TILE_XY(pcity->tile), city_size_get(pcity),
68 aibuf);
69
70 va_start(ap, msg);
71 fc_vsnprintf(buffer2, sizeof(buffer2), msg, ap);
72 va_end(ap);
73
74 cat_snprintf(buffer, sizeof(buffer), "%s", buffer2);
75 if (notify) {
76 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer);
77 }
78 do_log(file, function, line, FALSE, level, "%s", buffer);
79}
80
81/**********************************************************************/
87 void real_unit_log(const char *file, const char *function, int line,
88 enum log_level level, bool notify,
89 const struct unit *punit, const char *msg, ...)
90{
91 char buffer[500];
92 char buffer2[500];
93 va_list ap;
94 int gx, gy;
95 char aibuf[500] = "\0";
96
97 CALL_PLR_AI_FUNC(log_fragment_unit, unit_owner(punit), aibuf, sizeof(aibuf), punit);
98
99 if (punit->goto_tile) {
100 index_to_map_pos(&gx, &gy, tile_index(punit->goto_tile));
101 } else {
102 gx = gy = -1;
103 }
104
105 fc_snprintf(buffer, sizeof(buffer),
106 "%s %s(%d) %s (%d,%d)->(%d,%d){%s} ",
107 nation_rule_name(nation_of_unit(punit)),
108 unit_rule_name(punit),
109 punit->id,
110 get_activity_text(punit->activity),
111 TILE_XY(unit_tile(punit)),
112 gx, gy, aibuf);
113
114 va_start(ap, msg);
115 fc_vsnprintf(buffer2, sizeof(buffer2), msg, ap);
116 va_end(ap);
117
118 cat_snprintf(buffer, sizeof(buffer), "%s", buffer2);
119 if (notify) {
120 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer);
121 }
122 do_log(file, function, line, FALSE, level, "%s", buffer);
123}
124
125/**********************************************************************/
129 void timing_log_real(enum ai_timer timer, enum ai_timer_activity activity)
130{
131 static int turn = -1;
132
133 if (game.info.turn != turn) {
134 int i;
135
136 turn = game.info.turn;
137 for (i = 0; i < AIT_LAST; i++) {
138 timer_clear(aitimer[i][0]);
139 }
140 fc_assert(activity == TIMER_START);
141 }
142
143 if (activity == TIMER_START && recursion[timer] == 0) {
144 timer_start(aitimer[timer][0]);
145 timer_start(aitimer[timer][1]);
146 recursion[timer]++;
147 } else if (activity == TIMER_STOP && recursion[timer] == 1) {
148 timer_stop(aitimer[timer][0]);
149 timer_stop(aitimer[timer][1]);
150 recursion[timer]--;
151 }
152}
153
154/**********************************************************************/
157 void timing_results_real(void)
158{
159 char buf[200];
160
161#ifdef LOG_TIMERS
162
163#define AILOG_OUT(text, which) \
164 fc_snprintf(buf, sizeof(buf), " %s: %g sec turn, %g sec game", text, \
165 timer_read_seconds(aitimer[which][0]), \
166 timer_read_seconds(aitimer[which][1])); \
167 log_test("%s", buf); \
168 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buf);
169
170 log_test(" --- AI timing results ---");
171
172#else /* LOG_TIMERS */
173
174#define AILOG_OUT(text, which) \
175 fc_snprintf(buf, sizeof(buf), " %s: %g sec turn, %g sec game", text, \
176 timer_read_seconds(aitimer[which][0]), \
177 timer_read_seconds(aitimer[which][1])); \
178 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buf);
179
180#endif /* LOG_TIMERS */
181
182 notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log,
183 " --- AI timing results ---");
184 AILOG_OUT("Total AI time", AIT_ALL);
185 AILOG_OUT("Movemap", AIT_MOVEMAP);
186 AILOG_OUT("Units", AIT_UNITS);
187 AILOG_OUT(" - Military", AIT_MILITARY);
188 AILOG_OUT(" - Attack", AIT_ATTACK);
189 AILOG_OUT(" - Defense", AIT_DEFENDERS);
190 AILOG_OUT(" - Ferry", AIT_FERRY);
191 AILOG_OUT(" - Rampage", AIT_RAMPAGE);
192 AILOG_OUT(" - Bodyguard", AIT_BODYGUARD);
193 AILOG_OUT(" - Recover", AIT_RECOVER);
194 AILOG_OUT(" - Caravan", AIT_CARAVAN);
195 AILOG_OUT(" - Hunter", AIT_HUNTER);
196 AILOG_OUT(" - Airlift", AIT_AIRLIFT);
197 AILOG_OUT(" - Diplomat", AIT_DIPLOMAT);
198 AILOG_OUT(" - Air", AIT_AIRUNIT);
199 AILOG_OUT(" - Explore", AIT_EXPLORER);
200 AILOG_OUT("fstk", AIT_FSTK);
201 AILOG_OUT("Settlers", AIT_SETTLERS);
202 AILOG_OUT("Workers", AIT_WORKERS);
203 AILOG_OUT("Government", AIT_GOVERNMENT);
204 AILOG_OUT("Taxes", AIT_TAXES);
205 AILOG_OUT("Cities", AIT_CITIES);
206 AILOG_OUT(" - Buildings", AIT_BUILDINGS);
207 AILOG_OUT(" - Danger", AIT_DANGER);
208 AILOG_OUT(" - Worker want", AIT_CITY_TERRAIN);
209 AILOG_OUT(" - Military want", AIT_CITY_MILITARY);
210 AILOG_OUT(" - Settler want", AIT_CITY_SETTLERS);
211 AILOG_OUT("Citizen arrange", AIT_CITIZEN_ARRANGE);
212 AILOG_OUT("Tech", AIT_TECH);
213}
214
215/**********************************************************************/
218 void timing_log_init(void)
219{
220 int i;
221
222 for (i = 0; i < AIT_LAST; i++) {
223 char buf[60];
224
225 fc_snprintf(buf, sizeof(buf), "AI type %d turn", i);
226 aitimer[i][0] = timer_new(TIMER_CPU, TIMER_ACTIVE, buf);
227 fc_snprintf(buf, sizeof(buf), "AI type %d game", i);
228 aitimer[i][1] = timer_new(TIMER_CPU, TIMER_ACTIVE, buf);
229 recursion[i] = 0;
230 }
231}
232
233/**********************************************************************/
236 void timing_log_free(void)
237{
238 int i;
239
240 for (i = 0; i < AIT_LAST; i++) {
241 timer_destroy(aitimer[i][0]);
242 timer_destroy(aitimer[i][1]);
243 }
244}
advdata.h
ai.h
CALL_PLR_AI_FUNC
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:377
astring.h
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1128
city.h
city_size_get
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
city_owner
#define city_owner(_pcity_)
Definition city.h:563
incite_cost
char * incite_cost
Definition comments.c:74
punit
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:74
ftc_log
const struct ft_color ftc_log
Definition featured_text.c:76
game
struct civ_game game
Definition game.c:62
game.h
do_log
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
log.h
log_test
#define log_test
Definition log.h:136
fc_assert
#define fc_assert(condition)
Definition log.h:176
log_level
log_level
Definition log.h:28
map.h
index_to_map_pos
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:218
nation_rule_name
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
nation_of_unit
struct nation_type * nation_of_unit(const struct unit *punit)
Definition nation.c:463
nation_of_city
struct nation_type * nation_of_city(const struct city *pcity)
Definition nation.c:454
nation.h
notify_conn
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:239
notify.h
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
shared.h
timing_log_init
void timing_log_init(void)
Definition srv_log.c:218
real_city_log
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:53
aitimer
static struct timer * aitimer[AIT_LAST][2]
Definition srv_log.c:44
recursion
static int recursion[AIT_LAST]
Definition srv_log.c:45
real_unit_log
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:87
timing_log_free
void timing_log_free(void)
Definition srv_log.c:236
timing_results_real
void timing_results_real(void)
Definition srv_log.c:157
AILOG_OUT
#define AILOG_OUT(text, which)
timing_log_real
void timing_log_real(enum ai_timer timer, enum ai_timer_activity activity)
Definition srv_log.c:129
srv_log.h
ai_timer
ai_timer
Definition srv_log.h:40
AIT_RECOVER
@ AIT_RECOVER
Definition srv_log.h:68
AIT_FSTK
@ AIT_FSTK
Definition srv_log.h:54
AIT_GOVERNMENT
@ AIT_GOVERNMENT
Definition srv_log.h:47
AIT_CARAVAN
@ AIT_CARAVAN
Definition srv_log.h:56
AIT_RAMPAGE
@ AIT_RAMPAGE
Definition srv_log.h:71
AIT_CITY_MILITARY
@ AIT_CITY_MILITARY
Definition srv_log.h:63
AIT_BUILDINGS
@ AIT_BUILDINGS
Definition srv_log.h:51
AIT_TAXES
@ AIT_TAXES
Definition srv_log.h:48
AIT_ATTACK
@ AIT_ATTACK
Definition srv_log.h:66
AIT_HUNTER
@ AIT_HUNTER
Definition srv_log.h:57
AIT_BODYGUARD
@ AIT_BODYGUARD
Definition srv_log.h:69
AIT_DEFENDERS
@ AIT_DEFENDERS
Definition srv_log.h:55
AIT_SETTLERS
@ AIT_SETTLERS
Definition srv_log.h:44
AIT_UNITS
@ AIT_UNITS
Definition srv_log.h:43
AIT_CITIES
@ AIT_CITIES
Definition srv_log.h:49
AIT_AIRUNIT
@ AIT_AIRUNIT
Definition srv_log.h:60
AIT_EXPLORER
@ AIT_EXPLORER
Definition srv_log.h:61
AIT_TECH
@ AIT_TECH
Definition srv_log.h:53
AIT_CITY_TERRAIN
@ AIT_CITY_TERRAIN
Definition srv_log.h:64
AIT_FERRY
@ AIT_FERRY
Definition srv_log.h:70
AIT_MILITARY
@ AIT_MILITARY
Definition srv_log.h:67
AIT_ALL
@ AIT_ALL
Definition srv_log.h:41
AIT_MOVEMAP
@ AIT_MOVEMAP
Definition srv_log.h:42
AIT_WORKERS
@ AIT_WORKERS
Definition srv_log.h:45
AIT_DIPLOMAT
@ AIT_DIPLOMAT
Definition srv_log.h:59
AIT_LAST
@ AIT_LAST
Definition srv_log.h:72
AIT_AIRLIFT
@ AIT_AIRLIFT
Definition srv_log.h:58
AIT_CITIZEN_ARRANGE
@ AIT_CITIZEN_ARRANGE
Definition srv_log.h:50
AIT_CITY_SETTLERS
@ AIT_CITY_SETTLERS
Definition srv_log.h:65
AIT_DANGER
@ AIT_DANGER
Definition srv_log.h:52
ai_timer_activity
ai_timer_activity
Definition srv_log.h:75
TIMER_STOP
@ TIMER_STOP
Definition srv_log.h:76
TIMER_START
@ TIMER_START
Definition srv_log.h:76
srv_main.h
city
Definition city.h:320
city::tile
struct tile * tile
Definition city.h:322
civ_game::info
struct packet_game_info info
Definition game.h:89
packet_game_info::turn
int turn
Definition packets_gen.h:247
timer
Definition timing.c:81
unit
Definition unit.h:140
unit::activity
enum unit_activity activity
Definition unit.h:159
unit::id
int id
Definition unit.h:147
unit::goto_tile
struct tile * goto_tile
Definition unit.h:157
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
cat_snprintf
int cat_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:986
fc_vsnprintf
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:886
support.h
FALSE
#define FALSE
Definition support.h:47
tile_index
#define tile_index(_pt_)
Definition tile.h:89
TILE_XY
#define TILE_XY(ptile)
Definition tile.h:43
timer_clear
void timer_clear(struct timer *t)
Definition timing.c:252
timer_destroy
void timer_destroy(struct timer *t)
Definition timing.c:208
timer_start
void timer_start(struct timer *t)
Definition timing.c:264
timer_stop
void timer_stop(struct timer *t)
Definition timing.c:308
timer_new
struct timer * timer_new(enum timer_timetype type, enum timer_use use, const char *name)
Definition timing.c:160
timing.h
TIMER_ACTIVE
@ TIMER_ACTIVE
Definition timing.h:46
TIMER_CPU
@ TIMER_CPU
Definition timing.h:41
get_activity_text
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:634
unit.h
unit_tile
#define unit_tile(_pu)
Definition unit.h:397
unit_owner
#define unit_owner(_pu)
Definition unit.h:396
unit_rule_name
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587