Skip to content

Instantly share code, notes, and snippets.

@johntyree
Last active November 24, 2023 08:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johntyree/4718393 to your computer and use it in GitHub Desktop.
Save johntyree/4718393 to your computer and use it in GitHub Desktop.
C++ TRACE and LOG macros.
#include <iostream>
#include <sstream>
#include "test.h"
using std::string;
void debug_printer(string type, string fn, string func, int line, string msg) {
std::cout
<< type << ": "
<< fn << "(" << line << "): "
<< func;
if (msg.size()) {
std::cout << "\n\t" << msg;
}
std::cout << std::endl;
}
void where_am_I(int x) {
TRACE;
if (x % 2) {
LOG("x("<<x<<") is odd.");
}
}
int main(int argc, char* argv[]) {
TRACE;
where_am_I(3);
where_are_you();
return 0;
}
using std::string;
#define TRACE debug_printer("TRACE", __FILE__, __PRETTY_FUNCTION__, __LINE__ , string());
#define LOG(msg) {std::ostringstream _s; _s << msg; debug_printer("LOG", __FILE__, __PRETTY_FUNCTION__, __LINE__ , _s.str());}
void debug_printer(string type, string fn, string func, int line, string msg);
void where_are_you() {
LOG("Here I am!");
}
TRACE: /dev/shm/test.cpp(27): int main(int, char**)
TRACE: /dev/shm/test.cpp(20): void where_am_I(int)
LOG: /dev/shm/test.cpp(22): void where_am_I(int)
x(3) is odd.
LOG: /dev/shm/test.h(10): void where_are_you()
Here I am!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment