Log status messages to stderr instead of stdout

There was a buffer on stdout that prevented messages from
getting logged until it was full. This buffer was disabled
to prevent any other printfs from being delayed.

Change-Id: I9cc421b00678833e0b7f9459ab31377ab6314e9e
diff --git a/src/server/main.c b/src/server/main.c
index 60a098e..32fd628 100644
--- a/src/server/main.c
+++ b/src/server/main.c
@@ -178,7 +178,7 @@
 
     closedir(proc_fd);
   } else {
-    printf("/proc failed to open\n");
+    fprintf(stderr, "/proc failed to open\n");
   }
   return 0;
 }
@@ -242,7 +242,7 @@
 static DIALStatus youtube_start(DIALServer *ds, const char *appname,
                                 const char *args, size_t arglen,
                                 DIAL_run_t *run_id, void *callback_data) {
-    printf("** LAUNCH YouTube **\n");
+    fprintf(stderr, "** LAUNCH YouTube **\n");
 
     // TODO(steineldar): Verify that the args string is a valid URL query param.
     for (int n = 0; args[n] != 0; ++n) {
@@ -251,7 +251,7 @@
             ( 'a' <= c && c <= 'z' ) ||
             ( 'A' <= c && c <= 'Z' ) ||
             ( c == '=' || c == '-' || c == '_' || c == '&' ))) {
-        printf("Invalid char [%d] = %c", (int) c, c);
+        fprintf(stderr, "Invalid char [%d] = %c", (int) c, c);
         args = "";
         break;
       }
@@ -280,7 +280,7 @@
 
 static void youtube_stop(DIALServer *ds, const char *appname, DIAL_run_t run_id,
                          void *callback_data) {
-    printf("** KILL YouTube **\n");
+    fprintf(stderr, "** KILL YouTube **\n");
     pid_t pid;
     if ((pid = isAppRunning( spAppYouTube, spAppYouTubeMatch ))) {
         kill(pid, SIGTERM);
@@ -314,7 +314,7 @@
         }
     }
 
-    printf("appPid = %s, shouldRelaunch = %s queryParams = %s\n",
+    fprintf(stderr, "appPid = %s, shouldRelaunch = %s queryParams = %s\n",
           appPid?"TRUE":"FALSE", 
           shouldRelaunchApp?"TRUE":"FALSE",
           sQueryParam );
@@ -344,7 +344,7 @@
     pid = isAppRunning( spAppNetflix, NULL );
     if( pid )
     {
-        printf("Killing pid %d\n", pid);
+        fprintf(stderr, "Killing pid %d\n", pid);
         kill((pid_t)pid, SIGTERM);
         waitpid((pid_t)pid, NULL, 0); // reap child
     }
@@ -353,7 +353,7 @@
 static DIALStatus fibertv_start(DIALServer *ds, const char *appname,
                                 const char *args, size_t arglen,
                                 DIAL_run_t *run_id, void *callback_data) {
-    printf("** LAUNCH FiberTV **\n");
+    fprintf(stderr, "** LAUNCH FiberTV **\n");
 
     const char * const miniclient_args[] = { "miniclient", NULL };
     runApplication( miniclient_args, run_id );
@@ -370,7 +370,7 @@
 
 static void fibertv_stop(DIALServer *ds, const char *appname, DIAL_run_t run_id,
                          void *callback_data) {
-    printf("** KILL FiberTV **\n");
+    fprintf(stderr, "** KILL FiberTV **\n");
     pid_t pid;
     if ((pid = isAppRunning( spAppFiberTV, spAppFiberTVMatch ))) {
         kill(pid, SIGTERM);
@@ -382,11 +382,11 @@
 static void printUsage()
 {
     int i, numberOfOptions = sizeof(gDialOptions) / sizeof(dial_options_t);
-    printf("usage: dialserver <options>\n");
-    printf("options:\n");
+    fprintf(stderr, "usage: dialserver <options>\n");
+    fprintf(stderr, "options:\n");
     for( i = 0; i < numberOfOptions; i++ )
     {
-        printf("        %s|%s [value]: %s\n", 
+        fprintf(stderr, "        %s|%s [value]: %s\n",
             gDialOptions[i].pOption,
             gDialOptions[i].pLongOption,
             gDialOptions[i].pOptionDescription );
@@ -412,7 +412,7 @@
     DIAL_register_app(ds, "YouTube", &cb_yt, NULL);
     DIAL_register_app(ds, "FiberTV", &cb_ft, NULL);
     gDialPort = DIAL_get_port(ds);
-    printf("launcher listening on gDialPort %d\n", gDialPort);
+    fprintf(stderr, "launcher listening on gDialPort %d\n", gDialPort);
     run_ssdp(gDialPort, spFriendlyName, spModelName, spUuid);
 
     DIAL_stop(ds);
@@ -441,12 +441,16 @@
 int main(int argc, char* argv[]) 
 {
     int i;
+
+    // Set stdout to unbuffered mode to avoid delayed logs
+    setvbuf(stdout, NULL, _IONBF, 0);
+
     i = isAppRunning(spAppNetflix, NULL );
-    printf("Netflix is %s\n", i ? "Running":"Not Running");
+    fprintf(stderr, "Netflix is %s\n", i ? "Running":"Not Running");
     i = isAppRunning( spAppYouTube, spAppYouTubeMatch );
-    printf("YouTube is %s\n", i ? "Running":"Not Running");
+    fprintf(stderr, "YouTube is %s\n", i ? "Running":"Not Running");
     i = isAppRunning( spAppFiberTV, spAppFiberTVMatch );
-    printf("FiberTV is %s\n", i ? "Running":"Not Running");
+    fprintf(stderr, "FiberTV is %s\n", i ? "Running":"Not Running");
 
     // set all defaults
     setValue(spDefaultFriendlyName, spFriendlyName );
diff --git a/src/server/quick_ssdp.c b/src/server/quick_ssdp.c
index 91e46a0..9cef629 100644
--- a/src/server/quick_ssdp.c
+++ b/src/server/quick_ssdp.c
@@ -182,7 +182,7 @@
     if (!strstr(gBuf, "ST: urn:dial-multiscreen-org:service:dial:1"))
     {
 #if 0  // use for debugging
-      printf("Dropping: \n");
+      fprintf(stderr, "Dropping: \n");
       {
           int i;
           for (i = 0; i < bytes; i++)
@@ -190,12 +190,12 @@
               putchar(gBuf[i]);
           }
       }
-      printf("\n##### End of DROP #######\n");
+      fprintf(stderr, "\n##### End of DROP #######\n");
 #endif
       continue;
     }
     if (dial_allowed()) {
-        printf("Sending SSDP reply to %s:%d\n",
+        fprintf(stderr, "Sending SSDP reply to %s:%d\n",
               inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
         if (-1 == sendto(s, send_buf, send_size, 0, (struct sockaddr *)&saddr, addrlen)) {
           perror("sendto");
@@ -236,6 +236,6 @@
     my_port = ntohs(((struct sockaddr_in *)&sa)->sin_port);
   }
 
-  printf("SSDP listening on %s:%d\n", ip_addr, my_port);
+  fprintf(stderr, "SSDP listening on %s:%d\n", ip_addr, my_port);
   handle_mcast();
 }