Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.0KB

  1. /*
  2. * Carla Log thread
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_LOG_THREAD_HPP_INCLUDED
  18. #define CARLA_LOG_THREAD_HPP_INCLUDED
  19. #include "CarlaBackend.hpp"
  20. #include "CarlaString.hpp"
  21. #include "CarlaThread.hpp"
  22. #include <fcntl.h>
  23. using CarlaBackend::CallbackFunc;
  24. // -----------------------------------------------------------------------
  25. // Log thread
  26. class CarlaLogThread : public CarlaThread
  27. {
  28. public:
  29. CarlaLogThread()
  30. : CarlaThread("CarlaLogThread"),
  31. fCallback(nullptr),
  32. fCallbackPtr(nullptr)
  33. {
  34. pipe(fPipe);
  35. fflush(stdout);
  36. fflush(stderr);
  37. //fPipe[1] = ::dup(STDOUT_FILENO);
  38. //fPipe[1] = ::dup(STDERR_FILENO);
  39. dup2(fPipe[1], STDOUT_FILENO);
  40. dup2(fPipe[1], STDERR_FILENO);
  41. fcntl(fPipe[0], F_SETFL, O_NONBLOCK);
  42. start(2);
  43. }
  44. ~CarlaLogThread()
  45. {
  46. fCallback = nullptr;
  47. fCallbackPtr = nullptr;
  48. stop(5000);
  49. fflush(stdout);
  50. fflush(stderr);
  51. close(fPipe[0]);
  52. close(fPipe[1]);
  53. }
  54. void setCallback(CallbackFunc callback, void* callbackPtr)
  55. {
  56. CARLA_ASSERT(callback != nullptr);
  57. fCallback = callback;
  58. fCallbackPtr = callbackPtr;
  59. }
  60. protected:
  61. void run()
  62. {
  63. while (! shouldExit())
  64. {
  65. size_t r, lastRead;
  66. ssize_t r2; // to avoid sign/unsign conversions
  67. static char bufTemp[1024+1] = { '\0' };
  68. static char bufRead[1024+1];
  69. static char bufSend[2048+1];
  70. while ((r2 = read(fPipe[0], bufRead, sizeof(char)*1024)) > 0)
  71. {
  72. r = static_cast<size_t>(r2);
  73. bufRead[r] = '\0';
  74. lastRead = 0;
  75. for (size_t i=0; i < r; ++i)
  76. {
  77. CARLA_ASSERT(bufRead[i] != '\0');
  78. if (bufRead[i] == '\n')
  79. {
  80. std::strcpy(bufSend, bufTemp);
  81. std::strncat(bufSend, bufRead+lastRead, i-lastRead);
  82. bufSend[std::strlen(bufTemp)+i-lastRead] = '\0';
  83. lastRead = i;
  84. bufTemp[0] = '\0';
  85. if (fCallback != nullptr)
  86. {
  87. if (fOldBuffer.isNotEmpty())
  88. {
  89. fCallback(fCallbackPtr, CarlaBackend::CALLBACK_DEBUG, 0, 0, 0, 0.0f, (const char*)fOldBuffer);
  90. fOldBuffer = nullptr;
  91. }
  92. fCallback(fCallbackPtr, CarlaBackend::CALLBACK_DEBUG, 0, 0, 0, 0.0f, bufSend);
  93. }
  94. else
  95. fOldBuffer += bufSend;
  96. }
  97. }
  98. CARLA_ASSERT(lastRead < r);
  99. if (lastRead > 0 && r > 0 && lastRead+1 < r)
  100. {
  101. std::strncpy(bufTemp, bufRead+lastRead, r-lastRead);
  102. bufTemp[r-lastRead] = '\0';
  103. }
  104. }
  105. carla_msleep(20);
  106. }
  107. }
  108. private:
  109. int fPipe[2];
  110. CallbackFunc fCallback;
  111. void* fCallbackPtr;
  112. CarlaString fOldBuffer;
  113. CARLA_PREVENT_HEAP_ALLOCATION
  114. CARLA_DECLARE_NON_COPY_CLASS(CarlaLogThread)
  115. };
  116. // -----------------------------------------------------------------------
  117. #endif // CARLA_LOG_THREAD_HPP_INCLUDED