Freeciv21
Develop your civilization from humble roots to a global empire
console.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include <fc_config.h>
15 
16 #include <cstdarg>
17 #include <cstdio>
18 
19 // utility
20 #include "fcbacktrace.h"
21 #include "fciconv.h"
22 #include "fcintl.h"
23 #include "log.h"
24 #include "support.h"
25 
26 // ms clang wants readline to included here
27 #include <readline/readline.h>
28 // common
29 #include "game.h"
30 
31 // server
32 #include "console.h"
33 #include "notify.h"
34 
35 static bool console_show_prompt = false;
36 static bool console_prompt_is_showing = false;
37 static bool console_rfcstyle = false;
38 static bool readline_received_enter = true;
39 
40 namespace {
41 static QString log_prefix();
42 static QtMessageHandler original_handler = nullptr;
43 
47 static void console_handle_message(QtMsgType type,
48  const QMessageLogContext &context,
49  const QString &message)
50 {
52  if (type == QtCriticalMsg) {
54  notify_conn(nullptr, nullptr, E_LOG_ERROR, ftc_warning, "%s",
55  qUtf8Printable(message));
56  } else if (type == QtFatalMsg) {
57  // Make sure that message is not left to buffers when server dies
59  {
60  pconn->send_buffer->do_buffer_sends = 0;
61  pconn->compression.frozen_level = 0;
62  }
64 
65  notify_conn(nullptr, nullptr, E_LOG_FATAL, ftc_warning, "%s",
66  qUtf8Printable(message));
67  notify_conn(nullptr, nullptr, E_LOG_FATAL, ftc_warning,
68  _("Please report this message at %s"), BUG_URL);
69  }
70 
71  if (original_handler != nullptr) {
72  original_handler(type, context, log_prefix() + message);
73  }
75 }
76 } // anonymous namespace
77 
81 static void con_update_prompt()
82 {
84  return;
85  }
86 
89  } else {
90  rl_forced_update_display();
91  }
93 }
94 
99 namespace {
100 static QString log_prefix()
101 {
102  // TRANS: T for turn
103  return game.info.turn > 0 ? QString::asprintf(_("T%03d: "), game.info.turn)
104  : QLatin1String("");
105 }
106 } // anonymous namespace
107 
111 void con_log_init(const QString &log_filename)
112 {
113  log_set_file(log_filename);
114  backtrace_init();
115 
116  // Install our handler last so it gets executed first
117  original_handler = qInstallMessageHandler(console_handle_message);
118 }
119 
124 {
126 
127  log_close();
128 }
129 
130 void con_set_color(const char *col)
131 {
132  fc_printf("%s", col);
135 }
139 void con_write(enum rfc_status rfc_status, const char *message, ...)
140 {
141  // First buffer contains featured text tags
142  static char buf1[(MAX_LEN_CONSOLE_LINE * 3) / 2];
143  static char buf2[MAX_LEN_CONSOLE_LINE];
144  va_list args;
145 
146  va_start(args, message);
147  fc_vsnprintf(buf1, sizeof(buf1), message, args);
148  va_end(args);
149 
150  // remove all format tags
151  featured_text_to_plain_text(buf1, buf2, sizeof(buf2), nullptr, false);
152  con_puts(rfc_status, buf2);
153 }
154 
162 void con_puts(enum rfc_status rfc_status, const char *str)
163 {
164  if (rfc_status > 0) {
166  }
168  fc_printf("\n");
169  }
170  if ((console_rfcstyle) && (rfc_status >= 0)) {
171  fc_printf("%.3d %s\n", rfc_status, str);
172  } else {
173  fc_printf("%s\n", str);
174  }
178 }
179 
183 void con_flush() { fflush(stdout); }
184 
188 void con_set_style(bool i)
189 {
190  console_rfcstyle = i;
191  if (console_rfcstyle) {
192  con_puts(C_OK, _("Ok. RFC-style set."));
193  } else {
194  con_puts(C_OK, _("Ok. Standard style set."));
195  }
196 }
197 
201 bool con_get_style() { return console_rfcstyle; }
202 
207 {
208  static bool first = true;
209 
210  if (first) {
211  con_puts(C_COMMENT, "");
212  con_puts(C_COMMENT, _("For introductory help, type 'help'."));
213  first = false;
214  }
215 }
216 
221 
226 {
229 }
#define conn_list_iterate(connlist, pconn)
Definition: connection.h:99
#define conn_list_iterate_end
Definition: connection.h:101
void con_log_init(const QString &log_filename)
Initialize logging via console.
Definition: console.cpp:111
void con_prompt_off()
Do not print a prompt after log messages.
Definition: console.cpp:220
void con_flush()
Ensure timely update.
Definition: console.cpp:183
static bool console_rfcstyle
Definition: console.cpp:37
static bool console_prompt_is_showing
Definition: console.cpp:36
void con_log_close()
Deinitialize logging.
Definition: console.cpp:123
void con_set_style(bool i)
Set style.
Definition: console.cpp:188
void con_prompt_init()
Initialize prompt; display initial message.
Definition: console.cpp:206
static void con_update_prompt()
Print the prompt if it is not the last thing printed.
Definition: console.cpp:81
static bool readline_received_enter
Definition: console.cpp:38
void con_write(enum rfc_status rfc_status, const char *message,...)
Write to console and add line-break, and show prompt if required.
Definition: console.cpp:139
bool con_get_style()
Returns rfc-style.
Definition: console.cpp:201
void con_prompt_enter()
User pressed enter: will need a new prompt.
Definition: console.cpp:225
void con_puts(enum rfc_status rfc_status, const char *str)
Write to console and add line-break, and show prompt if required.
Definition: console.cpp:162
void con_set_color(const char *col)
Definition: console.cpp:130
static bool console_show_prompt
Definition: console.cpp:35
#define CON_RESET
Definition: console.h:58
#define CON_GREEN
Definition: console.h:52
rfc_status
Definition: console.h:36
@ C_OK
Definition: console.h:41
@ C_COMMENT
Definition: console.h:37
#define CON_RED
Definition: console.h:51
#define MAX_LEN_CONSOLE_LINE
Definition: console.h:19
#define CON_YELLOW
Definition: console.h:53
void backtrace_init()
Take backtrace log callback to use.
Definition: fcbacktrace.cpp:48
void backtrace_deinit()
Remove backtrace log callback from use.
Definition: fcbacktrace.cpp:58
#define fc_printf(...)
Definition: fciconv.h:84
#define _(String)
Definition: fcintl.h:50
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)
Separate the text from the text features.
const struct ft_color ftc_warning
struct civ_game game
Definition: game.cpp:47
void log_set_file(const QString &path)
Redirects the log to a file.
Definition: log.cpp:177
void log_close()
Deinitialize logging module.
Definition: log.cpp:212
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
See notify_conn_packet - this is just the "non-v" version, with varargs.
Definition: notify.cpp:235
struct conn_list * est_connections
Definition: game.h:88
struct packet_game_info info
Definition: game.h:80
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition: support.cpp:512