Skip to content

Instantly share code, notes, and snippets.



Last active Jan 31, 2018
What would you like to do?
include debug.h to c++ projects using std::cout to enable macro debugging, enable debugging by defining DEBUG
#ifndef __DEBUG_H__
#define __DEBUG_H__
This Debug Header is used improve debugging.
#define DEBUG
#include "debug.h"
//Insert rest of code here
There are a few macros that are enabled by DEBUG and disabled without it.
#ifdef DEBUG
#include <string>
#include <iostream>
#include <iomanip>
Debug print to cout withought an endline
@param x anything compatible with cout, even expressions work.
DEBUG_PRINT("foo" << bar);
#define DEBUG_PRINT(x) std::cout << x
Print to cout with an endline.
@param x anything compatible with cout.
#define DEBUG_PRINTLN(x) std::cout << x << std::endl
void debugPrintBar(std::string input);
Print a Bar of `=` with centered text inside
@param x a string of what to be displayed
I redefined the function as a macro because I like the way visual studio highlights them,
also I wanted to keep it standardized.
#define DEBUG_PRINT_BAR(x) debugPrintBar(x)
void debugPrintBar(std::string input) {
const int width = 119;
int size = input.length();
std::cout << std::setw(width / 2 - size / 2) << std::setfill('=') << "="
<< std::setw(width / 2 + size / 2) << std::left << input << std::endl;
// When DEBUG is not defined, we still need to define the functions from above, so the
// precompiler knows to remove it during compile time.
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINT_BAR(x)
The Memory leak check that was provided to us.
@note The check is not disabled when DEBUG is not defined.
#ifdef _MSC_VER
#include <crtdbg.h>
#define VS_MEM_CHECK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment