2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file process_pipe.cpp
18 * @author Tomasz Iwanek (t.iwanek@samsung.com)
20 * @brief This file is the implementation pipe from process
23 #include<dpl/test/process_pipe.h>
24 #include<dpl/log/wrt_log.h>
28 ProcessPipe::ProcessPipe(PipeErrorPolicy err) : m_file(NULL), m_errPolicy(err)
32 ProcessPipe::~ProcessPipe()
36 void ProcessPipe::Open(const std::string & command)
40 ThrowMsg(Exception::DoubleOpen, "Trying to open pipe second time. Close it first");
43 std::string stdErrRedirection;
46 case PipeErrorPolicy::NONE: break;
47 case PipeErrorPolicy::OFF: stdErrRedirection = " 2>/dev/null"; break;
48 case PipeErrorPolicy::PIPE: stdErrRedirection = " 2>&1"; break;
52 std::string fcommand = command + stdErrRedirection;
53 FILE * file = popen(fcommand.c_str(), "r");
55 // Throw an exception if an error occurred
57 ThrowMsg(FileInput::Exception::OpenFailed, fcommand);
60 // Save new descriptor
62 m_fd = fileno(m_file);
64 WrtLogD("Opened pipe: %s", fcommand.c_str());
67 void ProcessPipe::Close()
73 if (pclose(m_file) == -1) {
74 Throw(FileInput::Exception::CloseFailed);
80 WrtLogD("Closed pipe");