1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/***********************************************************************
Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif
#include "fc_prehdrs.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#ifdef FREECIV_HAVE_LIBREADLINE
#include <readline/readline.h>
#endif
/* utility */
#include "deprecations.h"
#include "fcbacktrace.h"
#include "fciconv.h"
#include "fcintl.h"
#include "log.h"
#include "support.h"
/* common */
#include "game.h"
/* server */
#include "notify.h"
#include "srv_main.h"
#include "console.h"
static bool console_show_prompt = FALSE;
static bool console_prompt_is_showing = FALSE;
static bool console_rfcstyle = FALSE;
#ifdef FREECIV_HAVE_LIBREADLINE
static bool readline_received_enter = TRUE;
#else
static int con_dump(enum rfc_status rfc_status, const char *message, ...);
#endif
/********************************************************************//**
Function to handle log messages.
This must match the log_callback_fn typedef signature.
************************************************************************/
static void con_handle_log(enum log_level level, const char *message,
bool file_too)
{
if (LOG_ERROR == level) {
notify_conn(NULL, NULL, E_LOG_ERROR, ftc_warning, "%s", message);
} else if (LOG_FATAL >= level) {
/* Make sure that message is not left to buffers when server dies */
conn_list_iterate(game.est_connections, pconn) {
pconn->send_buffer->do_buffer_sends = 0;
#ifdef USE_COMPRESSION
pconn->compression.frozen_level = 0;
#endif
} conn_list_iterate_end;
notify_conn(NULL, NULL, E_LOG_FATAL, ftc_warning, "%s", message);
notify_conn(NULL, NULL, E_LOG_FATAL, ftc_warning,
_("Please report this message at %s"),
BUG_URL);
}
/* Write debug/verbose message to console only when not written to file. */
if (!file_too || level <= LOG_NORMAL) {
if (console_rfcstyle) {
con_write(C_LOG_BASE + level, "%s", message);
} else {
con_write(C_LOG_BASE + level, "%d: %s", level, message);
}
}
}
/********************************************************************//**
Print the prompt if it is not the last thing printed.
************************************************************************/
static void con_update_prompt(void)
{
if (console_prompt_is_showing || !console_show_prompt) {
return;
}
#ifdef FREECIV_HAVE_LIBREADLINE
if (readline_received_enter) {
readline_received_enter = FALSE;
} else {
rl_forced_update_display();
}
#else /* FREECIV_HAVE_LIBREADLINE */
con_dump(C_READY, "> ");
con_flush();
#endif /* FREECIV_HAVE_LIBREADLINE */
console_prompt_is_showing = TRUE;
}
#ifdef FREECIV_DEBUG
/********************************************************************//**
Prefix for log messages saved to file. At the moment the turn and the
current date and time are used.
************************************************************************/
static const char *log_prefix(void)
{
static char buf[128];
#ifdef LOG_TIMERS
char timestr[32];
time_t timestamp;
struct tm tr;
time(×tamp);
strftime(timestr, sizeof(timestr), "%Y/%m/%d %H:%M:%S",
fc_localtime(×tamp, &tr));
fc_snprintf(buf, sizeof(buf), "T%03d - %s", game.info.turn, timestr);
#else /* LOG_TIMERS */
fc_snprintf(buf, sizeof(buf), "T%03d", game.info.turn);
#endif /* LOG_TIMERS */
return buf;
}
#endif /* FREECIV_DEBUG */
/********************************************************************//**
Deprecation warning callback to send event to clients.
************************************************************************/
static void depr_warn_callback(const char *msg)
{
notify_conn(NULL, NULL, E_DEPRECATION_WARNING, ftc_warning, "%s", msg);
}
/********************************************************************//**
Initialize logging via console.
************************************************************************/
void con_log_init(const char *log_filename, enum log_level level,
int fatal_assertions)
{
#ifdef FREECIV_DEBUG
log_init(log_filename, level, con_handle_log, log_prefix,
fatal_assertions);
#else
log_init(log_filename, level, con_handle_log, NULL,
fatal_assertions);
#endif /* FREECIV_DEBUG */
backtrace_init();
deprecation_warn_cb_set(depr_warn_callback);
}
/********************************************************************//**
Deinitialize logging
************************************************************************/
void con_log_close(void)
{
backtrace_deinit();
log_close();
}
#ifndef FREECIV_HAVE_LIBREADLINE
/********************************************************************//**
Write to console without line-break, don't print prompt.
************************************************************************/
static int con_dump(enum rfc_status rfc_status, const char *message, ...)
{
static char buf[MAX_LEN_CONSOLE_LINE];
va_list args;
va_start(args, message);
fc_vsnprintf(buf, sizeof(buf), message, args);
va_end(args);
if (console_prompt_is_showing) {
fc_printf("\n");
}
if ((console_rfcstyle) && (rfc_status >= 0)) {
fc_printf("%.3d %s", rfc_status, buf);
} else {
fc_printf("%s", buf);
}
console_prompt_is_showing = FALSE;
return (int) strlen(buf);
}
#endif /* FREECIV_HAVE_LIBREADLINE */
/********************************************************************//**
Write to console and add line-break, and show prompt if required.
************************************************************************/
void con_write(enum rfc_status rfc_status, const char *message, ...)
{
/* First buffer contains featured text tags */
static char buf1[(MAX_LEN_CONSOLE_LINE * 3) / 2];
static char buf2[MAX_LEN_CONSOLE_LINE];
va_list args;
va_start(args, message);
fc_vsnprintf(buf1, sizeof(buf1), message, args);
va_end(args);
/* remove all format tags */
featured_text_to_plain_text(buf1, buf2, sizeof(buf2), NULL, FALSE);
con_puts(rfc_status, buf2);
}
/********************************************************************//**
Write to console and add line-break, and show prompt if required.
Same as con_write, but without the format string stuff.
The real reason for this is because __attribute__ complained
with con_write(C_COMMENT, "") of "warning: zero-length format string";
this allows con_puts(C_COMMENT, "");
************************************************************************/
void con_puts(enum rfc_status rfc_status, const char *str)
{
if (console_prompt_is_showing) {
fc_printf("\n");
}
if ((console_rfcstyle) && (rfc_status >= 0)) {
fc_printf("%.3d %s\n", rfc_status, str);
} else {
fc_printf("%s\n", str);
}
console_prompt_is_showing = FALSE;
con_update_prompt();
}
/********************************************************************//**
Ensure timely update.
************************************************************************/
void con_flush(void)
{
fflush(stdout);
}
/********************************************************************//**
Set style.
************************************************************************/
void con_set_style(bool i)
{
console_rfcstyle = i;
if (console_rfcstyle) {
con_puts(C_OK, _("Ok. RFC-style set."));
} else {
con_puts(C_OK, _("Ok. Standard style set."));
}
}
/********************************************************************//**
Returns rfc-style.
************************************************************************/
bool con_get_style(void)
{
return console_rfcstyle;
}
/********************************************************************//**
Initialize prompt; display initial message.
************************************************************************/
void con_prompt_init(void)
{
static bool first = TRUE;
if (first) {
con_puts(C_COMMENT, "");
con_puts(C_COMMENT, _("For introductory help, type 'help'."));
first = FALSE;
}
}
/********************************************************************//**
Make sure a prompt is printed, and re-printed after every message.
************************************************************************/
void con_prompt_on(void)
{
console_show_prompt = TRUE;
con_update_prompt();
}
/********************************************************************//**
Do not print a prompt after log messages.
************************************************************************/
void con_prompt_off(void)
{
console_show_prompt = FALSE;
}
/********************************************************************//**
User pressed enter: will need a new prompt
************************************************************************/
void con_prompt_enter(void)
{
console_prompt_is_showing = FALSE;
#ifdef FREECIV_HAVE_LIBREADLINE
readline_received_enter = TRUE;
#endif
}
/********************************************************************//**
Clear "user pressed enter" state (used in special cases).
************************************************************************/
void con_prompt_enter_clear(void)
{
console_prompt_is_showing = TRUE;
#ifdef FREECIV_HAVE_LIBREADLINE
readline_received_enter = FALSE;
#endif
}
|