Created and integrated levelized log

Change-Id: Ibfed76796a880bab2709b77b0c9540be7b75fad7
diff --git a/.waf-tools/boost.py b/.waf-tools/boost.py
index 8c36b34..9b9395e 100755
--- a/.waf-tools/boost.py
+++ b/.waf-tools/boost.py
@@ -30,15 +30,15 @@
 
 Options are generated, in order to specify the location of boost includes/libraries.
 The `check_boost` configuration function allows to specify the used boost libraries.
-It can also provide default arguments to the --boost-static and --boost-mt command-line arguments.
+It can also provide default arguments to the --boost-mt command-line arguments.
 Everything will be packaged together in a BOOST component that you can use.
 
 When using MSVC, a lot of compilation flags need to match your BOOST build configuration:
  - you may have to add /EHsc to your CXXFLAGS or define boost::throw_exception if BOOST_NO_EXCEPTIONS is defined.
    Errors: C4530
  - boost libraries will try to be smart and use the (pretty but often not useful) auto-linking feature of MSVC
-   So before calling `conf.check_boost` you might want to disabling by adding:
-   	conf.env.DEFINES_BOOST += ['BOOST_ALL_NO_LIB']
+   So before calling `conf.check_boost` you might want to disabling by adding
+		conf.env.DEFINES_BOOST += ['BOOST_ALL_NO_LIB']
    Errors:
  - boost might also be compiled with /MT, which links the runtime statically.
    If you have problems with redefined symbols,
@@ -52,27 +52,55 @@
 import re
 from waflib import Utils, Logs, Errors
 from waflib.Configure import conf
+from waflib.TaskGen import feature, after_method
 
-BOOST_LIBS = ['/usr/lib', '/usr/local/lib', '/opt/local/lib', '/sw/lib', '/lib',
-              '/usr/lib/x86_64-linux-gnu', '/usr/lib/i386-linux-gnu',
-              '/usr/local/ndn/lib', '/opt/ndn/lib']
-BOOST_INCLUDES = ['/usr/include', '/usr/local/include', '/opt/local/include', '/sw/include',
-                  '/usr/local/ndn/include', '/opt/ndn/include']
+BOOST_LIBS = ['/usr/lib/x86_64-linux-gnu', '/usr/lib/i386-linux-gnu',
+			  '/usr/lib', '/usr/local/lib', '/opt/local/lib', '/sw/lib', '/lib']
+BOOST_INCLUDES = ['/usr/include', '/usr/local/include', '/opt/local/include', '/sw/include']
 BOOST_VERSION_FILE = 'boost/version.hpp'
 BOOST_VERSION_CODE = '''
 #include <iostream>
 #include <boost/version.hpp>
 int main() { std::cout << BOOST_LIB_VERSION << ":" << BOOST_VERSION << std::endl; }
 '''
-BOOST_SYSTEM_CODE = '''
+
+BOOST_ERROR_CODE = '''
 #include <boost/system/error_code.hpp>
 int main() { boost::system::error_code c; }
 '''
+
+PTHREAD_CODE = '''
+#include <pthread.h>
+static void* f(void*) { return 0; }
+int main() {
+	pthread_t th;
+	pthread_attr_t attr;
+	pthread_attr_init(&attr);
+	pthread_create(&th, &attr, &f, 0);
+	pthread_join(th, 0);
+	pthread_cleanup_push(0, 0);
+	pthread_cleanup_pop(0);
+	pthread_attr_destroy(&attr);
+}
+'''
+
 BOOST_THREAD_CODE = '''
 #include <boost/thread.hpp>
 int main() { boost::thread t; }
 '''
 
+BOOST_LOG_CODE = '''
+#include <boost/log/trivial.hpp>
+#include <boost/log/utility/setup/console.hpp>
+#include <boost/log/utility/setup/common_attributes.hpp>
+int main() {
+	using namespace boost::log;
+	add_common_attributes();
+	add_console_log(std::clog, keywords::format = "%Message%");
+	BOOST_LOG_TRIVIAL(debug) << "log is working" << std::endl;
+}
+'''
+
 # toolsets from {boost_dir}/tools/build/v2/tools/common.jam
 PLATFORM = Utils.unversioned_sys_platform()
 detect_intel = lambda env: (PLATFORM == 'win32') and 'iw' or 'il'
@@ -104,34 +132,37 @@
 
 def options(opt):
 	opt = opt.add_option_group('Boost Options')
-
 	opt.add_option('--boost-includes', type='string',
 				   default='', dest='boost_includes',
-				   help='''path to the directory where the boost includes are, e.g., /path/to/boost_1_55_0/stage/include''')
+				   help='''path to the directory where the boost includes are,
+				   e.g., /path/to/boost_1_55_0/stage/include''')
 	opt.add_option('--boost-libs', type='string',
 				   default='', dest='boost_libs',
-				   help='''path to the directory where the boost libs are, e.g., /path/to/boost_1_55_0/stage/lib''')
-	opt.add_option('--boost-static', action='store_true',
-				   default=False, dest='boost_static',
-				   help='link with static boost libraries (.lib/.a)')
+				   help='''path to the directory where the boost libs are,
+				   e.g., path/to/boost_1_55_0/stage/lib''')
 	opt.add_option('--boost-mt', action='store_true',
 				   default=False, dest='boost_mt',
 				   help='select multi-threaded libraries')
 	opt.add_option('--boost-abi', type='string', default='', dest='boost_abi',
-				   help='''select libraries with tags (dgsyp, d for debug), see doc Boost, Getting Started, chapter 6.1''')
+				   help='''select libraries with tags (gd for debug, static is automatically added),
+				   see doc Boost, Getting Started, chapter 6.1''')
 	opt.add_option('--boost-linkage_autodetect', action="store_true", dest='boost_linkage_autodetect',
 				   help="auto-detect boost linkage options (don't get used to it / might break other stuff)")
 	opt.add_option('--boost-toolset', type='string',
 				   default='', dest='boost_toolset',
-				   help='force a toolset e.g. msvc, vc90, gcc, mingw, mgw45 (default: auto)')
+				   help='force a toolset e.g. msvc, vc90, \
+						gcc, mingw, mgw45 (default: auto)')
 	py_version = '%d%d' % (sys.version_info[0], sys.version_info[1])
 	opt.add_option('--boost-python', type='string',
 				   default=py_version, dest='boost_python',
-				   help='select the lib python with this version (default: %s)' % py_version)
+				   help='select the lib python with this version \
+						(default: %s)' % py_version)
 
 
 @conf
 def __boost_get_version_file(self, d):
+	if not d:
+		return None
 	dnode = self.root.find_dir(d)
 	if dnode:
 		return dnode.find_node(BOOST_VERSION_FILE)
@@ -144,18 +175,15 @@
 	if node:
 		try:
 			txt = node.read()
-		except (OSError, IOError):
+		except EnvironmentError:
 			Logs.error("Could not read the file %r" % node.abspath())
 		else:
-			re_but1 = re.compile('^#define\\s+BOOST_LIB_VERSION\\s+"(.*)"', re.M)
+			re_but1 = re.compile('^#define\\s+BOOST_LIB_VERSION\\s+"(.+)"', re.M)
 			m1 = re_but1.search(txt)
-
-			re_but2 = re.compile('^#define\\s+BOOST_VERSION\\s+(\\d*)', re.M)
+			re_but2 = re.compile('^#define\\s+BOOST_VERSION\\s+(\\d+)', re.M)
 			m2 = re_but2.search(txt)
-
 			if m1 and m2:
 				return (m1.group(1), m2.group(1))
-
 	return self.check_cxx(fragment=BOOST_VERSION_CODE, includes=[d], execute=True, define_ret=True).split(":")
 
 @conf
@@ -163,7 +191,7 @@
 	includes = k and k[0] or kw.get('includes', None)
 	if includes and self.__boost_get_version_file(includes):
 		return includes
-	for d in Utils.to_list(self.environ.get('INCLUDE', '')) + BOOST_INCLUDES:
+	for d in self.environ.get('INCLUDE', '').split(';') + BOOST_INCLUDES:
 		if self.__boost_get_version_file(d):
 			return d
 	if includes:
@@ -198,7 +226,9 @@
 		path = self.root.find_dir(libs)
 		files = path.ant_glob('*boost_*')
 	if not libs or not files:
-		for d in Utils.to_list(self.environ.get('LIB', [])) + BOOST_LIBS:
+		for d in self.environ.get('LIB', '').split(';') + BOOST_LIBS:
+			if not d:
+				continue
 			path = self.root.find_dir(d)
 			if path:
 				files = path.ant_glob('*boost_*')
@@ -229,15 +259,10 @@
 	according to the parameters
 	'''
 	path, files = self.__boost_get_libs_path(**kw)
-	t = []
-	if kw.get('mt', False):
-		t.append('mt')
-	if kw.get('abi', None):
-		t.append(kw['abi'])
-	tags = t and '(-%s)+' % '-'.join(t) or ''
+	files = sorted(files, key=lambda f: (len(f.name), f.name), reverse=True)
 	toolset = self.boost_get_toolset(kw.get('toolset', ''))
-	toolset_pat = '(-%s[0-9]{0,3})+' % toolset
-	version = '(-%s)+' % self.env.BOOST_VERSION
+	toolset_pat = '(-%s[0-9]{0,3})' % toolset
+	version = '-%s' % self.env.BOOST_VERSION
 
 	def find_lib(re_lib, files):
 		for file in files:
@@ -251,28 +276,110 @@
 			name = name[3:]
 		return name[:name.rfind('.')]
 
-	libs = []
-	for lib in Utils.to_list(k and k[0] or kw.get('lib', None)):
-		py = (lib == 'python') and '(-py%s)+' % kw['python'] or ''
-		# Trying libraries, from most strict match to least one
-		for pattern in ['boost_%s%s%s%s%s' % (lib, toolset_pat, tags, py, version),
-						'boost_%s%s%s%s' % (lib, tags, py, version),
-						'boost_%s%s%s' % (lib, tags, version),
-						# Give up trying to find the right version
-						'boost_%s%s%s%s' % (lib, toolset_pat, tags, py),
-						'boost_%s%s%s' % (lib, tags, py),
-						'boost_%s%s' % (lib, tags)]:
-			self.to_log('Trying pattern %s' % pattern)
-			file = find_lib(re.compile(pattern), files)
-			if file:
-				libs.append(format_lib_name(file.name))
-				break
-		else:
-			self.end_msg('lib %s not found in %s' % (lib, path.abspath()))
-			self.fatal('The configuration failed')
+	def match_libs(lib_names, is_static):
+		libs = []
+		lib_names = Utils.to_list(lib_names)
+		if not lib_names:
+			return libs
+		t = []
+		if kw.get('mt', False):
+			t.append('-mt')
+		if kw.get('abi', None):
+			t.append('%s%s' % (is_static and '-s' or '-', kw['abi']))
+		elif is_static:
+			t.append('-s')
+		tags_pat = t and ''.join(t) or ''
+		ext = is_static and self.env.cxxstlib_PATTERN or self.env.cxxshlib_PATTERN
+		ext = ext.partition('%s')[2] # remove '%s' or 'lib%s' from PATTERN
 
-	return path.abspath(), libs
+		for lib in lib_names:
+			if lib == 'python':
+				# for instance, with python='27',
+				# accepts '-py27', '-py2', '27' and '2'
+				# but will reject '-py3', '-py26', '26' and '3'
+				tags = '({0})?((-py{2})|(-py{1}(?=[^0-9]))|({2})|({1}(?=[^0-9]))|(?=[^0-9])(?!-py))'.format(tags_pat, kw['python'][0], kw['python'])
+			else:
+				tags = tags_pat
+			# Trying libraries, from most strict match to least one
+			for pattern in ['boost_%s%s%s%s%s$' % (lib, toolset_pat, tags, version, ext),
+							'boost_%s%s%s%s$' % (lib, tags, version, ext),
+							# Give up trying to find the right version
+							'boost_%s%s%s%s$' % (lib, toolset_pat, tags, ext),
+							'boost_%s%s%s$' % (lib, tags, ext),
+							'boost_%s%s$' % (lib, ext),
+							'boost_%s' % lib]:
+				self.to_log('Trying pattern %s' % pattern)
+				file = find_lib(re.compile(pattern), files)
+				if file:
+					libs.append(format_lib_name(file.name))
+					break
+			else:
+				self.end_msg('lib %s not found in %s' % (lib, path.abspath()))
+				self.fatal('The configuration failed')
+		return libs
 
+	return  path.abspath(), match_libs(kw.get('lib', None), False), match_libs(kw.get('stlib', None), True)
+
+@conf
+def _check_pthread_flag(self, *k, **kw):
+	'''
+	Computes which flags should be added to CXXFLAGS and LINKFLAGS to compile in multi-threading mode
+
+	Yes, we *need* to put the -pthread thing in CPPFLAGS because with GCC3,
+	boost/thread.hpp will trigger a #error if -pthread isn't used:
+	  boost/config/requires_threads.hpp:47:5: #error "Compiler threading support
+	  is not turned on. Please set the correct command line options for
+	  threading: -pthread (Linux), -pthreads (Solaris) or -mthreads (Mingw32)"
+
+	Based on _BOOST_PTHREAD_FLAG(): https://github.com/tsuna/boost.m4/blob/master/build-aux/boost.m4
+    '''
+
+	var = kw.get('uselib_store', 'BOOST')
+
+	self.start_msg('Checking the flags needed to use pthreads')
+
+	# The ordering *is* (sometimes) important.  Some notes on the
+	# individual items follow:
+	# (none): in case threads are in libc; should be tried before -Kthread and
+	#       other compiler flags to prevent continual compiler warnings
+	# -lpthreads: AIX (must check this before -lpthread)
+	# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
+	# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
+	# -llthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
+	# -pthread: GNU Linux/GCC (kernel threads), BSD/GCC (userland threads)
+	# -pthreads: Solaris/GCC
+	# -mthreads: MinGW32/GCC, Lynx/GCC
+	# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
+	#      doesn't hurt to check since this sometimes defines pthreads too;
+	#      also defines -D_REENTRANT)
+	#      ... -mt is also the pthreads flag for HP/aCC
+	# -lpthread: GNU Linux, etc.
+	# --thread-safe: KAI C++
+	if Utils.unversioned_sys_platform() == "sunos":
+		# On Solaris (at least, for some versions), libc contains stubbed
+		# (non-functional) versions of the pthreads routines, so link-based
+		# tests will erroneously succeed.  (We need to link with -pthreads/-mt/
+		# -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
+		# a function called by this macro, so we could check for that, but
+		# who knows whether they'll stub that too in a future libc.)  So,
+		# we'll just look for -pthreads and -lpthread first:
+		boost_pthread_flags = ["-pthreads", "-lpthread", "-mt", "-pthread"]
+	else:
+		boost_pthread_flags = ["", "-lpthreads", "-Kthread", "-kthread", "-llthread", "-pthread",
+							   "-pthreads", "-mthreads", "-lpthread", "--thread-safe", "-mt"]
+
+	for boost_pthread_flag in boost_pthread_flags:
+		try:
+			self.env.stash()
+			self.env['CXXFLAGS_%s' % var] += [boost_pthread_flag]
+			self.env['LINKFLAGS_%s' % var] += [boost_pthread_flag]
+			self.check_cxx(code=PTHREAD_CODE, msg=None, use=var, execute=False)
+
+			self.end_msg(boost_pthread_flag)
+			return
+		except self.errors.ConfigurationError:
+			self.env.revert()
+	self.end_msg('None')
 
 @conf
 def check_boost(self, *k, **kw):
@@ -285,7 +392,10 @@
 	if not self.env['CXX']:
 		self.fatal('load a c++ compiler first, conf.load("compiler_cxx")')
 
-	params = {'lib': k and k[0] or kw.get('lib', None)}
+	params = {
+		'lib': k and k[0] or kw.get('lib', None),
+		'stlib': kw.get('stlib', None)
+	}
 	for key, value in self.options.__dict__.items():
 		if not key.startswith('boost_'):
 			continue
@@ -300,42 +410,53 @@
 	self.env.BOOST_VERSION = versions[0]
 	self.env.BOOST_VERSION_NUMBER = int(versions[1])
 	self.end_msg("%d.%d.%d" % (int(versions[1]) / 100000,
-				   int(versions[1]) / 100 % 1000,
-				   int(versions[1]) % 100))
+							   int(versions[1]) / 100 % 1000,
+							   int(versions[1]) % 100))
 	if Logs.verbose:
 		Logs.pprint('CYAN', '	path : %s' % self.env['INCLUDES_%s' % var])
 
-	if not params['lib']:
+	if not params['lib'] and not params['stlib']:
 		return
+	if 'static' in kw or 'static' in params:
+		Logs.warn('boost: static parameter is deprecated, use stlib instead.')
 	self.start_msg('Checking boost libs')
-	suffix = params.get('static', None) and 'ST' or ''
-	path, libs = self.boost_get_libs(**params)
-	self.env['%sLIBPATH_%s' % (suffix, var)] = [path]
-	self.env['%sLIB_%s' % (suffix, var)] = libs
+	path, libs, stlibs = self.boost_get_libs(**params)
+	self.env['LIBPATH_%s' % var] = [path]
+	self.env['STLIBPATH_%s' % var] = [path]
+	self.env['LIB_%s' % var] = libs
+	self.env['STLIB_%s' % var] = stlibs
 	self.end_msg('ok')
 	if Logs.verbose:
 		Logs.pprint('CYAN', '	path : %s' % path)
-		Logs.pprint('CYAN', '	libs : %s' % libs)
+		Logs.pprint('CYAN', '	shared libs : %s' % libs)
+		Logs.pprint('CYAN', '	static libs : %s' % stlibs)
 
+	def has_shlib(lib):
+		return params['lib'] and lib in params['lib']
+	def has_stlib(lib):
+		return params['stlib'] and lib in params['stlib']
+	def has_lib(lib):
+		return has_shlib(lib) or has_stlib(lib)
+	if has_lib('thread'):
+		# not inside try_link to make check visible in the output
+		self._check_pthread_flag(k, kw)
 
 	def try_link():
-		if 'system' in params['lib']:
-			self.check_cxx(
-			 fragment=BOOST_SYSTEM_CODE,
-			 use=var,
-			 execute=False,
-			)
-		if 'thread' in params['lib']:
-			self.check_cxx(
-			 fragment=BOOST_THREAD_CODE,
-			 use=var,
-			 execute=False,
-			)
+		if has_lib('system'):
+			self.check_cxx(fragment=BOOST_ERROR_CODE, use=var, execute=False)
+		if has_lib('thread'):
+			self.check_cxx(fragment=BOOST_THREAD_CODE, use=var, execute=False)
+		if has_lib('log'):
+			if not has_lib('thread'):
+				self.env['DEFINES_%s' % var] += ['BOOST_LOG_NO_THREADS']
+			if has_shlib('log'):
+				self.env['DEFINES_%s' % var] += ['BOOST_LOG_DYN_LINK']
+			self.check_cxx(fragment=BOOST_LOG_CODE, use=var, execute=False)
 
 	if params.get('linkage_autodetect', False):
 		self.start_msg("Attempting to detect boost linkage flags")
 		toolset = self.boost_get_toolset(kw.get('toolset', ''))
-		if toolset in ['vc']:
+		if toolset in ('vc',):
 			# disable auto-linking feature, causing error LNK1181
 			# because the code wants to be linked against
 			self.env['DEFINES_%s' % var] += ['BOOST_ALL_NO_LIB']
@@ -359,14 +480,14 @@
 				try:
 					try_link()
 					self.end_msg("ok: winning cxxflags combination: %s" % (self.env["CXXFLAGS_%s" % var]))
-					e = None
+					exc = None
 					break
-				except Errors.ConfigurationError as exc:
+				except Errors.ConfigurationError as e:
 					self.env.revert()
-					e = exc
+					exc = e
 
-			if e is not None:
-				self.end_msg("Could not auto-detect boost linking flags combination, you may report it to boost.py author", ex=e)
+			if exc is not None:
+				self.end_msg("Could not auto-detect boost linking flags combination, you may report it to boost.py author", ex=exc)
 				self.fatal('The configuration failed')
 		else:
 			self.end_msg("Boost linkage flags auto-detection not implemented (needed ?) for this toolchain")
@@ -379,3 +500,19 @@
 			self.end_msg("Could not link against boost libraries using supplied options")
 			self.fatal('The configuration failed')
 		self.end_msg('ok')
+
+
+@feature('cxx')
+@after_method('apply_link')
+def install_boost(self):
+	if install_boost.done or not Utils.is_win32 or not self.bld.cmd.startswith('install'):
+		return
+	install_boost.done = True
+	inst_to = getattr(self, 'install_path', '${BINDIR}')
+	for lib in self.env.LIB_BOOST:
+		try:
+			file = self.bld.find_file(self.env.cxxshlib_PATTERN % lib, self.env.LIBPATH_BOOST)
+			self.bld.install_files(inst_to, self.bld.root.find_node(file))
+		except:
+			continue
+install_boost.done = False
diff --git a/src/main.cpp b/src/main.cpp
index 8ae511f..5500ada 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -23,6 +23,7 @@
 #include "sequential-data-fetcher.hpp"
 #include "torrent-file.hpp"
 #include "util/io-util.hpp"
+#include "util/logging.hpp"
 
 #include <iostream>
 #include <iterator>
@@ -31,8 +32,11 @@
 
 #include <boost/program_options.hpp>
 #include <boost/program_options/errors.hpp>
+#include <boost/log/utility/setup/common_attributes.hpp>
 
+namespace logging = boost::log;
 namespace po = boost::program_options;
+
 using namespace ndn::ntorrent;
 
 namespace ndn {
@@ -53,6 +57,9 @@
 int main(int argc, char *argv[])
 {
   try {
+   LoggingUtil::init();
+   logging::add_common_attributes();
+
     po::options_description desc("Allowed options");
     desc.add_options()
     // TODO(msweatt) Consider  adding  flagged args for other parameters
@@ -100,14 +107,14 @@
       // write all the torrent segments
       for (const TorrentFile& t : torrentSegments) {
         if (!IoUtil::writeTorrentSegment(t, torrentPath)) {
-          std::cerr << "Write failed: " << t.getName() << std::endl;
+          LOG_ERROR << "Write failed: " << t.getName() << std::endl;
           return -1;
         }
       }
       auto manifestPath = outputPath + "/manifests/";
       for (const FileManifest& m : manifests) {
         if (!IoUtil::writeFileManifest(m, manifestPath)) {
-          std::cerr << "Write failed: " << m.getName() << std::endl;
+          LOG_ERROR << "Write failed: " << m.getName() << std::endl;
           return -1;
         }
       }
diff --git a/src/sequential-data-fetcher.cpp b/src/sequential-data-fetcher.cpp
index 06b3f02..b994384 100644
--- a/src/sequential-data-fetcher.cpp
+++ b/src/sequential-data-fetcher.cpp
@@ -20,6 +20,7 @@
 */
 
 #include "sequential-data-fetcher.hpp"
+#include "util/logging.hpp"
 #include "util/io-util.hpp"
 
 namespace ndn {
@@ -67,7 +68,7 @@
   std::vector<ndn::Name> returnedNames;
   returnedNames = m_manager->downloadTorrentFile(".appdata/torrent_files/");
   if (!returnedNames.empty() && IoUtil::NAME_TYPE::FILE_MANIFEST == IoUtil::findType(returnedNames[0])) {
-    std::cout << "Torrent File Received: "
+    LOG_INFO  << "Torrent File Received: "
               << m_torrentFileName.getSubName(0, m_torrentFileName.size() - 1) << std::endl;
   }
   return returnedNames;
@@ -121,13 +122,13 @@
 SequentialDataFetcher::onDataPacketReceived(const ndn::Data& data)
 {
   // Data Packet Received
-  std::cout << "Data Packet Received: " << data.getName();
+  LOG_INFO << "Data Packet Received: " << data.getName();
 }
 
 void
 SequentialDataFetcher::onManifestReceived(const std::vector<Name>& packetNames)
 {
-  std::cout << "Manifest File Received: "
+  LOG_INFO << "Manifest File Received: "
             << packetNames[0].getSubName(0, packetNames[0].size()- 3) << std::endl;
   this->downloadPackets(packetNames);
 }
@@ -136,26 +137,32 @@
 SequentialDataFetcher::onDataRetrievalFailure(const ndn::Interest& interest,
                                               const std::string& errorCode)
 {
-  // std::cerr << "Data Retrieval Failed: " << interest.getName() << std::endl;
-
   // Data retrieval failure
   uint32_t nameType = IoUtil::findType(interest.getName());
   if (nameType == IoUtil::TORRENT_FILE) {
     // this should never happen
-    // std::cerr << "Torrent File Segment Downloading Failed: " << interest.getName();
+    LOG_ERROR << "Torrent File Segment Downloading Failed: " << interest.getName();
     this->downloadTorrentFile();
   }
   else if (nameType == IoUtil::FILE_MANIFEST) {
-    // std::cerr << "Manifest File Segment Downloading Failed: " << interest.getName();
+    LOG_ERROR << "Manifest File Segment Downloading Failed: " << interest.getName();
     this->downloadManifestFiles({ interest.getName() });
   }
   else if (nameType == IoUtil::DATA_PACKET) {
-    // std::cerr << "Data Packet Downloading Failed: " << interest.getName();
+    LOG_ERROR << "Torrent File Segment Downloading Failed: " << interest.getName();
+    this->downloadTorrentFile();
+  }
+  else if (nameType == IoUtil::FILE_MANIFEST) {
+    LOG_ERROR << "Manifest File Segment Downloading Failed: " << interest.getName();
+    this->downloadManifestFiles({ interest.getName() });
+  }
+  else if (nameType == IoUtil::DATA_PACKET) {
+    LOG_ERROR << "Data Packet Downloading Failed: " << interest.getName();
     this->downloadPackets({ interest.getName() });
   }
   else {
     // This should never happen
-    // std::cerr << "Unknown Packet Type Downloading Failed: " << interest.getName();
+    LOG_ERROR << "Unknown Packet Type Downloading Failed: " << interest.getName();
   }
 }
 
diff --git a/src/torrent-manager.cpp b/src/torrent-manager.cpp
index 790e166..c976cb1 100644
--- a/src/torrent-manager.cpp
+++ b/src/torrent-manager.cpp
@@ -3,6 +3,7 @@
 #include "file-manifest.hpp"
 #include "torrent-file.hpp"
 #include "util/io-util.hpp"
+#include "util/logging.hpp"
 
 #include <boost/filesystem.hpp>
 #include <boost/filesystem/fstream.hpp>
@@ -613,7 +614,7 @@
   }
   else {
     // TODO(msweatt) NACK
-    std::cerr << "NACK: " << interest << std::endl;
+    LOG_ERROR << "NACK: " << interest << std::endl;
   }
   return;
 }
@@ -621,7 +622,7 @@
 void
 TorrentManager::onRegisterFailed(const Name& prefix, const std::string& reason)
 {
-  std::cerr << "ERROR: Failed to register prefix \""
+  LOG_ERROR << "ERROR: Failed to register prefix \""
             << prefix << "\" in local hub's daemon (" << reason << ")"
             << std::endl;
   m_face->shutdown();
diff --git a/src/update-handler.cpp b/src/update-handler.cpp
index 3d13b21..5c942da 100644
--- a/src/update-handler.cpp
+++ b/src/update-handler.cpp
@@ -20,6 +20,7 @@
 */
 
 #include "update-handler.hpp"
+#include "util/logging.hpp"
 
 #include <ndn-cxx/security/signing-helpers.hpp>
 
@@ -106,7 +107,7 @@
   // RoutableName ::= NAME-TYPE TLV-LENGTH
   //                  Name
 
-  std::cout << "ALIVE data packet received: " << data.getName() << std::endl;
+  LOG_INFO << "ALIVE data packet received: " << data.getName() << std::endl;
 
   if (data.getContentType() != tlv::ContentType_Blob) {
       BOOST_THROW_EXCEPTION(Error("Expected Content Type Blob"));
@@ -160,7 +161,7 @@
   };
 
   auto prefixRetrievalFailed = [this] (const Interest&) {
-    std::cerr << "Own Routable Prefix Retrieval Failed. Trying again." << std::endl;
+    LOG_ERROR << "Own Routable Prefix Retrieval Failed. Trying again." << std::endl;
     // TODO(Spyros): This could lead to an infinite loop. Figure out something better...
     this->learnOwnRoutablePrefix();
   };
@@ -172,7 +173,7 @@
 void
 UpdateHandler::onInterestReceived(const InterestFilter& filter, const Interest& interest)
 {
-  std::cout << "Interest Received: " << interest.getName().toUri() << std::endl;
+  LOG_INFO << "Interest Received: " << interest.getName().toUri() << std::endl;
   shared_ptr<Data> data = this->createDataPacket(interest.getName());
   m_keyChain->sign(*data, signingWithSha256());
   m_face->put(*data);
@@ -181,7 +182,7 @@
 void
 UpdateHandler::onRegisterFailed(const Name& prefix, const std::string& reason)
 {
-  std::cerr << "ERROR: Failed to register prefix \""
+ LOG_ERROR << "ERROR: Failed to register prefix \""
             << prefix << "\" in local hub's daemon (" << reason << ")"
             << std::endl;
   m_face->shutdown();
diff --git a/src/util/io-util.cpp b/src/util/io-util.cpp
index 0d02475..12572b3 100644
--- a/src/util/io-util.cpp
+++ b/src/util/io-util.cpp
@@ -2,6 +2,7 @@
 
 #include "file-manifest.hpp"
 #include "torrent-file.hpp"
+#include "util/logging.hpp"
 
 #include <boost/filesystem.hpp>
 #include <boost/filesystem/fstream.hpp>
@@ -148,7 +149,7 @@
     return true;
   }
   catch (io::Error &e) {
-    std::cerr << e.what() << std::endl;
+    LOG_ERROR << e.what() << std::endl;
     return false;
   }
 }
@@ -166,14 +167,14 @@
   is.sync();
   is.seekg(start_offset + packetNum * dataPacketSize);
   if (is.tellg() < 0) {
-    std::cerr << "bad seek" << std::endl;
+    LOG_ERROR << "bad seek" << std::endl;
   }
  // read contents
  std::vector<char> bytes(dataPacketSize);
  is.read(&bytes.front(), dataPacketSize);
  auto read_size = is.gcount();
  if (is.bad() || read_size < 0) {
-  std::cerr << "Bad read" << std::endl;
+  LOG_ERROR << "Bad read" << std::endl;
   return nullptr;
  }
  // construct packet
diff --git a/src/util/logging.cpp b/src/util/logging.cpp
new file mode 100644
index 0000000..b067c80
--- /dev/null
+++ b/src/util/logging.cpp
@@ -0,0 +1,71 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+
+#include "util/logging.hpp"
+
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <boost/log/core.hpp>
+#include <boost/log/expressions.hpp>
+#include <boost/log/sinks/text_file_backend.hpp>
+#include <boost/log/sources/record_ostream.hpp>
+#include <boost/log/sources/severity_logger.hpp>
+#include <boost/log/support/date_time.hpp>
+#include <boost/log/trivial.hpp>
+#include <boost/log/utility/setup/file.hpp>
+
+// ===== log macros =====
+namespace logging = boost::log;
+namespace src = boost::log::sources;
+namespace sinks = boost::log::sinks;
+namespace keywords = boost::log::keywords;
+namespace expr = boost::log::expressions;
+
+namespace ndn {
+namespace ntorrent {
+
+void LoggingUtil::init()
+{
+  logging::core::get()->set_filter
+  (
+      logging::trivial::severity >= SEVERITY_THRESHOLD
+  );
+
+  logging::add_file_log
+  (
+     keywords::file_name = "sample_%N.log",                                        // < file name pattern >
+     keywords::rotation_size = 10 * 1024 * 1024,                                   // < rotate files every 10 MiB... >
+     keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), // < ...or at midnight >
+     keywords::format =                                                            // < log record format >
+     (
+       expr::stream
+           << expr::attr< unsigned int >("LineID")
+           << ": [" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]"
+           << ": <" << logging::trivial::severity
+           << "> " << expr::smessage
+    )
+  );
+}
+
+} // end ntorrent
+} // end ndn
+
+
diff --git a/src/util/logging.hpp b/src/util/logging.hpp
new file mode 100644
index 0000000..bb926a4
--- /dev/null
+++ b/src/util/logging.hpp
@@ -0,0 +1,58 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+#ifndef UTIL_LOGGING_HPP
+#define UTIL_LOGGING_HPP
+
+#define BOOST_LOG_DYN_LINK 1
+
+#include <boost/log/core.hpp>
+#include <boost/log/sources/global_logger_storage.hpp>
+#include <boost/log/sources/severity_logger.hpp>
+#include <boost/log/trivial.hpp>
+
+enum { SEVERITY_THRESHOLD = boost::log::trivial::warning };
+
+// register a global logger
+BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(logger, boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level>)
+
+// just a helper macro used by the macros below - don't use it in your code
+#define LOG(severity) BOOST_LOG_SEV(logger::get(), boost::log::trivial::severity)
+
+// ===== log macros =====
+#define LOG_TRACE   LOG(trace)
+#define LOG_DEBUG   LOG(debug)
+#define LOG_INFO    LOG(info)
+#define LOG_WARNING LOG(warning)
+#define LOG_ERROR   LOG(error)
+#define LOG_FATAL   LOG(fatal)
+
+namespace ndn {
+namespace ntorrent {
+
+struct LoggingUtil {
+  static void init();
+  // Initialize the log for the application. THis method must be called in the main function in
+  // the application before any logging may be performed.
+};
+
+} // end ntorrent
+} // end ndn
+#endif // UTIL_LOGGING_HPP
diff --git a/tests/unit-tests/boost-test.hpp b/tests/unit-tests/boost-test.hpp
index 149c310..a4eebca 100644
--- a/tests/unit-tests/boost-test.hpp
+++ b/tests/unit-tests/boost-test.hpp
@@ -26,8 +26,24 @@
 #pragma GCC system_header
 #pragma clang system_header
 
-#include <boost/test/unit_test.hpp>
+#include "util/logging.hpp"
+
 #include <boost/concept_check.hpp>
+#include <boost/log/utility/setup/common_attributes.hpp>
 #include <boost/test/output_test_stream.hpp>
+#include <boost/test/unit_test.hpp>
+
+
+struct NtorrentGlobalConfig {
+  NtorrentGlobalConfig() {
+    ndn::ntorrent::LoggingUtil::init();
+    boost::log::add_common_attributes();
+  }
+
+  ~NtorrentGlobalConfig() {
+  }
+};
+
+BOOST_GLOBAL_FIXTURE(NtorrentGlobalConfig);
 
 #endif // NTORRENT_TESTS_BOOST_TEST_HPP
diff --git a/wscript b/wscript
index a4c72b3..ba4a4f6 100644
--- a/wscript
+++ b/wscript
@@ -29,13 +29,13 @@
     conf.check_cfg(package='libndn-cxx', args=['--cflags', '--libs'],
                    uselib_store='NDN_CXX', mandatory=True)
 
-    boost_libs = 'system random thread filesystem'
+    boost_libs = 'system random thread filesystem log log_setup'
     if conf.options.with_tests:
         conf.env['WITH_TESTS'] = 1
         conf.define('WITH_TESTS', 1);
         boost_libs += ' unit_test_framework'
 
-    conf.check_boost(lib=boost_libs)
+    conf.check_boost(lib=boost_libs, mt=True)
     if conf.env.BOOST_VERSION_NUMBER < 104800:
         Logs.error("Minimum required boost version is 1.48.0")
         Logs.error("Please upgrade your distribution or install custom boost libraries" +
@@ -48,8 +48,6 @@
     conf.write_config_header('src/config.h')
 
 def build (bld):
-    feature_list = 'cxx'
-
     bld(
         features='cxx',
         name='nTorrent',
@@ -64,12 +62,7 @@
       target='ntorrent',
       features='cxx cxxprogram',
       source='src/main.cpp',
-      use = 'BOOST nTorrent')
-
-    if bld.env["WITH_TESTS"]:
-        feature_list += ' cxxstlib'
-    else:
-        feature_list += ' cxxprogram'
+      use = 'nTorrent')
 
     # Unit tests
     if bld.env["WITH_TESTS"]:
@@ -77,7 +70,7 @@
           target="unit-tests",
           source = bld.path.ant_glob(['tests/**/*.cpp']),
           features=['cxx', 'cxxprogram'],
-          use = 'BOOST nTorrent',
+          use = 'nTorrent',
           includes = "src .",
           install_path = None
           )