LCOV - code coverage report
Current view: top level - media/server/service/source - ConsoleLogForwarding.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 71 0
Test Date: 2026-11-10 16:58:19 Functions: 0.0 % 5 0

            Line data    Source code
       1              : /*
       2              :  * If not stated otherwise in this file or this component's LICENSE file the
       3              :  * following copyright and licenses apply:
       4              :  *
       5              :  * Copyright 2026 Sky UK
       6              :  *
       7              :  * Licensed under the Apache License, Version 2.0 (the "License");
       8              :  * you may not use this file except in compliance with the License.
       9              :  * You may obtain a copy of the License at
      10              :  *
      11              :  * http://www.apache.org/licenses/LICENSE-2.0
      12              :  *
      13              :  * Unless required by applicable law or agreed to in writing, software
      14              :  * distributed under the License is distributed on an "AS IS" BASIS,
      15              :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      16              :  * See the License for the specific language governing permissions and
      17              :  * limitations under the License.
      18              :  */
      19              : 
      20              : #include "ConsoleLogForwarding.h"
      21              : #include "RialtoLogging.h"
      22              : 
      23              : namespace firebolt::rialto::server::service
      24              : {
      25            0 : ConsoleLogForwarding::ConsoleLogForwarding()
      26              : {
      27            0 :     if (logging::isConsoleLoggingEnabled())
      28              :     {
      29              :         // Console logging is enabled, no need to forward logs.
      30            0 :         return;
      31              :     }
      32            0 :     start();
      33              : }
      34              : 
      35            0 : ConsoleLogForwarding::~ConsoleLogForwarding()
      36              : {
      37            0 :     stop();
      38              : }
      39              : 
      40            0 : bool ConsoleLogForwarding::start()
      41              : {
      42            0 :     if (pipe(m_stdoutPipe) == -1)
      43            0 :         return false;
      44              : 
      45            0 :     if (pipe(m_stderrPipe) == -1)
      46              :     {
      47            0 :         close(m_stdoutPipe[0]);
      48            0 :         close(m_stdoutPipe[1]);
      49            0 :         return false;
      50              :     }
      51              : 
      52            0 :     m_stdoutFd = dup(STDOUT_FILENO);
      53            0 :     m_stdderrFd = dup(STDERR_FILENO);
      54              : 
      55            0 :     if (m_stdoutFd == -1 || m_stdderrFd == -1)
      56              :     {
      57            0 :         stop();
      58            0 :         return false;
      59              :     }
      60              : 
      61            0 :     if (dup2(m_stdoutPipe[1], STDOUT_FILENO) == -1)
      62            0 :         return false;
      63              : 
      64            0 :     if (dup2(m_stderrPipe[1], STDERR_FILENO) == -1)
      65            0 :         return false;
      66              : 
      67            0 :     m_running = true;
      68            0 :     m_stdoutThread = std::thread(&ConsoleLogForwarding::readLoop, this, m_stdoutPipe[0]);
      69              : 
      70            0 :     m_stderrThread = std::thread(&ConsoleLogForwarding::readLoop, this, m_stderrPipe[0]);
      71              : 
      72            0 :     return true;
      73              : }
      74              : 
      75            0 : void ConsoleLogForwarding::stop()
      76              : {
      77            0 :     if (!m_running)
      78            0 :         return;
      79              : 
      80            0 :     m_running = false;
      81              : 
      82            0 :     if (m_stdoutFd != -1)
      83              :     {
      84            0 :         dup2(m_stdoutFd, STDOUT_FILENO);
      85            0 :         close(m_stdoutFd);
      86            0 :         m_stdoutFd = -1;
      87              :     }
      88              : 
      89            0 :     if (m_stdderrFd != -1)
      90              :     {
      91            0 :         dup2(m_stdderrFd, STDERR_FILENO);
      92            0 :         close(m_stdderrFd);
      93            0 :         m_stdderrFd = -1;
      94              :     }
      95              : 
      96            0 :     if (m_stdoutPipe[1] != -1)
      97              :     {
      98            0 :         close(m_stdoutPipe[1]);
      99            0 :         m_stdoutPipe[1] = -1;
     100              :     }
     101              : 
     102            0 :     if (m_stderrPipe[1] != -1)
     103              :     {
     104            0 :         close(m_stderrPipe[1]);
     105            0 :         m_stderrPipe[1] = -1;
     106              :     }
     107              : 
     108            0 :     if (m_stdoutThread.joinable())
     109            0 :         m_stdoutThread.join();
     110              : 
     111            0 :     if (m_stderrThread.joinable())
     112            0 :         m_stderrThread.join();
     113              : 
     114            0 :     if (m_stdoutPipe[0] != -1)
     115              :     {
     116            0 :         close(m_stdoutPipe[0]);
     117            0 :         m_stdoutPipe[0] = -1;
     118              :     }
     119              : 
     120            0 :     if (m_stderrPipe[0] != -1)
     121              :     {
     122            0 :         close(m_stderrPipe[0]);
     123            0 :         m_stderrPipe[0] = -1;
     124              :     }
     125              : }
     126              : 
     127            0 : void ConsoleLogForwarding::readLoop(int fd)
     128              : {
     129            0 :     std::string buffer;
     130              :     char temp[4096];
     131              : 
     132            0 :     while (m_running)
     133              :     {
     134            0 :         ssize_t n = read(fd, temp, sizeof(temp));
     135              : 
     136            0 :         if (n == 0)
     137              :         {
     138            0 :             break;
     139              :         }
     140              : 
     141            0 :         if (n < 0)
     142              :         {
     143            0 :             if (errno == EINTR)
     144            0 :                 continue;
     145            0 :             break;
     146              :         }
     147              : 
     148            0 :         buffer.append(temp, n);
     149              : 
     150              :         size_t pos;
     151            0 :         while ((pos = buffer.find('\n')) != std::string::npos)
     152              :         {
     153            0 :             std::string line = buffer.substr(0, pos);
     154            0 :             buffer.erase(0, pos + 1);
     155              : 
     156            0 :             RIALTO_LOG_EXTERNAL("%s", line.c_str());
     157              :         }
     158              :     }
     159              : 
     160              :     // Flush a final partial line.
     161            0 :     if (!buffer.empty())
     162              :     {
     163            0 :         RIALTO_LOG_EXTERNAL("%s", buffer.c_str());
     164              :     }
     165              : }
     166              : } // namespace firebolt::rialto::server::service
        

Generated by: LCOV version 2.0-1