Skip to content

Commit

Permalink
server: refactor: output options
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Feb 21, 2022
1 parent 424d51f commit 4917dc9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/rpcserver/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

bool g_stdout = false;
bool g_syslog = false;
FILE *g_file = NULL;

void trace(const char *prefix, const char *fmt, ...)
{
Expand All @@ -31,11 +32,17 @@ void trace(const char *prefix, const char *fmt, ...)
if (g_stdout)
{
puts(prefixed_line);
fflush(stdout);
}
if (g_syslog)
{
syslog(LOG_DEBUG, "%s", prefixed_line);
}
if (g_file)
{
fprintf(g_file, "%s\n", prefixed_line);
fflush(g_file);
}
}

bool recvall(int sockfd, char *buf, size_t len)
Expand Down
1 change: 1 addition & 0 deletions src/rpcserver/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef unsigned long s64;

bool g_stdout;
bool g_syslog;
FILE *g_file;

#define TRACE(...) trace(__PRETTY_FUNCTION__, __VA_ARGS__)
#define CHECK(expression) \
Expand Down
38 changes: 26 additions & 12 deletions src/rpcserver/rpcserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@

#define DEFAULT_PORT ("5910")
#define DEFAULT_SHELL ("/bin/sh")
#define USAGE ("Usage: %s [-p port] [-s] [-S] \n\
#define USAGE ("Usage: %s [-p port] [-o (stdout|syslog|file:filename)] \n\
-h show this help message \n\
-s log to stdout \n\
-S log to syslog \n")
-o output. can be all of the following: stdout, syslog and file:filename. can be passed multiple times \n\
\n\
Example usage: \n\
%s -p 5910 -o syslog -o stdout -o file:/tmp/log.txt\n")
#define SERVER_MAGIC_VERSION (0x88888800)
#define MAGIC (0x12345678)
#define MAX_CONNECTIONS (1024)
Expand Down Expand Up @@ -695,7 +697,7 @@ int main(int argc, const char *argv[])
int opt;
char port[MAX_OPTION_LEN] = DEFAULT_PORT;

while ((opt = getopt(argc, (char *const *)argv, "p:sSh")) != -1)
while ((opt = getopt(argc, (char *const *)argv, "hp:o:")) != -1)
{
switch (opt)
{
Expand All @@ -704,20 +706,32 @@ int main(int argc, const char *argv[])
strncpy(port, optarg, sizeof(port) - 1);
break;
}
case 's':
case 'o':
{
g_stdout = true;
break;
}
case 'S':
{
g_syslog = true;
if (0 == strcmp(optarg, "stdout"))
{
g_stdout = true;
}
if (0 == strcmp(optarg, "syslog"))
{
g_syslog = true;
}
char *file = strstr(optarg, "file:");
if (file)
{
g_file = fopen(file + 5, "wb");
if (!g_file)
{
printf("failed to open %s for writing\n", optarg);
}
}
break;
}
case 'h':
case '?':
default: /* '?' */
{
printf(USAGE, argv[0]);
printf(USAGE, argv[0], argv[0]);
exit(EXIT_FAILURE);
}
}
Expand Down

0 comments on commit 4917dc9

Please sign in to comment.