Changeset - r25660:a71b98216c01
[Not reviewed]
master
0 2 0
rubidium42 - 3 years ago 2021-06-12 18:43:37
rubidium@openttd.org
Codechange: add an IConsolePrint overload that does formatting with fmt
2 files changed with 29 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/console.cpp
Show inline comments
 
@@ -79,46 +79,45 @@ bool CloseConsoleLogIfActive()
 
void IConsoleFree()
 
{
 
	IConsoleGUIFree();
 
	CloseConsoleLogIfActive();
 
}
 

	
 
/**
 
 * Handle the printing of text entered into the console or redirected there
 
 * by any other means. Text can be redirected to other clients in a network game
 
 * as well as to a logfile. If the network server is a dedicated server, all activities
 
 * are also logged. All lines to print are added to a temporary buffer which can be
 
 * used as a history to print them onscreen
 
 * @param colour_code the colour of the command. Red in case of errors, etc.
 
 * @param string the message entered or output on the console (notice, error, etc.)
 
 * @param colour_code The colour of the command.
 
 * @param string The message to output on the console (notice, error, etc.)
 
 */
 
void IConsolePrint(TextColour colour_code, const char *string)
 
void IConsolePrint(TextColour colour_code, const std::string &string)
 
{
 
	assert(IsValidConsoleColour(colour_code));
 

	
 
	char *str;
 
	if (_redirect_console_to_client != INVALID_CLIENT_ID) {
 
		/* Redirect the string to the client */
 
		NetworkServerSendRcon(_redirect_console_to_client, colour_code, string);
 
		return;
 
	}
 

	
 
	if (_redirect_console_to_admin != INVALID_ADMIN_ID) {
 
		NetworkServerSendAdminRcon(_redirect_console_to_admin, colour_code, string);
 
		return;
 
	}
 

	
 
	/* Create a copy of the string, strip if of colours and invalid
 
	 * characters and (when applicable) assign it to the console buffer */
 
	str = stredup(string);
 
	char *str = stredup(string.c_str());
 
	str_strip_colours(str);
 
	StrMakeValidInPlace(str);
 

	
 
	if (_network_dedicated) {
 
		NetworkAdminConsole("console", str);
 
		fprintf(stdout, "%s%s\n", GetLogPrefix(), str);
 
		fflush(stdout);
 
		IConsoleWriteToLogFile(str);
 
		free(str); // free duplicated string since it's not used anymore
 
		return;
 
	}
 

	
src/console_func.h
Show inline comments
 
@@ -2,34 +2,58 @@
 
 * This file is part of OpenTTD.
 
 * OpenTTD 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, version 2.
 
 * OpenTTD 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. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file console_func.h Console functions used outside of the console code. */
 

	
 
#ifndef CONSOLE_FUNC_H
 
#define CONSOLE_FUNC_H
 

	
 
#include "console_type.h"
 
#include "3rdparty/fmt/format.h"
 

	
 
/* console modes */
 
extern IConsoleModes _iconsole_mode;
 

	
 
/* console functions */
 
void IConsoleInit();
 
void IConsoleFree();
 
void IConsoleClose();
 

	
 
/* console output */
 
void IConsolePrint(TextColour colour_code, const char *string);
 
void IConsolePrint(TextColour colour_code, const std::string &string);
 

	
 
/**
 
 * Handle the printing of text entered into the console or redirected there
 
 * by any other means. Text can be redirected to other clients in a network game
 
 * as well as to a logfile. If the network server is a dedicated server, all activities
 
 * are also logged. All lines to print are added to a temporary buffer which can be
 
 * used as a history to print them onscreen
 
 * @param colour_code The colour of the command.
 
 * @param format_string The formatting string to tell what to do with the remaining arguments.
 
 * @param first_arg The first argument to the format.
 
 * @param other_args The other arguments to the format.
 
 * @tparam T The type of formatting parameter.
 
 * @tparam A The type of the first argument.
 
 * @tparam Args The types of the other arguments.
 
 */
 
template <typename T, typename A, typename ... Args>
 
static inline void IConsolePrint(TextColour colour_code, const T &format, A first_arg, Args&&... other_args)
 
{
 
	/* The separate first_arg argument is added to aid overloading.
 
	 * Otherwise the calls that do no need formatting will still use this function. */
 
	IConsolePrint(colour_code, fmt::format(format, first_arg, other_args...));
 
}
 

	
 
void CDECL IConsolePrintF(TextColour colour_code, const char *format, ...) WARN_FORMAT(2, 3);
 
void IConsoleDebug(const char *dbg, const char *string);
 
void IConsoleWarning(const char *string);
 
void IConsoleError(const char *string);
 

	
 
/* Parser */
 
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count = 0);
 

	
 
bool IsValidConsoleColour(TextColour c);
 

	
 
#endif /* CONSOLE_FUNC_H */
0 comments (0 inline, 0 general)