Changeset - r25657:33b86bb7de01
[Not reviewed]
master
0 2 0
rubidium42 - 3 years ago 2021-06-12 09:32:57
rubidium@openttd.org
Codechange: use fmt in DebugPrint where applicable
2 files changed with 16 insertions and 20 deletions:
0 comments (0 inline, 0 general)
src/debug.cpp
Show inline comments
 
@@ -79,88 +79,85 @@ struct DebugLevel {
 
 * @param last Last valid address for storing the output.
 
 * @return Next free position in the output.
 
 */
 
char *DumpDebugFacilityNames(char *buf, char *last)
 
{
 
	size_t length = 0;
 
	for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
 
		if (length == 0) {
 
			buf = strecpy(buf, "List of debug facility names:\n", last);
 
		} else {
 
			buf = strecpy(buf, ", ", last);
 
			length += 2;
 
		}
 
		buf = strecpy(buf, i->name, last);
 
		length += strlen(i->name);
 
	}
 
	if (length > 0) {
 
		buf = strecpy(buf, "\n\n", last);
 
	}
 
	return buf;
 
}
 

	
 
/**
 
 * Internal function for outputting the debug line.
 
 * @param dbg Debug category.
 
 * @param buf Text line to output.
 
 * @param level Debug category.
 
 * @param message The message to output.
 
 */
 
void debug_print(const char *dbg, const char *buf)
 
void DebugPrint(const char *level, const std::string &message)
 
{
 
	if (_debug_socket != INVALID_SOCKET) {
 
		char buf2[1024 + 32];
 

	
 
		seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
 
		std::string msg = fmt::format("{}dbg: [{}] {}\n", GetLogPrefix(), level, message);
 
		/* Sending out an error when this fails would be nice, however... the error
 
		 * would have to be send over this failing socket which won't work. */
 
		send(_debug_socket, buf2, (int)strlen(buf2), 0);
 
		send(_debug_socket, msg.c_str(), (int)msg.size(), 0);
 
		return;
 
	}
 
	if (strcmp(dbg, "desync") == 0) {
 
	if (strcmp(level, "desync") == 0) {
 
		static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
 
		if (f == nullptr) return;
 

	
 
		fprintf(f, "%s%s\n", GetLogPrefix(), buf);
 
		fprintf(f, "%s%s\n", GetLogPrefix(), message.c_str());
 
		fflush(f);
 
#ifdef RANDOM_DEBUG
 
	} else if (strcmp(dbg, "random") == 0) {
 
	} else if (strcmp(level, "random") == 0) {
 
		static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
 
		if (f == nullptr) return;
 

	
 
		fprintf(f, "%s\n", buf);
 
		fprintf(f, "%s\n", message.c_str());
 
		fflush(f);
 
#endif
 
	} else {
 
		char buffer[512];
 
		seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
 
		std::string msg = fmt::format("{}dbg: [{}] {}\n", GetLogPrefix(), level, message);
 
#if defined(_WIN32)
 
		wchar_t system_buf[512];
 
		convert_to_fs(buffer, system_buf, lengthof(system_buf));
 
		convert_to_fs(msg.c_str(), system_buf, lengthof(system_buf));
 
		fputws(system_buf, stderr);
 
#else
 
		fputs(buffer, stderr);
 
		fputs(msg.c_str(), stderr);
 
#endif
 
		NetworkAdminConsole(dbg, buf);
 
		IConsoleDebug(dbg, buf);
 
		NetworkAdminConsole(level, message);
 
		IConsoleDebug(level, message.c_str());
 
	}
 
}
 

	
 
/**
 
 * Set debugging levels by parsing the text in \a s.
 
 * For setting individual levels a string like \c "net=3,grf=6" should be used.
 
 * If the string starts with a number, the number is used as global debugging level.
 
 * @param s Text describing the wanted debugging levels.
 
 */
 
void SetDebugString(const char *s)
 
{
 
	int v;
 
	char *end;
 
	const char *t;
 

	
 
	/* global debugging level? */
 
	if (*s >= '0' && *s <= '9') {
 
		const DebugLevel *i;
 

	
 
		v = strtoul(s, &end, 0);
 
		s = end;
 

	
 
		for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
 
	}
src/debug.h
Show inline comments
 
@@ -7,57 +7,56 @@
 

	
 
/** @file debug.h Functions related to debugging. */
 

	
 
#ifndef DEBUG_H
 
#define DEBUG_H
 

	
 
#include "cpu.h"
 
#include <chrono>
 
#include "3rdparty/fmt/format.h"
 

	
 
/* Debugging messages policy:
 
 * These should be the severities used for direct Debug() calls
 
 * maximum debugging level should be 10 if really deep, deep
 
 * debugging is needed.
 
 * (there is room for exceptions, but you have to have a good cause):
 
 * 0   - errors or severe warnings
 
 * 1   - other non-fatal, non-severe warnings
 
 * 2   - crude progress indicator of functionality
 
 * 3   - important debugging messages (function entry)
 
 * 4   - debugging messages (crude loop status, etc.)
 
 * 5   - detailed debugging information
 
 * 6.. - extremely detailed spamming
 
 */
 

	
 
void debug_print(const char *dbg, const char *buf);
 

	
 
/**
 
 * Ouptut a line of debugging information.
 
 * @param name The category of debug information.
 
 * @param level The maximum debug level this message should be shown at. When the debug level for this category is set lower, then the message will not be shown.
 
 * @param format_string The formatting string of the message.
 
 */
 
#define Debug(name, level, format_string, ...) if ((level) == 0 || _debug_ ## name ## _level >= (level)) debug_print(#name, fmt::format(FMT_STRING(format_string), ## __VA_ARGS__).c_str())
 
#define Debug(name, level, format_string, ...) if ((level) == 0 || _debug_ ## name ## _level >= (level)) DebugPrint(#name, fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
 
void DebugPrint(const char *level, const std::string &message);
 

	
 
extern int _debug_driver_level;
 
extern int _debug_grf_level;
 
extern int _debug_map_level;
 
extern int _debug_misc_level;
 
extern int _debug_net_level;
 
extern int _debug_sprite_level;
 
extern int _debug_oldloader_level;
 
extern int _debug_npf_level;
 
extern int _debug_yapf_level;
 
extern int _debug_freetype_level;
 
extern int _debug_script_level;
 
extern int _debug_sl_level;
 
extern int _debug_gamelog_level;
 
extern int _debug_desync_level;
 
extern int _debug_console_level;
 
#ifdef RANDOM_DEBUG
 
extern int _debug_random_level;
 
#endif
 

	
 
char *DumpDebugFacilityNames(char *buf, char *last);
 
void SetDebugString(const char *s);
 
const char *GetDebugString();
 

	
0 comments (0 inline, 0 general)