Files @ r25895:573b0b525189
Branch filter:

Location: cpp/openttd-patchpack/source/src/debug.cpp - annotation

Patric Stout
Fix: [Actions] changelog assumed major.minor.patch versioning (#9482)

We now use major.minor versioning, so it failed to pick up the
correct information.
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r9336:b3c601814650
r18882:b2227172c2cf
r18882:b2227172c2cf
r10515:c73815918fba
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r23088:6710c4c79ac6
r22873:025d4805a116
r22873:025d4805a116
r22873:025d4805a116
r25479:5d0b41ec5e3a
r5959:56b708e464d2
r16264:1f7e2ab6252c
r5959:56b708e464d2
r5584:545d748cc681
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18532:5a0e8241e328
r5584:545d748cc681
r9457:ecfeb802c57a
r10515:c73815918fba
r14498:ba88afbc611b
r18988:3f7b98056f8d
r18988:3f7b98056f8d
r18988:3f7b98056f8d
r5584:545d748cc681
r6248:b940b09d7ab8
r5584:545d748cc681
r5584:545d748cc681
r6248:b940b09d7ab8
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18532:5a0e8241e328
r5584:545d748cc681
r9457:ecfeb802c57a
r10515:c73815918fba
r14499:548a06b3ce78
r18988:3f7b98056f8d
r18988:3f7b98056f8d
r18988:3f7b98056f8d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19210:e15739107602
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r19200:843e97bfe892
r16081:e554542808c8
r16081:e554542808c8
r25657:33b86bb7de01
r25657:33b86bb7de01
r16081:e554542808c8
r25657:33b86bb7de01
r5584:545d748cc681
r8409:2e7aa818987b
r25657:33b86bb7de01
r20984:a4da9302878d
r20984:a4da9302878d
r25657:33b86bb7de01
r12752:0ee2ec59ee1c
r12752:0ee2ec59ee1c
r25657:33b86bb7de01
r18988:3f7b98056f8d
r23607:36c15679007d
r18988:3f7b98056f8d
r25657:33b86bb7de01
r18988:3f7b98056f8d
r18988:3f7b98056f8d
r25657:33b86bb7de01
r18988:3f7b98056f8d
r23607:36c15679007d
r18988:3f7b98056f8d
r25657:33b86bb7de01
r18988:3f7b98056f8d
r18988:3f7b98056f8d
r18988:3f7b98056f8d
r25657:33b86bb7de01
r25657:33b86bb7de01
r25661:2509fe7b65ed
r25657:33b86bb7de01
r25661:2509fe7b65ed
r5584:545d748cc681
r5584:545d748cc681
r9457:ecfeb802c57a
r16081:e554542808c8
r16081:e554542808c8
r16081:e554542808c8
r16081:e554542808c8
r16081:e554542808c8
r16081:e554542808c8
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6125:eb40461cb765
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6125:eb40461cb765
r23607:36c15679007d
r15542:f6c6d0f8689e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15542:f6c6d0f8689e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r23607:36c15679007d
r5584:545d748cc681
r5584:545d748cc681
r11870:92eb16f6d311
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r16081:e554542808c8
r16081:e554542808c8
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r8192:a0f38ced35f2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r21387:4ac1de161213
r5584:545d748cc681
r5584:545d748cc681
r21387:4ac1de161213
r10310:bfae42dc59ac
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r23607:36c15679007d
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r25479:5d0b41ec5e3a
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
r12984:b9c53ed05d55
/*
 * 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 debug.cpp Handling of printing debug messages. */

#include "stdafx.h"
#include <stdarg.h>
#include "console_func.h"
#include "debug.h"
#include "string_func.h"
#include "fileio_func.h"
#include "settings_type.h"

#if defined(_WIN32)
#include "os/windows/win32.h"
#endif

#include "walltime_func.h"

#include "network/network_admin.h"
SOCKET _debug_socket = INVALID_SOCKET;

#include "safeguards.h"

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

struct DebugLevel {
	const char *name;
	int *level;
};

#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
	static const DebugLevel debug_level[] = {
	DEBUG_LEVEL(driver),
	DEBUG_LEVEL(grf),
	DEBUG_LEVEL(map),
	DEBUG_LEVEL(misc),
	DEBUG_LEVEL(net),
	DEBUG_LEVEL(sprite),
	DEBUG_LEVEL(oldloader),
	DEBUG_LEVEL(npf),
	DEBUG_LEVEL(yapf),
	DEBUG_LEVEL(freetype),
	DEBUG_LEVEL(script),
	DEBUG_LEVEL(sl),
	DEBUG_LEVEL(gamelog),
	DEBUG_LEVEL(desync),
	DEBUG_LEVEL(console),
#ifdef RANDOM_DEBUG
	DEBUG_LEVEL(random),
#endif
	};
#undef DEBUG_LEVEL

/**
 * Dump the available debug facility names in the help text.
 * @param buf Start address for storing the output.
 * @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 level Debug category.
 * @param message The message to output.
 */
void DebugPrint(const char *level, const std::string &message)
{
	if (_debug_socket != INVALID_SOCKET) {
		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, msg.c_str(), (int)msg.size(), 0);
		return;
	}
	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(), message.c_str());
		fflush(f);
#ifdef RANDOM_DEBUG
	} else if (strcmp(level, "random") == 0) {
		static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
		if (f == nullptr) return;

		fprintf(f, "%s\n", message.c_str());
		fflush(f);
#endif
	} else {
		std::string msg = fmt::format("{}dbg: [{}] {}\n", GetLogPrefix(), level, message);
		fputs(msg.c_str(), stderr);

		NetworkAdminConsole(level, message);
		if (_settings_client.gui.developer >= 2) IConsolePrint(CC_DEBUG, "dbg: [{}] {}", level, message);
	}
}

/**
 * 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;
	}

	/* individual levels */
	for (;;) {
		const DebugLevel *i;
		int *p;

		/* skip delimiters */
		while (*s == ' ' || *s == ',' || *s == '\t') s++;
		if (*s == '\0') break;

		t = s;
		while (*s >= 'a' && *s <= 'z') s++;

		/* check debugging levels */
		p = nullptr;
		for (i = debug_level; i != endof(debug_level); ++i) {
			if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
				p = i->level;
				break;
			}
		}

		if (*s == '=') s++;
		v = strtoul(s, &end, 0);
		s = end;
		if (p != nullptr) {
			*p = v;
		} else {
			ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
			return;
		}
	}
}

/**
 * Print out the current debug-level.
 * Just return a string with the values of all the debug categories.
 * @return string with debug-levels
 */
const char *GetDebugString()
{
	const DebugLevel *i;
	static char dbgstr[150];
	char dbgval[20];

	memset(dbgstr, 0, sizeof(dbgstr));
	i = debug_level;
	seprintf(dbgstr, lastof(dbgstr), "%s=%d", i->name, *i->level);

	for (i++; i != endof(debug_level); i++) {
		seprintf(dbgval, lastof(dbgval), ", %s=%d", i->name, *i->level);
		strecat(dbgstr, dbgval, lastof(dbgstr));
	}

	return dbgstr;
}

/**
 * Get the prefix for logs; if show_date_in_logs is enabled it returns
 * the date, otherwise it returns nothing.
 * @return the prefix for logs (do not free), never nullptr
 */
const char *GetLogPrefix()
{
	static char _log_prefix[24];
	if (_settings_client.gui.show_date_in_logs) {
		LocalTime::Format(_log_prefix, lastof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ");
	} else {
		*_log_prefix = '\0';
	}
	return _log_prefix;
}