Skip to content

Commit 516cfa3

Browse files
committed
Read agent error messages
1 parent 9b6e5da commit 516cfa3

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

src/utils/logger.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ elog_internal(int elevel, bool file_only, const char *message)
162162
&& logger_config.log_directory[0] != '\0'
163163
&& !remote_agent;
164164
write_to_error_log = elevel >= ERROR && logger_config.error_log_filename &&
165-
logger_config.log_directory && logger_config.log_directory[0] != '\0';
166-
write_to_stderr = elevel >= (remote_agent ? ERROR : logger_config.log_level_console) && !file_only;
165+
logger_config.log_directory && logger_config.log_directory[0] != '\0'&& !remote_agent;
166+
write_to_stderr = (elevel >= (remote_agent ? ERROR : logger_config.log_level_console) && !file_only) || remote_agent;
167167

168168
pthread_lock(&log_file_mutex);
169169
loggin_in_progress = true;

src/utils/remote.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
#include <sys/types.h>
55
#include <sys/wait.h>
66
#include <signal.h>
7+
#include <pthread.h>
78

89
#include "pg_probackup.h"
910
#include "file.h"
1011

1112
#define MAX_CMDLINE_LENGTH 4096
1213
#define MAX_CMDLINE_OPTIONS 256
13-
#define ERR_BUF_SIZE 1024
14+
#define ERR_BUF_SIZE 4096
1415

1516
static int append_option(char* buf, size_t buf_size, size_t dst, char const* src)
1617
{
@@ -78,6 +79,37 @@ static void kill_child(void)
7879
kill(child_pid, SIGTERM);
7980
}
8081

82+
static void* error_reader_proc(void* arg)
83+
{
84+
int* errfd = (int*)arg;
85+
char buf[ERR_BUF_SIZE];
86+
int offs = 0, rc;
87+
88+
while ((rc = read(errfd[0], &buf[offs], sizeof(buf) - offs)) > 0)
89+
{
90+
char* nl;
91+
offs += rc;
92+
buf[offs] = '\0';
93+
nl = strchr(buf, '\n');
94+
if (nl != NULL) {
95+
*nl = '\0';
96+
if (strncmp(buf, "ERROR: ", 7) == 0) {
97+
elog(ERROR, "%s", buf + 7);
98+
} if (strncmp(buf, "WARNING: ", 9) == 0) {
99+
elog(WARNING, "%s", buf + 9);
100+
} else if (strncmp(buf, "LOG: ", 5) == 0) {
101+
elog(LOG, "%s", buf + 5);
102+
} else if (strncmp(buf, "INFO: ", 6) == 0) {
103+
elog(INFO, "%s", buf + 6);
104+
} else {
105+
elog(LOG, "%s", buf);
106+
}
107+
memmove(buf, nl+1, offs -= (nl + 1 - buf));
108+
}
109+
}
110+
return NULL;
111+
}
112+
81113
int remote_execute(int argc, char* argv[], bool listen)
82114
{
83115
char cmd[MAX_CMDLINE_LENGTH];
@@ -89,6 +121,7 @@ int remote_execute(int argc, char* argv[], bool listen)
89121
int infd[2];
90122
int errfd[2];
91123
char* pg_probackup = argv[0];
124+
pthread_t error_reader_thread;
92125

93126
ssh_argc = 0;
94127
ssh_argv[ssh_argc++] = instance_config.remote.proto;
@@ -178,18 +211,13 @@ int remote_execute(int argc, char* argv[], bool listen)
178211
SYS_CHECK(close(errfd[1]));
179212
atexit(kill_child);
180213

214+
pthread_create(&error_reader_thread, NULL, error_reader_proc, errfd);
215+
181216
if (listen) {
182217
int status;
183218
fio_communicate(infd[0], outfd[1]);
219+
184220
SYS_CHECK(wait(&status));
185-
if (status != 0)
186-
{
187-
char buf[ERR_BUF_SIZE];
188-
int offs, rc;
189-
for (offs = 0; (rc = read(errfd[0], &buf[offs], sizeof(buf) - offs)) > 0; offs += rc);
190-
buf[offs] = '\0';
191-
elog(ERROR, "%s", strncmp(buf, "ERROR: ", 6) == 0 ? buf + 6 : buf);
192-
}
193221
return status;
194222
} else {
195223
fio_redirect(infd[0], outfd[1]); /* write to stdout */

0 commit comments

Comments
 (0)