obs-scripting-logging.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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, va_list args)
  19. {
  20. char msg[2048];
  21. const char *lang = "(Unknown)";
  22. size_t start_len;
  23. if (script) {
  24. switch (script->type) {
  25. case OBS_SCRIPT_LANG_UNKNOWN:
  26. lang = "(Unknown language)";
  27. break;
  28. case OBS_SCRIPT_LANG_LUA:
  29. lang = "Lua";
  30. break;
  31. case OBS_SCRIPT_LANG_PYTHON:
  32. lang = "Python";
  33. break;
  34. }
  35. start_len = snprintf(msg, sizeof(msg), "[%s: %s] ", lang, script->file.array);
  36. } else {
  37. start_len = snprintf(msg, sizeof(msg), "[Unknown Script] ");
  38. }
  39. vsnprintf(msg + start_len, sizeof(msg) - start_len, format, args);
  40. if (callback)
  41. callback(param, script, level, msg + start_len);
  42. blog(level, "%s", msg);
  43. }
  44. void script_log(obs_script_t *script, int level, const char *format, ...)
  45. {
  46. va_list args;
  47. va_start(args, format);
  48. script_log_va(script, level, format, args);
  49. va_end(args);
  50. }
  51. void obs_scripting_set_log_callback(scripting_log_handler_t handler, void *log_param)
  52. {
  53. callback = handler;
  54. param = log_param;
  55. }