Created
May 25, 2020 17:02
-
-
Save ryutorion/5ef8343c8e75dd6001664a34cfa835f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <cstdint> | |
#include <cinttypes> | |
#include <cstdio> | |
#include <cstdarg> | |
#include <iostream> | |
void log(const char * file_name, uint64_t line_number, const char * function_name, const char * format, ...) | |
{ | |
char buffer[21]; | |
int written = snprintf(buffer, sizeof(buffer), "[%s(%" PRIu64 ")]@%s:", file_name, line_number, function_name); | |
if(written < 0 || written >= sizeof(buffer)) | |
{ | |
fprintf(stderr, "header over %d\n", written); | |
return ; | |
} | |
va_list args; | |
va_start(args, format); | |
int result = vsnprintf(buffer + written, sizeof(buffer) - written, format, args); | |
va_end(args); | |
if(result < 0 || (written + result) >= sizeof(buffer)) | |
{ | |
fprintf(stderr, "contents over (%d, %d)\n", written, result); | |
return ; | |
} | |
printf("%s", buffer); | |
} | |
namespace A | |
{ | |
void B() | |
{ | |
log(__FILE__, __LINE__, __func__, "Hello, A::B()\n"); | |
} | |
} | |
int main(int argc, char * argv[]) | |
{ | |
log(__FILE__, __LINE__, __func__, "Hello, Log\n"); | |
A::B(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment