obs-scripting-logging.c 2.0 KB

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