Print variables with file and line number

Submitted by:Tim Kelly

Date added:24 November, 2012

Category:C++

Print variables with file and line number for debugging source code

Tags: print variables

Code Snippet:

#ifndef __TEMP__H__
#define __TEMP__H__
//
// Author: Mike Lear mikeofthenight2003@yahoo.com
//
// TEMP.H: Display messages - variables whilst testing source code
//
// NOTE:
// This header uses macros which should not be left in the finished program.
// Renove this header and any macros used once testing is completed.
//
// Usage:
// Place this header file in your include path, then include the temp header in
// your source code:
// IE:
// #include <iostream>
// #include <string>
// #include <temp.h>
//
// You can then place SV(var); or DV(var,var); anywhere in your code to display
// variables,messages etc during testing.
//
// SV(var); -- Displays the file then line number and a Single Variable
// DV(var,var); -- Displays the file then line number and two (Dual Variables)
//
// Example Output
// vmake.cc: line no: 225: count = 0
// vmake.cc: line no: 230: FunKey.test(F7) = 0, FunKey.test(F8) = 1
// vmake.cc: line no: 231: count = 1


#define SV(m) std::cerr << __FILE__ << ": line no: " << __LINE__ << ": " << #m " = " << m << std::endl;
#define DV(m,l) std::cerr << __FILE__ << ": line no: " << __LINE__ << ": " << #m " = " << m << ", " << #l " = " << l << std::endl;

#endif
 
 

Comments