Freeciv-3.3
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
47static bool console_rfcstyle = FALSE;
48#ifdef FREECIV_HAVE_LIBREADLINE
49static bool readline_received_enter = TRUE;
50#else
51static int con_dump(enum rfc_status rfc_status, const char *message, ...);
52#endif
53
54/********************************************************************/
58static void con_handle_log(enum log_level level, const char *message,
59 bool file_too)
60{
61 if (LOG_ERROR == level) {
63 } else if (LOG_FATAL >= level) {
64 /* Make sure that message is not left to buffers when server dies */
66 pconn->send_buffer->do_buffer_sends = 0;
67#ifdef USE_COMPRESSION
68 pconn->compression.frozen_level = 0;
69#endif
71
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) {
82 } else {
83 con_write(C_LOG_BASE + level, "%d: %s", level, message);
84 }
85 }
86}
87
88/********************************************************************/
91static void con_update_prompt(void)
92{
94 return;
95 }
96
97#ifdef FREECIV_HAVE_LIBREADLINE
100 } else {
102 }
103#else /* FREECIV_HAVE_LIBREADLINE */
104 con_dump(C_READY, "> ");
105 con_flush();
106#endif /* FREECIV_HAVE_LIBREADLINE */
107
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/********************************************************************/
142static void depr_warn_callback(const char *msg)
143{
145}
146
147/********************************************************************/
152{
153#ifdef FREECIV_DEBUG
156#else
159#endif /* FREECIV_DEBUG */
162}
163
164/********************************************************************/
168{
170
171 log_close();
172}
173
174#ifndef FREECIV_HAVE_LIBREADLINE
175/********************************************************************/
178static 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
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 }
196 return (int) strlen(buf);
197}
198#endif /* FREECIV_HAVE_LIBREADLINE */
199
200/********************************************************************/
203void 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 */
217}
218
219/********************************************************************/
226void con_puts(enum rfc_status rfc_status, const char *str)
227{
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 }
238}
239
240/********************************************************************/
243void con_flush(void)
244{
245 fflush(stdout);
246}
247
248/********************************************************************/
251void con_set_style(bool i)
252{
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/********************************************************************/
265{
266 return console_rfcstyle;
267}
268
269/********************************************************************/
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/********************************************************************/
287{
290}
291
292/********************************************************************/
296{
298}
299
300/********************************************************************/
304{
306#ifdef FREECIV_HAVE_LIBREADLINE
308#endif
309}
310
311/********************************************************************/
315{
317#ifdef FREECIV_HAVE_LIBREADLINE
319#endif
320}
#define str
Definition astring.c:76
char * incite_cost
Definition comments.c:76
#define conn_list_iterate(connlist, pconn)
Definition connection.h:108
#define conn_list_iterate_end
Definition connection.h:110
static int con_dump(enum rfc_status rfc_status, const char *message,...)
static void depr_warn_callback(const char *msg)
Definition console.c:142
bool con_get_style(void)
Definition console.c:264
static bool console_rfcstyle
Definition console.c:47
static void con_update_prompt(void)
Definition console.c:91
void con_log_init(const char *log_filename, enum log_level level, int fatal_assertions)
Definition console.c:150
static bool console_prompt_is_showing
Definition console.c:46
void con_prompt_enter_clear(void)
Definition console.c:314
void con_set_style(bool i)
Definition console.c:251
void con_flush(void)
Definition console.c:243
void con_prompt_init(void)
Definition console.c:272
void con_write(enum rfc_status rfc_status, const char *message,...)
Definition console.c:203
void con_prompt_enter(void)
Definition console.c:303
void con_puts(enum rfc_status rfc_status, const char *str)
Definition console.c:226
static void con_handle_log(enum log_level level, const char *message, bool file_too)
Definition console.c:58
void con_prompt_off(void)
Definition console.c:295
void con_prompt_on(void)
Definition console.c:286
static bool console_show_prompt
Definition console.c:45
void con_log_close(void)
Definition console.c:167
rfc_status
Definition console.h:35
@ C_OK
Definition console.h:41
@ C_READY
Definition console.h:51
@ C_LOG_BASE
Definition console.h:40
@ C_COMMENT
Definition console.h:37
#define MAX_LEN_CONSOLE_LINE
Definition console.h:19
void deprecation_warn_cb_set(deprecation_warn_callback new_cb)
void backtrace_deinit(void)
Definition fcbacktrace.c:65
void backtrace_init(void)
Definition fcbacktrace.c:55
#define fc_printf(...)
Definition fciconv.h:109
#define _(String)
Definition fcintl.h:67
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)
const struct ft_color ftc_warning
struct civ_game game
Definition game.c:61
void log_close(void)
Definition log.c:270
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
static char * log_filename
Definition log.c:39
static log_prefix_fn log_prefix
Definition log.c:42
log_level
Definition log.h:29
@ LOG_ERROR
Definition log.h:31
@ LOG_NORMAL
Definition log.h:33
@ LOG_FATAL
Definition log.h:30
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
static int fatal_assertions
Definition ruledit.cpp:58
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
struct conn_list * est_connections
Definition game.h:97
struct packet_game_info info
Definition game.h:89
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
struct tm * fc_localtime(const time_t *timep, struct tm *result)
Definition support.c:1301
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:886
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47