Update devserver logging to utilize --log-dir.

Since not everyone wants to log files and depending on if the devserver
is run inside or outside of the chroot it may cause problems, if we assume
we can write in the directory where dev server is installed add a --log-dir
option. If specified dump access and error logs in to that directory.

TEST=Ran with --log-dir and a valid dir to ensure files were logged,
Ran --log-dir without a valid dir to ensure error.
Ran without --log-dir to ensure no log files were created.
BUG=chromium-os:26870

Change-Id: I2ebd946be61e4caf33cdb0769e262cee59eb85f1
Reviewed-on: https://gerrit.chromium.org/gerrit/16815
Reviewed-by: Chris Masone <cmasone@chromium.org>
Reviewed-by: Mark Hayter <mdhayter@chromium.org>
Reviewed-by: Scott Zawalski <scottz@chromium.org>
Tested-by: Scott Zawalski <scottz@chromium.org>
Commit-Ready: Scott Zawalski <scottz@chromium.org>
diff --git a/devserver.py b/devserver.py
index e75f31e..dc02a93 100755
--- a/devserver.py
+++ b/devserver.py
@@ -60,9 +60,6 @@
 
 def _GetConfig(options):
   """Returns the configuration for the devserver."""
-  base_path = os.path.dirname(__file__)
-  access_log = os.path.join(base_path, 'devserver_access.log')
-  error_log = os.path.join(base_path, 'devserver_error.log')
   base_config = { 'global':
                   { 'server.log_request_headers': True,
                     'server.protocol_version': 'HTTP/1.1',
@@ -72,8 +69,6 @@
                     'response.timeout': 6000,
                     'tools.staticdir.root':
                       os.path.dirname(os.path.abspath(sys.argv[0])),
-                    'log.access_file': access_log,
-                    'log.error_file': error_log,
                   },
                   '/api':
                   {
@@ -97,6 +92,13 @@
                     'response.timeout': 10000,
                   },
                 }
+
+  if options.log_dir:
+    base_config['global']['log.access_file'] = os.path.join(
+        options.log_dir, 'devserver_access.log')
+    base_config['global']['log.error_file'] = os.path.join(
+        options.log_dir, 'devserver_error.log')
+
   if options.production:
     base_config['global']['server.environment'] = 'production'
 
@@ -307,8 +309,9 @@
   parser.add_option('--validate_factory_config', action="store_true",
                     dest='validate_factory_config',
                     help='Validate factory config file, then exit.')
-  parser.add_option('-l', '--logging', action="store_true", default=False,
-                    help='Enable logging and reporting of update processes.')
+  parser.add_option('-l', '--log-dir', default=None,
+                    help=('Specify a directory for error and access logs. ',
+                          'Default None, i.e. no logging.'))
   (options, _) = parser.parse_args()
 
   devserver_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
@@ -318,6 +321,10 @@
   static_dir = os.path.realpath('%s/static' % options.data_dir)
   os.system('mkdir -p %s' % static_dir)
 
+  if options.log_dir and not os.path.isdir(options.log_dir):
+    parser.error('%s is not a valid dir, provide a valid dir to --log-dir' %
+                 options.log_dir)
+
   if options.archive_dir:
   # TODO(zbehan) Remove legacy support:
   #  archive_dir is the directory where static/archive will point.