Pull logging prefix from program name from argv[0]
[guacd.git] / src / daemon.c
index dd4ab8a..aff36e0 100644 (file)
@@ -48,6 +48,7 @@
 
 #include <errno.h>
 #include <syslog.h>
+#include <libgen.h>
 
 #include <guacamole/client.h>
 #include <guacamole/error.h>
@@ -227,9 +228,13 @@ int main(int argc, char* argv[]) {
         }
     }
 
+    /* Set up logging prefix */
+    strncpy(log_prefix, basename(argv[0]), sizeof(log_prefix));
+
+
     /* Get addresses for binding */
     if ((retval = getaddrinfo(listen_address, listen_port, &hints, &addresses))) {
-        guacd_log_error("Error parsing given address or port: %s\n",
+        guacd_log_error("Error parsing given address or port: %s",
                 gai_strerror(retval));
         exit(EXIT_FAILURE);
     }
@@ -237,13 +242,13 @@ int main(int argc, char* argv[]) {
     /* Get socket */
     socket_fd = socket(AF_INET, SOCK_STREAM, 0);
     if (socket_fd < 0) {
-        guacd_log_error("Error opening socket: %s\n", strerror(errno));
+        guacd_log_error("Error opening socket: %s", strerror(errno));
         exit(EXIT_FAILURE);
     }
 
     /* Allow socket reuse */
     if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (void*) &opt_on, sizeof(opt_on))) {
-        guacd_log_info("Unable to set socket options for reuse: %s\n", strerror(errno));
+        guacd_log_info("Unable to set socket options for reuse: %s", strerror(errno));
     }
 
     /* Attempt binding of each address until success */
@@ -258,7 +263,7 @@ int main(int argc, char* argv[]) {
                 bound_address, sizeof(bound_address),
                 bound_port, sizeof(bound_port),
                 NI_NUMERICHOST | NI_NUMERICSERV)))
-            guacd_log_error("Unable to resolve host: %s\n",
+            guacd_log_error("Unable to resolve host: %s",
                     gai_strerror(retval));
 
         /* Attempt to bind socket to address */
@@ -266,18 +271,18 @@ int main(int argc, char* argv[]) {
                     current_address->ai_addr,
                     current_address->ai_addrlen) == 0) {
 
-            guacd_log_error("Successfully bound socket to "
-                    "host %s, port %s\n", bound_address, bound_port);
+            guacd_log_info("Successfully bound socket to "
+                    "host %s, port %s", bound_address, bound_port);
 
             /* Done if successful bind */
             break;
 
         }
 
-        /* Otherwise log error */
+        /* Otherwise log information regarding bind failure */
         else
-            guacd_log_error("Error binding socket to "
-                    "host %s, port %s: %s\n",
+            guacd_log_info("Unable to bind socket to "
+                    "host %s, port %s: %s",
                     bound_address, bound_port, strerror(errno));
 
         current_address = current_address->ai_next;
@@ -286,7 +291,7 @@ int main(int argc, char* argv[]) {
 
     /* If unable to bind to anything, fail */
     if (current_address == NULL) {
-        guacd_log_error("Unable to bind socket to any addresses.\n");
+        guacd_log_error("Unable to bind socket to any addresses.");
         exit(EXIT_FAILURE);
     }
 
@@ -295,7 +300,7 @@ int main(int argc, char* argv[]) {
 
     /* If error, fail */
     if (daemon_pid == -1) {
-        guacd_log_error("Error forking daemon process: %s\n", strerror(errno));
+        guacd_log_error("Error forking daemon process: %s", strerror(errno));
         exit(EXIT_FAILURE);
     }
 
@@ -313,7 +318,7 @@ int main(int argc, char* argv[]) {
 
             /* Warn on failure */
             else {
-                guacd_log_error("Could not write PID file: %s\n", strerror(errno));
+                guacd_log_error("Could not write PID file: %s", strerror(errno));
                 exit(EXIT_FAILURE);
             }
 
@@ -327,12 +332,12 @@ int main(int argc, char* argv[]) {
 
     /* Ignore SIGPIPE */
     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
-        guacd_log_error("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
+        guacd_log_info("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
     }
 
     /* Ignore SIGCHLD (force automatic removal of children) */
     if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
-        guacd_log_error("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
+        guacd_log_info("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
     }
 
     /* Log listening status */
@@ -374,7 +379,7 @@ int main(int argc, char* argv[]) {
 
         /* If error, log */
         if (child_pid == -1)
-            guacd_log_error("Error forking child process: %s\n", strerror(errno));
+            guacd_log_error("Error forking child process: %s", strerror(errno));
 
         /* If child, start client, and exit when finished */
         else if (child_pid == 0) {