Freeciv-3.1: /fast/freeciv/nightly/S3_1/server/console.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
console.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 "fc_prehdrs.h"
19
20#include <stdarg.h>
21#include <stdio.h>
22#include <string.h>
23
24#ifdef FREECIV_HAVE_LIBREADLINE
25#include <readline/readline.h>
26#endif
27
28/* utility */
29#include "deprecations.h"
30#include "fcbacktrace.h"
31#include "fciconv.h"
32#include "fcintl.h"
33#include "log.h"
34#include "support.h"
35
36/* common */
37#include "game.h"
38
39/* server */
40#include "notify.h"
41#include "srv_main.h"
42
43#include "console.h"
44
45 static bool console_show_prompt = FALSE;
46 static bool console_prompt_is_showing = FALSE;
47 static bool console_rfcstyle = FALSE;
48#ifdef FREECIV_HAVE_LIBREADLINE
49static bool readline_received_enter = TRUE;
50#else
51 static int con_dump(enum rfc_status rfc_status, const char *message, ...);
52#endif
53
54/********************************************************************/
58 static void con_handle_log(enum log_level level, const char *message,
59 bool file_too)
60{
61 if (LOG_ERROR == level) {
62 notify_conn(NULL, NULL, E_LOG_ERROR, ftc_warning, "%s", message);
63 } else if (LOG_FATAL >= level) {
64 /* Make sure that message is not left to buffers when server dies */
65 conn_list_iterate(game.est_connections, pconn) {
66 pconn->send_buffer->do_buffer_sends = 0;
67#ifdef USE_COMPRESSION
68 pconn->compression.frozen_level = 0;
69#endif
70 } conn_list_iterate_end;
71
72 notify_conn(NULL, NULL, E_LOG_FATAL, ftc_warning, "%s", message);
73 notify_conn(NULL, NULL, E_LOG_FATAL, ftc_warning,
74 _("Please report this message at %s"),
75 BUG_URL);
76 }
77
78 /* Write debug/verbose message to console only when not written to file. */
79 if (!file_too || level <= LOG_NORMAL) {
80 if (console_rfcstyle) {
81 con_write(C_LOG_BASE + level, "%s", message);
82 } else {
83 con_write(C_LOG_BASE + level, "%d: %s", level, message);
84 }
85 }
86}
87
88/********************************************************************/
91 static void con_update_prompt(void)
92{
93 if (console_prompt_is_showing || !console_show_prompt) {
94 return;
95 }
96
97#ifdef FREECIV_HAVE_LIBREADLINE
98 if (readline_received_enter) {
99 readline_received_enter = FALSE;
100 } else {
101 rl_forced_update_display();
102 }
103#else /* FREECIV_HAVE_LIBREADLINE */
104 con_dump(C_READY, "> ");
105 con_flush();
106#endif /* FREECIV_HAVE_LIBREADLINE */
107
108 console_prompt_is_showing = TRUE;
109}
110
111#ifdef FREECIV_DEBUG
112/********************************************************************/
116static const char *log_prefix(void)
117{
118 static char buf[128];
119
120#ifdef LOG_TIMERS
121 char timestr[32];
122 time_t timestamp;
123 struct tm tr;
124
125 time(&timestamp);
126 strftime(timestr, sizeof(timestr), "%Y/%m/%d %H:%M:%S",
127 fc_localtime(&timestamp, &tr));
128
129 fc_snprintf(buf, sizeof(buf), "T%03d - %s", game.info.turn, timestr);
130
131#else /* LOG_TIMERS */
132 fc_snprintf(buf, sizeof(buf), "T%03d", game.info.turn);
133#endif /* LOG_TIMERS */
134
135 return buf;
136}
137#endif /* FREECIV_DEBUG */
138
139/********************************************************************/
142 static void depr_warn_callback(const char *msg)
143{
144 notify_conn(NULL, NULL, E_DEPRECATION_WARNING, ftc_warning, "%s", msg);
145}
146
147/********************************************************************/
150 void con_log_init(const char *log_filename, enum log_level level,
151 int fatal_assertions)
152{
153#ifdef FREECIV_DEBUG
154 log_init(log_filename, level, con_handle_log, log_prefix,
155 fatal_assertions);
156#else
157 log_init(log_filename, level, con_handle_log, NULL,
158 fatal_assertions);
159#endif /* FREECIV_DEBUG */
160 backtrace_init();
161 deprecation_warn_cb_set(depr_warn_callback);
162}
163
164/********************************************************************/
167 void con_log_close(void)
168{
169 backtrace_deinit();
170
171 log_close();
172}
173
174#ifndef FREECIV_HAVE_LIBREADLINE
175/********************************************************************/
178 static int con_dump(enum rfc_status rfc_status, const char *message, ...)
179{
180 static char buf[MAX_LEN_CONSOLE_LINE];
181 va_list args;
182
183 va_start(args, message);
184 fc_vsnprintf(buf, sizeof(buf), message, args);
185 va_end(args);
186
187 if (console_prompt_is_showing) {
188 fc_printf("\n");
189 }
190 if ((console_rfcstyle) && (rfc_status >= 0)) {
191 fc_printf("%.3d %s", rfc_status, buf);
192 } else {
193 fc_printf("%s", buf);
194 }
195 console_prompt_is_showing = FALSE;
196 return (int) strlen(buf);
197}
198#endif /* FREECIV_HAVE_LIBREADLINE */
199
200/********************************************************************/
203 void con_write(enum rfc_status rfc_status, const char *message, ...)
204{
205 /* First buffer contains featured text tags */
206 static char buf1[(MAX_LEN_CONSOLE_LINE * 3) / 2];
207 static char buf2[MAX_LEN_CONSOLE_LINE];
208 va_list args;
209
210 va_start(args, message);
211 fc_vsnprintf(buf1, sizeof(buf1), message, args);
212 va_end(args);
213
214 /* remove all format tags */
215 featured_text_to_plain_text(buf1, buf2, sizeof(buf2), NULL, FALSE);
216 con_puts(rfc_status, buf2);
217}
218
219/********************************************************************/
226 void con_puts(enum rfc_status rfc_status, const char *str)
227{
228 if (console_prompt_is_showing) {
229 fc_printf("\n");
230 }
231 if ((console_rfcstyle) && (rfc_status >= 0)) {
232 fc_printf("%.3d %s\n", rfc_status, str);
233 } else {
234 fc_printf("%s\n", str);
235 }
236 console_prompt_is_showing = FALSE;
237 con_update_prompt();
238}
239
240/********************************************************************/
243 void con_flush(void)
244{
245 fflush(stdout);
246}
247
248/********************************************************************/
251 void con_set_style(bool i)
252{
253 console_rfcstyle = i;
254 if (console_rfcstyle) {
255 con_puts(C_OK, _("Ok. RFC-style set."));
256 } else {
257 con_puts(C_OK, _("Ok. Standard style set."));
258 }
259}
260
261/********************************************************************/
264 bool con_get_style(void)
265{
266 return console_rfcstyle;
267}
268
269/********************************************************************/
272 void con_prompt_init(void)
273{
274 static bool first = TRUE;
275
276 if (first) {
277 con_puts(C_COMMENT, "");
278 con_puts(C_COMMENT, _("For introductory help, type 'help'."));
279 first = FALSE;
280 }
281}
282
283/********************************************************************/
286 void con_prompt_on(void)
287{
288 console_show_prompt = TRUE;
289 con_update_prompt();
290}
291
292/********************************************************************/
295 void con_prompt_off(void)
296{
297 console_show_prompt = FALSE;
298}
299
300/********************************************************************/
303 void con_prompt_enter(void)
304{
305 console_prompt_is_showing = FALSE;
306#ifdef FREECIV_HAVE_LIBREADLINE
307 readline_received_enter = TRUE;
308#endif
309}
310
311/********************************************************************/
314 void con_prompt_enter_clear(void)
315{
316 console_prompt_is_showing = TRUE;
317#ifdef FREECIV_HAVE_LIBREADLINE
318 readline_received_enter = FALSE;
319#endif
320}
str
#define str
Definition astring.c:76
conn_list_iterate
#define conn_list_iterate(connlist, pconn)
Definition connection.h:113
conn_list_iterate_end
#define conn_list_iterate_end
Definition connection.h:115
con_dump
static int con_dump(enum rfc_status rfc_status, const char *message,...)
depr_warn_callback
static void depr_warn_callback(const char *msg)
Definition console.c:142
con_get_style
bool con_get_style(void)
Definition console.c:264
console_rfcstyle
static bool console_rfcstyle
Definition console.c:47
con_update_prompt
static void con_update_prompt(void)
Definition console.c:91
con_log_init
void con_log_init(const char *log_filename, enum log_level level, int fatal_assertions)
Definition console.c:150
console_prompt_is_showing
static bool console_prompt_is_showing
Definition console.c:46
con_prompt_enter_clear
void con_prompt_enter_clear(void)
Definition console.c:314
con_set_style
void con_set_style(bool i)
Definition console.c:251
con_flush
void con_flush(void)
Definition console.c:243
con_prompt_init
void con_prompt_init(void)
Definition console.c:272
con_write
void con_write(enum rfc_status rfc_status, const char *message,...)
Definition console.c:203
con_prompt_enter
void con_prompt_enter(void)
Definition console.c:303
con_puts
void con_puts(enum rfc_status rfc_status, const char *str)
Definition console.c:226
con_handle_log
static void con_handle_log(enum log_level level, const char *message, bool file_too)
Definition console.c:58
con_prompt_off
void con_prompt_off(void)
Definition console.c:295
con_prompt_on
void con_prompt_on(void)
Definition console.c:286
console_show_prompt
static bool console_show_prompt
Definition console.c:45
con_log_close
void con_log_close(void)
Definition console.c:167
console.h
rfc_status
rfc_status
Definition console.h:35
C_OK
@ C_OK
Definition console.h:41
C_READY
@ C_READY
Definition console.h:51
C_LOG_BASE
@ C_LOG_BASE
Definition console.h:40
C_COMMENT
@ C_COMMENT
Definition console.h:37
MAX_LEN_CONSOLE_LINE
#define MAX_LEN_CONSOLE_LINE
Definition console.h:19
deprecation_warn_cb_set
void deprecation_warn_cb_set(deprecation_warn_callback new_cb)
Definition deprecations.c:51
deprecations.h
fc_prehdrs.h
backtrace_deinit
void backtrace_deinit(void)
Definition fcbacktrace.c:65
backtrace_init
void backtrace_init(void)
Definition fcbacktrace.c:55
fcbacktrace.h
fciconv.h
fc_printf
#define fc_printf(...)
Definition fciconv.h:109
fcintl.h
_
#define _(String)
Definition fcintl.h:67
featured_text_to_plain_text
size_t featured_text_to_plain_text(const char *featured_text, char *plain_text, size_t plain_text_len, struct text_tag_list **tags, bool replace_link_text)
Definition featured_text.c:864
ftc_warning
const struct ft_color ftc_warning
Definition featured_text.c:75
game
struct civ_game game
Definition game.c:57
game.h
log_close
void log_close(void)
Definition log.c:270
log_init
void log_init(const char *filename, enum log_level initial_level, log_callback_fn callback, log_prefix_fn prefix, int fatal_assertions)
Definition log.c:245
log_filename
static char * log_filename
Definition log.c:39
log_prefix
static log_prefix_fn log_prefix
Definition log.c:42
log.h
log_level
log_level
Definition log.h:28
LOG_ERROR
@ LOG_ERROR
Definition log.h:30
LOG_NORMAL
@ LOG_NORMAL
Definition log.h:32
LOG_FATAL
@ LOG_FATAL
Definition log.h:29
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
fatal_assertions
static int fatal_assertions
Definition ruledit.cpp:58
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
srv_main.h
civ_game::est_connections
struct conn_list * est_connections
Definition game.h:97
civ_game::info
struct packet_game_info info
Definition game.h:89
message
Definition messagewin_common.h:29
packet_game_info::turn
int turn
Definition packets_gen.h:241
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
fc_localtime
struct tm * fc_localtime(const time_t *timep, struct tm *result)
Definition support.c:1310
fc_vsnprintf
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:896
support.h
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47