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.

160 lines
4.1KB

  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 <fcntl.h>
  22. #include <QtCore/QThread>
  23. using CarlaBackend::CallbackFunc;
  24. // -----------------------------------------------------------------------
  25. // Log thread
  26. class CarlaLogThread : public QThread
  27. {
  28. public:
  29. CarlaLogThread()
  30. : QThread(nullptr),
  31. fStop(false),
  32. fCallback(nullptr),
  33. fCallbackPtr(nullptr)
  34. {
  35. pipe(fPipe);
  36. fflush(stdout);
  37. fflush(stderr);
  38. //fPipe[1] = ::dup(STDOUT_FILENO);
  39. //fPipe[1] = ::dup(STDERR_FILENO);
  40. dup2(fPipe[1], STDOUT_FILENO);
  41. dup2(fPipe[1], STDERR_FILENO);
  42. fcntl(fPipe[0], F_SETFL, O_NONBLOCK);
  43. QThread::start(LowPriority);
  44. }
  45. ~CarlaLogThread()
  46. {
  47. fCallback = nullptr;
  48. fCallbackPtr = nullptr;
  49. stop();
  50. fflush(stdout);
  51. fflush(stderr);
  52. close(fPipe[0]);
  53. close(fPipe[1]);
  54. }
  55. void setCallback(CallbackFunc callback, void* callbackPtr)
  56. {
  57. CARLA_ASSERT(callback != nullptr);
  58. fCallback = callback;
  59. fCallbackPtr = callbackPtr;
  60. }
  61. void stop()
  62. {
  63. fStop = true;
  64. if (isRunning())
  65. wait();
  66. }
  67. protected:
  68. void run()
  69. {
  70. while (! fStop)
  71. {
  72. size_t r, lastRead;
  73. ssize_t r2; // to avoid sign/unsign conversions
  74. static char bufTemp[1024+1] = { '\0' };
  75. static char bufRead[1024+1];
  76. static char bufSend[2048+1];
  77. while ((r2 = read(fPipe[0], bufRead, sizeof(char)*1024)) > 0)
  78. {
  79. r = static_cast<size_t>(r2);
  80. bufRead[r] = '\0';
  81. lastRead = 0;
  82. for (size_t i=0; i < r; ++i)
  83. {
  84. CARLA_ASSERT(bufRead[i] != '\0');
  85. if (bufRead[i] == '\n')
  86. {
  87. std::strcpy(bufSend, bufTemp);
  88. std::strncat(bufSend, bufRead+lastRead, i-lastRead);
  89. bufSend[std::strlen(bufTemp)+i-lastRead] = '\0';
  90. lastRead = i;
  91. bufTemp[0] = '\0';
  92. if (fCallback != nullptr)
  93. {
  94. if (fOldBuffer.isNotEmpty())
  95. {
  96. fCallback(fCallbackPtr, CarlaBackend::CALLBACK_DEBUG, 0, 0, 0, 0.0f, (const char*)fOldBuffer);
  97. fOldBuffer = nullptr;
  98. }
  99. fCallback(fCallbackPtr, CarlaBackend::CALLBACK_DEBUG, 0, 0, 0, 0.0f, bufSend);
  100. }
  101. else
  102. fOldBuffer += bufSend;
  103. }
  104. }
  105. CARLA_ASSERT(lastRead < r);
  106. if (lastRead > 0 && r > 0 && lastRead+1 < r)
  107. {
  108. std::strncpy(bufTemp, bufRead+lastRead, r-lastRead);
  109. bufTemp[r-lastRead] = '\0';
  110. }
  111. }
  112. carla_msleep(20);
  113. }
  114. }
  115. private:
  116. int fPipe[2];
  117. bool fStop;
  118. CallbackFunc fCallback;
  119. void* fCallbackPtr;
  120. CarlaString fOldBuffer;
  121. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaLogThread)
  122. };
  123. // -----------------------------------------------------------------------
  124. #endif // CARLA_LOG_THREAD_HPP_INCLUDED