c - Porting Zed Shaw's debug macros to MSVC -


i've found zed shaw's debug macros on website when c book "learn c hard way" free read. designed linux initially, , works pretty well, been using while.

now, i've started write c code windows , i'm using visual studio. want use these debug macros in project i'm working on.

the problem follows: let's want use check macro make sure function returned without error.

display = al_create_display(640, 480); check(display, "failed create display"); 

the definition macros used following:

#define clean_errno() (errno == 0 ? "none" : strerror(errno)) #define log_err(m, ...) fprintf(stderr, "[error] (%s:%d: errno: %s) " m "\n", __file__, __line__, clean_errno(), ##__va_args__) #define check(a, m, ...) if(!(a)) { log_err(m, ##__va_args__); errno=0; goto error; } 

however, problem encountered visual studio marks strerror() deprecated , aborts compilation. plan on using threads in project, don't want go "ignore" route, if possible.

what did created clean_errno() function same thing macro, uses global buffer variable , calls strerror_s() copy it, return pointer log_err() macro. have to:

  • either create brand new header , c file 1 3-liner function, think abundant
  • or declare , implement function in debug macro's header, isn't considered c practice @ all, , ugly.

is there other trick/hack don't know of provide elegant , simple solution this?

strerror deprecated because it's not thread safe. if don't care fact can find option in vs turn off warning.

but equally simple implement thread-safe version using sterror_s(). remember, macros can more evaluate expressions, it's possible allocate buffer in macro:

#define check(a, m, ...) \     if(!(a)) { \         char _buffer[128]; \         strerror_s(_buffer, 128, errno); \         fprintf(stderr, "[error] (%s:%d: errno: %s) " (m) "\n", __file__, \                 __line__, errno? _buffer : "none", ##__va_args__); \         errno = 0; \         goto error; \     } 

i haven't tested code may have bugs, should start.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -