1
0

obs-scripting-logging.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /******************************************************************************
  2. Copyright (C) 2017 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "obs-scripting-internal.h"
  15. #include <util/platform.h>
  16. static scripting_log_handler_t callback = NULL;
  17. static void *param = NULL;
  18. void script_log_va(obs_script_t *script, int level, const char *format,
  19. va_list args)
  20. {
  21. char msg[2048];
  22. const char *lang = "(Unknown)";
  23. size_t start_len;
  24. if (script) {
  25. switch (script->type) {
  26. case OBS_SCRIPT_LANG_UNKNOWN: lang = "(Unknown language)"; break;
  27. case OBS_SCRIPT_LANG_LUA: lang = "Lua"; break;
  28. case OBS_SCRIPT_LANG_PYTHON: lang = "Python"; break;
  29. }
  30. start_len = snprintf(msg, sizeof(msg), "[%s: %s] ",
  31. lang, script->file.array);
  32. } else {
  33. start_len = snprintf(msg, sizeof(msg), "[Unknown Script] ");
  34. }
  35. vsnprintf(msg + start_len, sizeof(msg) - start_len, format, args);
  36. if (callback)
  37. callback(param, script, level, msg + start_len);
  38. blog(level, "%s", msg);
  39. }
  40. void script_log(obs_script_t *script, int level, const char *format, ...)
  41. {
  42. va_list args;
  43. va_start(args, format);
  44. script_log_va(script, level, format, args);
  45. va_end(args);
  46. }
  47. void obs_scripting_set_log_callback(scripting_log_handler_t handler,
  48. void *log_param)
  49. {
  50. callback = handler;
  51. param = log_param;
  52. }