自用debug单元

时间:2023-03-08 16:49:26

将之前的内存查看单元小幅修改,加上文件操作和计时,组成了一个自用debug单元,使用方法如示例。

此单元便捷之处在于直接将#define DEBUG注释掉而无需改动源码,即可取消debug模式。

#define DEBUG stdi|fileo
#include "debug.h"
using namespace debug;
int main()
{
	char str[] = "jiji";
	println("this is a %s", str);
}

自用debug单元

自用debug单元

自用debug单元

自用debug单元

自用debug单元

// mem.h
#ifndef __MEM_H #define __MEM_H #include <stdio.h> #ifndef MEM_DEF_LENGTH #define MEM_DEF_LENGTH (0x80) #endif #include <windows.h> #define BLACK (0) #define BLUE (1) #define GREEN (2) #define RED (4) #define YELLOW (RED|GREEN) #define CYAN (GREEN|BLUE) #define MAGENTA (BLUE|RED) #define WHITE (RED|GREEN|BLUE) #define HL (8) #ifndef HighlightFG #define HighlightFG (WHITE|HL) #endif #ifndef HighlightBG #define HighlightBG (BLACK) #endif void memdispex(FILE* fp, char *srcptr, int size, int len); #include "mem.cpp" #endif
// mem.cpp
void memdispex(FILE* fp, char *srcptr, int size, int len)
{
	char *p, *pos;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
	fprintf(fp, "\n");
	for (pos = (char *)((unsigned int)srcptr & 0xFFFFFFF0);
		pos < srcptr + len; pos += 0x10){
		fprintf(fp, "%06X:  ", pos);
		for (p = pos; p < pos + 16; ++p){
			if (p >= srcptr && p < srcptr + size){
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
					(HighlightFG) | (HighlightBG) << 4);
			}
			if (p < srcptr || p >= srcptr + len)
				fprintf(fp, "   ");
			else if (p == pos + 7)
				fprintf(fp, "%02X-", *p & 0xFF);
			else
				fprintf(fp, "%02X ", *p & 0xFF);
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
				csbi.wAttributes);
		}
		fprintf(fp, "  ");
		for (p = pos; p < pos + 16; ++p){
			if (p >= srcptr && p < srcptr + size){
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
					(HighlightFG) | (HighlightBG) << 4);
			}
			if (p < srcptr || p >= srcptr + len)
				fprintf(fp, " ");
			else if (*p < 0x80 && *p >= 0x20)
				fprintf(fp, "%c", *p & 0xFF);
			else
				fprintf(fp, ".");
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
				csbi.wAttributes);
		}
		fprintf(fp, "\n");
	}
}
// timer.h
#include <ctime>

class TimerBase
{
private:
    int _start, _result;
public:
    inline void start()
    {
		_result = 0;
        _start = clock();
    }

    inline void resume()
    {
		_start = clock();
	}

	inline int pause()
	{
		return _result += clock() - _start;
	}

    inline int stop()
    {
        return _result += clock() - _start;
    }

    inline double second()
    {
        return (double)_result / 1000;
    }

    inline int tick()
    {
        return _result;
    }
};
// debug.h
#ifndef __DEBUG_H
#define __DEBUG_H

#include <cstdio>
#include <cstdarg>
#include "mem/mem.h"
#include "timer/timer.h"

namespace debug {
	#define stdi		(1)
	#define filei		(2)

	#define stdo		(4)
	#define fileo		(8)

	#define stde		(16)

	#define stdio		(stdi|stdo)
	#define fileio		(filei|fileo)

	#undef __CONFIRM_FILE_INPUT
	#undef __CONFIRM_FILE_OUTPUT

	#ifdef DEBUG
		#if ((DEBUG) & filei) > 0
			#define __CONFIRM_FILE_INPUT
			#ifndef INPUT
				#define INPUT "input.txt"
			#endif
		#endif
		#if ((DEBUG) & fileo) > 0
			#define __CONFIRM_FILE_OUTPUT
			#ifndef OUTPUT
				#define OUTPUT "output.txt"
			#endif
		#endif
	#endif	

	class DebugFileBase
	{
	public:
		static FILE* fp[256];
	};

	FILE* DebugFileBase::fp[256] = {};

	class FileInitializer: protected DebugFileBase
	{
	public:
		FileInitializer();
		~FileInitializer();
	} file_initializer;

	// IOBase

	class InputMethod: protected DebugFileBase
	{
	public:
		InputMethod();
		virtual inline void set_device(int);
	protected:
		int device;
	};

	class OutputMethod: protected DebugFileBase
	{
	public:
		OutputMethod();
		virtual inline void set_device(int);
	protected:
		int device;
	};

	// Input Method

	class Scan: public InputMethod
	{
	public:
		inline int operator ()(const char* format, ...);
	} scan;

	// Output Method

	class Print: public OutputMethod
	{
	public:
		inline int operator ()(const char* format, ...);
	} print;

	class Println: public OutputMethod
	{
	public:
		inline int operator ()(const char* format, ...);
	} println;

	// MemDisplay Method

	class Display: public OutputMethod
	{
	public:
		template <typename T>
		inline void operator ()(const T&, const int len = (MEM_DEF_LENGTH));
	} display;

	class DisplayFixed: public OutputMethod
	{
	public:
		template <typename T>
		inline void operator ()(const T&);
	} display_fixed;

	class DisplayAddr: public OutputMethod
	{
	public:
		template <typename T>
		inline void operator ()(const T&, const int len = (MEM_DEF_LENGTH));
	} display_addr;

	// Timer

	class Timer: public TimerBase
	{
	public:
		inline int halt();
	} timer;
}

#include "debug.cpp"

#endif
// debug.cpp
debug::FileInitializer::FileInitializer()
{
	fp[stdi] = stdin;
	fp[stdo] = stdout;
	fp[stde] = stderr;
	#ifdef __CONFIRM_FILE_INPUT
		fp[filei] = fopen(INPUT, "r");
	#endif
	#ifdef __CONFIRM_FILE_OUTPUT
		fp[fileo] = fopen(OUTPUT, "w");
	#endif
}

debug::FileInitializer::~FileInitializer()
{
	#ifdef __CONFIRM_FILE_INPUT
		fclose(fp[filei]);
	#endif
	#ifdef __CONFIRM_FILE_OUTPUT
    	fclose(fp[fileo]);
	#endif
}

debug::InputMethod::InputMethod()
{
	#ifdef __CONFIRM_FILE_INPUT
		device = filei;
	#else
		device = stdi;
	#endif
}

inline void debug::InputMethod::set_device(int tag)
{
	device = tag & filei ? filei : stdi;
}

debug::OutputMethod::OutputMethod()
{
	#ifdef __CONFIRM_FILE_OUTPUT
		device = fileo;
	#else
		#ifdef DEBUG
			#if (DEBUG & stde) > 0
				device = stde;
			#else
				device = stdo;
			#endif
		#else
			device = stdo;
		#endif
	#endif
}

inline void debug::OutputMethod::set_device(int tag)
{
	device = tag & fileo ? fileo :
		tag & stde ? stde : stdo;
}

inline int debug::Scan::operator ()(const char* format, ...)
{
	#ifdef DEBUG
		va_list args;
		va_start(args, format);
		return vfscanf(fp[device], format, args);
	#else
		return 0;
	#endif
}

inline int debug::Print::operator ()(const char* format, ...)
{
	#ifdef DEBUG
		va_list args;
		va_start(args, format);
		int val = vfprintf(fp[device], format, args);
		fflush(fp[device]);
		return val;
	#else
		return 0;
	#endif
}

inline int debug::Println::operator ()(const char* format, ...)
{
	#ifdef DEBUG
		va_list args;
		va_start(args, format);
		int val = vfprintf(fp[device], format, args);
		fprintf(fp[device], "\n");
		fflush(fp[device]);
		return val;
	#else
		return 0;
	#endif
}

template <typename T>
inline void debug::Display::operator ()(const T& src, const int len)
{
	#ifdef DEBUG
		memdispex(fp[device], (char*)&src, sizeof(src), len);
	#endif
}

template <typename T>
inline void debug::DisplayFixed::operator ()(const T& src)
{
	#ifdef DEBUG
		memdispex(fp[device], (char*)&src, sizeof(src), sizeof(src));
	#endif
}

template <typename T>
inline void debug::DisplayAddr::operator ()(const T& src, const int len)
{
	#ifdef DEBUG
		memdispex(fp[device], (char*)src, 0, len);
	#endif
}

inline int debug::Timer::halt()
{
	int val = stop();
	#ifdef DEBUG
		println("Time: %dms", val);
	#endif
	return val;
}