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.

152 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 "juce_core.h"
  22. #include <fcntl.h>
  23. using CarlaBackend::CallbackFunc;
  24. using juce::Thread;
  25. // -----------------------------------------------------------------------
  26. // Log thread
  27. class CarlaLogThread : public Thread
  28. {
  29. public:
  30. CarlaLogThread()
  31. : Thread("CarlaLogThread"),
  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. startThread(2);
  44. }
  45. ~CarlaLogThread()
  46. {
  47. fCallback = nullptr;
  48. fCallbackPtr = nullptr;
  49. stopThread(5000);
  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. protected:
  62. void run()
  63. {
  64. while (! threadShouldExit())
  65. {
  66. size_t r, lastRead;
  67. ssize_t r2; // to avoid sign/unsign conversions
  68. static char bufTemp[1024+1] = { '\0' };
  69. static char bufRead[1024+1];
  70. static char bufSend[2048+1];
  71. while ((r2 = read(fPipe[0], bufRead, sizeof(char)*1024)) > 0)
  72. {
  73. r = static_cast<size_t>(r2);
  74. bufRead[r] = '\0';
  75. lastRead = 0;
  76. for (size_t i=0; i < r; ++i)
  77. {
  78. CARLA_ASSERT(bufRead[i] != '\0');
  79. if (bufRead[i] == '\n')
  80. {
  81. std::strcpy(bufSend, bufTemp);
  82. std::strncat(bufSend, bufRead+lastRead, i-lastRead);
  83. bufSend[std::strlen(bufTemp)+i-lastRead] = '\0';
  84. lastRead = i;
  85. bufTemp[0] = '\0';
  86. if (fCallback != nullptr)
  87. {
  88. if (fOldBuffer.isNotEmpty())
  89. {
  90. fCallback(fCallbackPtr, CarlaBackend::CALLBACK_DEBUG, 0, 0, 0, 0.0f, (const char*)fOldBuffer);
  91. fOldBuffer = nullptr;
  92. }
  93. fCallback(fCallbackPtr, CarlaBackend::CALLBACK_DEBUG, 0, 0, 0, 0.0f, bufSend);
  94. }
  95. else
  96. fOldBuffer += bufSend;
  97. }
  98. }
  99. CARLA_ASSERT(lastRead < r);
  100. if (lastRead > 0 && r > 0 && lastRead+1 < r)
  101. {
  102. std::strncpy(bufTemp, bufRead+lastRead, r-lastRead);
  103. bufTemp[r-lastRead] = '\0';
  104. }
  105. }
  106. carla_msleep(20);
  107. }
  108. }
  109. private:
  110. int fPipe[2];
  111. CallbackFunc fCallback;
  112. void* fCallbackPtr;
  113. CarlaString fOldBuffer;
  114. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaLogThread)
  115. };
  116. // -----------------------------------------------------------------------
  117. #endif // CARLA_LOG_THREAD_HPP_INCLUDED