@@ -110,10 +110,16 @@ bool generate_plan(const std::string& query, const std::string& statistics,
110110
111111 // call compiler to generate plan
112112 {
113+ int pipefd[2 ];
114+ if (pipe (pipefd) == -1 ) {
115+ LOG (ERROR) << " pipe failed!" << strerror (errno);
116+ exit (EXIT_FAILURE);
117+ }
118+
113119 pid_t pid = fork ();
114120
115121 if (pid == -1 ) {
116- std::cerr << " Fork failed!" << std::endl ;
122+ LOG (ERROR) << " fork failed!" << strerror (errno) ;
117123 return false ;
118124 } else if (pid == 0 ) {
119125 const char * const args[] = {
@@ -127,22 +133,45 @@ bool generate_plan(const std::string& query, const std::string& statistics,
127133 " temp.cypher.yaml" ,
128134 nullptr // execvp expects a null-terminated array
129135 };
136+
137+ close (pipefd[0 ]);
138+
139+ if (dup2 (pipefd[1 ], STDERR_FILENO) == -1 ) {
140+ LOG (ERROR) << " dup2 failed!" << strerror (errno);
141+ exit (EXIT_FAILURE);
142+ }
143+
144+ close (pipefd[1 ]);
145+
130146 execvp (args[0 ], const_cast <char * const *>(args));
131147
132148 std::cerr << " Exec failed!" << std::endl;
133149 return false ;
134150 } else {
151+ close (pipefd[1 ]);
152+
153+ ssize_t count;
154+ constexpr size_t BUFFSIZ = 4096 ;
155+ char buffer[BUFFSIZ];
156+ std::string error_message;
157+ while ((count = read (pipefd[0 ], buffer, sizeof (buffer) - 1 )) > 0 ) {
158+ buffer[count] = ' \0 ' ;
159+ error_message += buffer;
160+ }
161+
135162 int status;
136163 waitpid (pid, &status, 0 );
137164 if (WIFEXITED (status)) {
138165 VLOG (1 ) << " Child exited with status " << WEXITSTATUS (status)
139166 << std::endl;
140167 }
168+ close (pipefd[0 ]);
141169
142170 {
143171 std::ifstream file (output_file, std::ios::binary);
144172
145173 if (!file.is_open ()) {
174+ LOG (ERROR) << " Compiler message: " << error_message;
146175 return false ;
147176 }
148177
@@ -157,6 +186,7 @@ bool generate_plan(const std::string& query, const std::string& statistics,
157186
158187 file.close ();
159188 if (!plan.ParseFromString (std::string (buffer))) {
189+ LOG (ERROR) << " Compiler message: " << error_message;
160190 return false ;
161191 }
162192 }
0 commit comments