Skip to content

Commit fc81f4b

Browse files
committed
Add file and line number to THError.
1 parent a046a6b commit fc81f4b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

THGeneral.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ void THError(const char *fmt, ...)
2121
/* vasprintf not standard */
2222
/* vsnprintf: how to handle if does not exists? */
2323
va_start(args, fmt);
24-
vsnprintf(msg, 1024, fmt, args);
24+
int n = vsnprintf(msg, 1024, fmt, args);
2525
va_end(args);
2626

27+
if(n < 1024) {
28+
snprintf(msg + n, 1024 - n, "\nat %s:%d", file, line);
29+
}
2730
(*torchErrorHandlerFunction)(msg, torchErrorHandlerData);
2831
}
2932

THGeneral.h.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@
4444
#endif
4545

4646
TH_API double THLog1p(const double x);
47-
TH_API void THError(const char *fmt, ...);
47+
TH_API void _THError(const char *file, const int line, const char *fmt, ...);
4848
TH_API void THSetErrorHandler( void (*torchErrorHandlerFunction)(const char *msg, void *data), void *data );
4949
TH_API void THArgCheck(int condition, int argNumber, const char *msg);
5050
TH_API void THSetArgErrorHandler( void (*torchArgErrorHandlerFunction)(int argNumber, const char *msg, void *data), void *data );
5151
TH_API void* THAlloc(long size);
5252
TH_API void* THRealloc(void *ptr, long size);
5353
TH_API void THFree(void *ptr);
5454

55+
#define THError(...) _THError(__FILE__, __LINE__, __VA_ARGS__)
56+
5557
#define TH_CONCAT_STRING_2(x,y) TH_CONCAT_STRING_2_EXPAND(x,y)
5658
#define TH_CONCAT_STRING_2_EXPAND(x,y) #x #y
5759

0 commit comments

Comments
 (0)