Last active
November 24, 2023 08:27
-
-
Save johntyree/4718393 to your computer and use it in GitHub Desktop.
C++ TRACE and LOG macros.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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