Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
printf for debugging

可変長引数をサポートするデバッグ用の関数

int Error(char *fmt,...);
int Warning(char *fmt,...);
int Info(char *fmt,...);
int Log(char *fmt,...);

星とか、コメントとか文句とか適当に書いてくれると嬉しいです。

debug functions support variable arguments.

int Error(char *fmt,...);
int Warning(char *fmt,...);
int Info(char *fmt,...);
int Log(char *fmt,...);

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef __linux__
//#define BLUE(s) "\e[34m"s"\e[37m"
#define RED(s) "\033[31m"s"\033[39m"
#define Error(fmt, ...) printf(RED("\nError: %s: %d: ")fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__)
#elif __WIN32__
#include <windows.h>
#include <string.h>
HANDLE hConsole;
WORD saved_attributes;
enum LOG_CLASS{CLASS_ERROR,CLASS_WARNING,CLASS_INFO,CLASS_LOG,DEFAULT};
int color(int class){
if(class==CLASS_WARNING) SetConsoleTextAttribute(hConsole,FOREGROUND_RED);
if(class==CLASS_INFO) SetConsoleTextAttribute(hConsole,FOREGROUND_BLUE);
if(class==DEFAULT) SetConsoleTextAttribute(hConsole,saved_attributes);
}
#define print(fmt, ...) do{ color(CLASS_LOG); printf("%s %d ",__func__,__LINE__); printf(fmt,##__VA_ARGS__); color(DEFAULT); }while(0)
#define log(fmt, ...) do{ color(CLASS_LOG); printf("%s %d ",__func__,__LINE__); printf(fmt,##__VA_ARGS__); color(DEFAULT); }while(0)
#define warning(fmt, ...) do{ color(CLASS_WARNING); printf("%s %d ",__func__,__LINE__); printf(fmt,##__VA_ARGS__); color(DEFAULT); }while(0)
#define info(fmt, ...) do{ color(CLASS_INFO); printf("%s %d ",__func__,__LINE__); printf(fmt,##__VA_ARGS__); color(DEFAULT); }while(0)
#ifdef __GNUC__
__attribute__((constructor))
void SetupColor(){
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
/* Save current attributes */
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
}
#endif //__GNUC__
#endif
#ifndef __DEBUG__
#undef print
#undef log
#undef warning
#undef info
#define print(fmt,...) while(0)
#define log(fmt,...) while(0)
#define warning(fmt,...) while(0)
#define info(fmt,...) while(0)
#endif //__DEBUG__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment