diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-28 15:42:34 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-28 15:42:34 -0200 |
| commit | 437a5b07d415e1a74160ddfd804017171d6cc5cb (patch) | |
| tree | 861f9a56ae175eaed91c163409c33ab85bee7ff9 /lauxlib.c | |
| parent | ba7da13ec5938f978c37d63aa40a3e340b301f79 (diff) | |
| download | lua-437a5b07d415e1a74160ddfd804017171d6cc5cb.tar.gz lua-437a5b07d415e1a74160ddfd804017171d6cc5cb.tar.bz2 lua-437a5b07d415e1a74160ddfd804017171d6cc5cb.zip | |
Added a warning system to Lua
The warning system is just a way for Lua to emit warnings, messages
to the programmer that do not interfere with the running program.
Diffstat (limited to 'lauxlib.c')
| -rw-r--r-- | lauxlib.c | 28 |
1 files changed, 27 insertions, 1 deletions
| @@ -986,9 +986,35 @@ static int panic (lua_State *L) { | |||
| 986 | } | 986 | } |
| 987 | 987 | ||
| 988 | 988 | ||
| 989 | /* | ||
| 990 | ** checks whether 'message' ends with end-of-line | ||
| 991 | ** (and therefore is the last part of a warning) | ||
| 992 | */ | ||
| 993 | static int islast (const char *message) { | ||
| 994 | size_t len = strlen(message); | ||
| 995 | return (len > 0 && message[len - 1] == '\n'); | ||
| 996 | } | ||
| 997 | |||
| 998 | |||
| 999 | /* | ||
| 1000 | ** Emit a warning. If '*pud' is NULL, previous message was to be | ||
| 1001 | ** continued by the current one. | ||
| 1002 | */ | ||
| 1003 | static void warnf (void **pud, const char *message) { | ||
| 1004 | if (*pud == NULL) /* previous message was not the last? */ | ||
| 1005 | lua_writestringerror("%s", message); | ||
| 1006 | else /* start a new warning */ | ||
| 1007 | lua_writestringerror("Lua warning: %s", message); | ||
| 1008 | *pud = (islast(message)) ? pud : NULL; | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | |||
| 989 | LUALIB_API lua_State *luaL_newstate (void) { | 1012 | LUALIB_API lua_State *luaL_newstate (void) { |
| 990 | lua_State *L = lua_newstate(l_alloc, NULL); | 1013 | lua_State *L = lua_newstate(l_alloc, NULL); |
| 991 | if (L) lua_atpanic(L, &panic); | 1014 | if (L) { |
| 1015 | lua_atpanic(L, &panic); | ||
| 1016 | lua_setwarnf(L, warnf, L); | ||
| 1017 | } | ||
| 992 | return L; | 1018 | return L; |
| 993 | } | 1019 | } |
| 994 | 1020 | ||
