Do proper logging rather than fprintf(stderr, ...)
diff --git a/common/rdr/RandomStream.cxx b/common/rdr/RandomStream.cxx
index 3fde18d..e22da3d 100644
--- a/common/rdr/RandomStream.cxx
+++ b/common/rdr/RandomStream.cxx
@@ -18,6 +18,7 @@
 
 #include <rdr/RandomStream.h>
 #include <rdr/Exception.h>
+#include <rfb/LogWriter.h>
 #include <time.h>
 #include <stdlib.h>
 #ifndef WIN32
@@ -30,6 +31,8 @@
 #endif
 #endif
 
+static rfb::LogWriter vlog("RandomStream");
+
 using namespace rdr;
 
 const int DEFAULT_BUF_LEN = 256;
@@ -46,11 +49,11 @@
   if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, 0)) {
     if (GetLastError() == (DWORD)NTE_BAD_KEYSET) {
       if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
-        fprintf(stderr, "RandomStream: unable to create keyset\n");
+        vlog.error("unable to create keyset");
         provider = 0;
       }
     } else {
-      fprintf(stderr, "RandomStream: unable to acquire context\n");
+      vlog.error("unable to acquire context");
       provider = 0;
     }
   }
@@ -65,7 +68,7 @@
   {
 #endif
 #endif
-    fprintf(stderr,"RandomStream: warning: no OS supplied random source - using rand()\n");
+    vlog.error("no OS supplied random source - using rand()");
     seed += (unsigned int) time(0) + getpid() + getpid() * 987654 + rand();
     srand(seed);
   }
diff --git a/common/rdr/ZlibOutStream.cxx b/common/rdr/ZlibOutStream.cxx
index 9d9f8ba..dd3c236 100644
--- a/common/rdr/ZlibOutStream.cxx
+++ b/common/rdr/ZlibOutStream.cxx
@@ -21,11 +21,14 @@
 
 #include <rdr/ZlibOutStream.h>
 #include <rdr/Exception.h>
+#include <rfb/LogWriter.h>
 
 #include <zlib.h>
 
 #undef ZLIBOUT_DEBUG
 
+static rfb::LogWriter vlog("ZlibOutStream");
+
 using namespace rdr;
 
 enum { DEFAULT_BUF_SIZE = 16384 };
@@ -85,7 +88,7 @@
   zs->avail_in = ptr - start;
 
 #ifdef ZLIBOUT_DEBUG
-  fprintf(stderr,"zos flush: avail_in %d\n",zs->avail_in);
+  vlog.debug("flush: avail_in %d",zs->avail_in);
 #endif
 
   // Force out everything from the zlib encoder
@@ -98,7 +101,7 @@
 int ZlibOutStream::overrun(int itemSize, int nItems)
 {
 #ifdef ZLIBOUT_DEBUG
-  fprintf(stderr,"zos overrun\n");
+  vlog.debug("overrun");
 #endif
 
   if (itemSize > bufSize)
@@ -120,7 +123,7 @@
     } else {
       // but didn't consume all the data?  try shifting what's left to the
       // start of the buffer.
-      fprintf(stderr,"z out buf not full, but in data not consumed\n");
+      vlog.info("z out buf not full, but in data not consumed");
       memmove(start, zs->next_in, ptr - zs->next_in);
       offset += zs->next_in - start;
       ptr -= zs->next_in - start;
@@ -149,8 +152,8 @@
     zs->avail_out = underlying->getend() - underlying->getptr();
 
 #ifdef ZLIBOUT_DEBUG
-    fprintf(stderr,"zos: calling deflate, avail_in %d, avail_out %d\n",
-            zs->avail_in,zs->avail_out);
+    vlog.debug("calling deflate, avail_in %d, avail_out %d",
+               zs->avail_in,zs->avail_out);
 #endif
 
     rc = ::deflate(zs, flush);
@@ -163,8 +166,8 @@
     }
 
 #ifdef ZLIBOUT_DEBUG
-    fprintf(stderr,"zos: after deflate: %d bytes\n",
-            zs->next_out-underlying->getptr());
+    vlog.debug("after deflate: %d bytes",
+               zs->next_out-underlying->getptr());
 #endif
 
     underlying->setptr(zs->next_out);
@@ -177,7 +180,7 @@
 
   if (newLevel != compressionLevel) {
 #ifdef ZLIBOUT_DEBUG
-    fprintf(stderr,"zos change: avail_in %d\n",zs->avail_in);
+    vlog.debug("change: avail_in %d",zs->avail_in);
 #endif
 
     // zlib is just horribly stupid. It does an implicit flush on
diff --git a/common/rfb/CMsgHandler.cxx b/common/rfb/CMsgHandler.cxx
index 4fe5041..c009067 100644
--- a/common/rfb/CMsgHandler.cxx
+++ b/common/rfb/CMsgHandler.cxx
@@ -19,9 +19,12 @@
 #include <stdio.h>
 
 #include <rfb/Exception.h>
+#include <rfb/LogWriter.h>
 #include <rfb/CMsgHandler.h>
 #include <rfb/screenTypes.h>
 
+static rfb::LogWriter vlog("CMsgHandler");
+
 using namespace rfb;
 
 CMsgHandler::CMsgHandler()
diff --git a/common/rfb/CMsgReader.cxx b/common/rfb/CMsgReader.cxx
index e8e7afb..2b5b9fb 100644
--- a/common/rfb/CMsgReader.cxx
+++ b/common/rfb/CMsgReader.cxx
@@ -23,10 +23,13 @@
 #include <rfb/msgTypes.h>
 #include <rdr/InStream.h>
 #include <rfb/Exception.h>
+#include <rfb/LogWriter.h>
 #include <rfb/util.h>
 #include <rfb/CMsgHandler.h>
 #include <rfb/CMsgReader.h>
 
+static rfb::LogWriter vlog("CMsgReader");
+
 using namespace rfb;
 
 CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_)
@@ -74,7 +77,7 @@
       readEndOfContinuousUpdates();
       break;
     default:
-      fprintf(stderr, "unknown message type %d\n", type);
+      vlog.error("unknown message type %d", type);
       throw Exception("unknown message type");
     }
   } else {
@@ -151,7 +154,7 @@
   rdr::U32 len = is->readU32();
   if (len > 256*1024) {
     is->skip(len);
-    fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
+    vlog.error("cut text too long (%d bytes) - ignoring",len);
     return;
   }
   CharArray ca(len+1);
@@ -172,7 +175,7 @@
 
   len = is->readU8();
   if (len > sizeof(data)) {
-    fprintf(stderr, "Ignoring fence with too large payload\n");
+    vlog.error("Ignoring fence with too large payload");
     is->skip(len);
     return;
   }
@@ -198,14 +201,14 @@
 {
   if ((r.br.x > handler->server.width()) ||
       (r.br.y > handler->server.height())) {
-    fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
+    vlog.error("Rect too big: %dx%d at %d,%d exceeds %dx%d",
 	    r.width(), r.height(), r.tl.x, r.tl.y,
             handler->server.width(), handler->server.height());
     throw Exception("Rect too big");
   }
 
   if (r.is_empty())
-    fprintf(stderr, "Warning: zero size rect\n");
+    vlog.error("zero size rect");
 
   handler->dataRect(r, encoding);
 }
@@ -449,7 +452,7 @@
   char* name = is->readString();
 
   if (x || y || w || h) {
-    fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
+    vlog.error("Ignoring DesktopName rect with non-zero position/size");
   } else {
     handler->setName(name);
   }
diff --git a/common/rfb/Configuration.cxx b/common/rfb/Configuration.cxx
index 619c4d5..ea00060 100644
--- a/common/rfb/Configuration.cxx
+++ b/common/rfb/Configuration.cxx
@@ -379,7 +379,7 @@
   : VoidParameter(name_, desc_, co), value(strDup(v)), def_value(v)
 {
   if (!v) {
-    fprintf(stderr,"Default value <null> for %s not allowed\n",name_);
+    vlog.error("Default value <null> for %s not allowed",name_);
     throw rfb::Exception("Default value <null> not allowed");
   }
 }
diff --git a/common/rfb/Region.cxx b/common/rfb/Region.cxx
index c17c5d4..beca482 100644
--- a/common/rfb/Region.cxx
+++ b/common/rfb/Region.cxx
@@ -24,6 +24,7 @@
 //
 
 #include <rfb/Region.h>
+#include <rfb/LogWriter.h>
 #include <assert.h>
 #include <stdio.h>
 
@@ -33,6 +34,8 @@
 #include <Xregion/Xregion.h>
 }
 
+static rfb::LogWriter vlog("Region");
+
 // A _RectRegion must never be passed as a return parameter to the Xlib region
 // operations.  This is because for efficiency its "rects" member has not been
 // allocated with Xmalloc.  It is however safe to pass it as an input
@@ -123,7 +126,7 @@
     BOX* prevRects = xrgn->rects;
     xrgn->rects = (BOX*)Xrealloc((char*)xrgn->rects, nRects * sizeof(BOX));
     if (!xrgn->rects) {
-      fprintf(stderr,"Xrealloc failed\n");
+      vlog.error("Xrealloc failed");
       Xfree(prevRects);
       return;
     }
@@ -234,13 +237,13 @@
 
 void rfb::Region::debug_print(const char* prefix) const
 {
-  fprintf(stderr,"%s num rects %3ld extents %3d,%3d %3dx%3d\n",
+  vlog.debug("%s num rects %3ld extents %3d,%3d %3dx%3d",
           prefix, xrgn->numRects, xrgn->extents.x1, xrgn->extents.y1,
           xrgn->extents.x2-xrgn->extents.x1,
           xrgn->extents.y2-xrgn->extents.y1);
 
   for (int i = 0; i < xrgn->numRects; i++) {
-    fprintf(stderr,"    rect %3d,%3d %3dx%3d\n",
+    vlog.debug("    rect %3d,%3d %3dx%3d",
             xrgn->rects[i].x1, xrgn->rects[i].y1,
             xrgn->rects[i].x2-xrgn->rects[i].x1,
             xrgn->rects[i].y2-xrgn->rects[i].y1);
diff --git a/common/rfb/SMsgReader.cxx b/common/rfb/SMsgReader.cxx
index cb71ac8..200350c 100644
--- a/common/rfb/SMsgReader.cxx
+++ b/common/rfb/SMsgReader.cxx
@@ -83,7 +83,7 @@
     readQEMUMessage();
     break;
   default:
-    fprintf(stderr, "unknown message type %d\n", msgType);
+    vlog.error("unknown message type %d", msgType);
     throw Exception("unknown message type");
   }
 }
@@ -173,7 +173,7 @@
 
   len = is->readU8();
   if (len > sizeof(data)) {
-    fprintf(stderr, "Ignoring fence with too large payload\n");
+    vlog.error("Ignoring fence with too large payload");
     is->skip(len);
     return;
   }