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.

146 lines
3.5KB

  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 "CarlaJuceUtils.hpp"
  21. #include <fcntl.h>
  22. #include <QtCore/QThread>
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------------------------------------
  25. // Log thread
  26. class LogThread : public QThread
  27. {
  28. public:
  29. LogThread()
  30. : fStop(false),
  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. }
  43. ~LogThread()
  44. {
  45. fflush(stdout);
  46. fflush(stderr);
  47. close(fPipe[0]);
  48. close(fPipe[1]);
  49. }
  50. void ready(CallbackFunc callback, void* callbackPtr)
  51. {
  52. CARLA_ASSERT(callback != nullptr);
  53. fCallback = callback;
  54. fCallbackPtr = callbackPtr;
  55. start();
  56. }
  57. void stop()
  58. {
  59. fStop = true;
  60. if (isRunning())
  61. wait();
  62. }
  63. protected:
  64. void run()
  65. {
  66. if (fCallback == nullptr)
  67. return;
  68. while (! fStop)
  69. {
  70. int i, r, lastRead;
  71. static char bufTemp[1024+1] = { '\0' };
  72. static char bufRead[1024+1];
  73. static char bufSend[2048+1];
  74. while ((r = read(fPipe[0], bufRead, sizeof(char)*1024)) > 0)
  75. {
  76. bufRead[r] = '\0';
  77. lastRead = 0;
  78. for (i=0; i < r; ++i)
  79. {
  80. CARLA_ASSERT(bufRead[i] != '\0');
  81. if (bufRead[i] == '\n')
  82. {
  83. std::strcpy(bufSend, bufTemp);
  84. std::strncat(bufSend, bufRead+lastRead, i-lastRead);
  85. bufSend[std::strlen(bufTemp)+i-lastRead] = '\0';
  86. lastRead = i;
  87. bufTemp[0] = '\0';
  88. fCallback(fCallbackPtr, CarlaBackend::CALLBACK_DEBUG, 0, 0, 0, 0.0f, bufSend);
  89. }
  90. }
  91. CARLA_ASSERT(i == r);
  92. CARLA_ASSERT(lastRead < r);
  93. if (lastRead > 0 && lastRead < r-1)
  94. {
  95. std::strncpy(bufTemp, bufRead+lastRead, r-lastRead);
  96. bufTemp[r-lastRead] = '\0';
  97. }
  98. }
  99. carla_msleep(20);
  100. }
  101. }
  102. private:
  103. int fPipe[2];
  104. bool fStop;
  105. CallbackFunc fCallback;
  106. void* fCallbackPtr;
  107. CARLA_PREVENT_HEAP_ALLOCATION
  108. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LogThread)
  109. };
  110. // -----------------------------------------------------------------------
  111. CARLA_BACKEND_END_NAMESPACE
  112. #endif // CARLA_LOG_THREAD_HPP_INCLUDED