
from datetime import datetime
import os

logfile = None
logbuf = []

def init(analysis_path):
    global logfile
    if not os.path.isdir(analysis_path):
        log("logger: analysis_path missing:", analysis_path)
    use_log_dir = False
    if use_log_dir:
        logdir = os.path.join(analysis_path, "log")
        if not os.path.isdir(logdir):
            log("logger: creating log directory:", logdir)
            os.makedirs(logdir)
        logfile = os.path.join(logdir, "messages")
    else:
        logfile = os.path.join(analysis_path, "messages")

# log a message to messages files (and to stdout by default)
def log(*args, quiet=False, fancy=False):
    global logbuf
    # timestamp
    now = datetime.now()
    timestamp = str(now) + ": "
    # assemble message line
    msg = []
    for a in args:
        msg.append(str(a))
    if not fancy:
        logbuf.append(timestamp + " ".join(msg))
    else:
        logbuf.append("")
        logbuf.append("############################################################################")
        logbuf.append("### " + timestamp + " ".join(msg))
        logbuf.append("############################################################################")
        logbuf.append("")
    if logfile:
        # flush log buffer
        f = open(logfile, "a")
        for line in logbuf:
            f.write(line)
            f.write("\n")
        f.close()
        logbuf = []
    if not quiet:
        print(*msg)

# log quietly (log to file, but not to stdout)
def qlog(*args):
    log(*args, quiet=True)


from datetime import datetime
import os
import socket                   # gethostname()

logfile = None
logbuf = []

def init(analysis_path):
    global logfile
    if not os.path.isdir(analysis_path):
        log("logger: analysis_path missing:", analysis_path)
    logfile = os.path.join(analysis_path, "messages-" + socket.gethostname())

# log a message to messages files (and to stdout by default)
def log(*args, quiet=False, fancy=False):
    global logbuf
    # timestamp
    now = datetime.now()
    timestamp = str(now) + ": "
    # assemble message line
    msg = []
    for a in args:
        msg.append(str(a))
    if not fancy:
        logbuf.append(timestamp + " ".join(msg))
    else:
        logbuf.append("")
        logbuf.append("############################################################################")
        logbuf.append("### " + timestamp + " ".join(msg))
        logbuf.append("############################################################################")
        logbuf.append("")
    if logfile:
        # flush log buffer
        f = open(logfile, "a")
        for line in logbuf:
            f.write(line)
            f.write("\n")
        f.close()
        logbuf = []
    if not quiet:
        print(*msg)

# log quietly (log to file, but not to stdout)
def qlog(*args):
    log(*args, quiet=True)

Append hostname to messages file. This supports a use-case where portions of the processing could happen on various hosts.
Append hostname to messages file.  This supports a use-case where portions of
the processing could happen on various hosts.

  
  from datetime import datetime
  import os
+ import socket                   # gethostname()
  
  logfile = None
  logbuf = []
  
  def init(analysis_path):
      global logfile
      if not os.path.isdir(analysis_path):
          log("logger: analysis_path missing:", analysis_path)
-     use_log_dir = False
-     if use_log_dir:
-         logdir = os.path.join(analysis_path, "log")
-         if not os.path.isdir(logdir):
-             log("logger: creating log directory:", logdir)
-             os.makedirs(logdir)
-         logfile = os.path.join(logdir, "messages")
-     else:
-         logfile = os.path.join(analysis_path, "messages")
+     logfile = os.path.join(analysis_path, "messages-" + socket.gethostname())
  
  # log a message to messages files (and to stdout by default)
  def log(*args, quiet=False, fancy=False):
      global logbuf
      # timestamp
      now = datetime.now()
      timestamp = str(now) + ": "
      # assemble message line
      msg = []
      for a in args:
          msg.append(str(a))
      if not fancy:
          logbuf.append(timestamp + " ".join(msg))
      else:
          logbuf.append("")
          logbuf.append("############################################################################")
          logbuf.append("### " + timestamp + " ".join(msg))
          logbuf.append("############################################################################")
          logbuf.append("")
      if logfile:
          # flush log buffer
          f = open(logfile, "a")
          for line in logbuf:
              f.write(line)
              f.write("\n")
          f.close()
          logbuf = []
      if not quiet:
          print(*msg)
  
  # log quietly (log to file, but not to stdout)
  def qlog(*args):
      log(*args, quiet=True)
  
Append hostname to messages file. This supports a use-case where portions of the processing could happen on various hosts.
## Code Before:

from datetime import datetime
import os

logfile = None
logbuf = []

def init(analysis_path):
    global logfile
    if not os.path.isdir(analysis_path):
        log("logger: analysis_path missing:", analysis_path)
    use_log_dir = False
    if use_log_dir:
        logdir = os.path.join(analysis_path, "log")
        if not os.path.isdir(logdir):
            log("logger: creating log directory:", logdir)
            os.makedirs(logdir)
        logfile = os.path.join(logdir, "messages")
    else:
        logfile = os.path.join(analysis_path, "messages")

# log a message to messages files (and to stdout by default)
def log(*args, quiet=False, fancy=False):
    global logbuf
    # timestamp
    now = datetime.now()
    timestamp = str(now) + ": "
    # assemble message line
    msg = []
    for a in args:
        msg.append(str(a))
    if not fancy:
        logbuf.append(timestamp + " ".join(msg))
    else:
        logbuf.append("")
        logbuf.append("############################################################################")
        logbuf.append("### " + timestamp + " ".join(msg))
        logbuf.append("############################################################################")
        logbuf.append("")
    if logfile:
        # flush log buffer
        f = open(logfile, "a")
        for line in logbuf:
            f.write(line)
            f.write("\n")
        f.close()
        logbuf = []
    if not quiet:
        print(*msg)

# log quietly (log to file, but not to stdout)
def qlog(*args):
    log(*args, quiet=True)

## Instruction:
Append hostname to messages file. This supports a use-case where portions of the processing could happen on various hosts.
## Code After:

from datetime import datetime
import os
import socket                   # gethostname()

logfile = None
logbuf = []

def init(analysis_path):
    global logfile
    if not os.path.isdir(analysis_path):
        log("logger: analysis_path missing:", analysis_path)
    logfile = os.path.join(analysis_path, "messages-" + socket.gethostname())

# log a message to messages files (and to stdout by default)
def log(*args, quiet=False, fancy=False):
    global logbuf
    # timestamp
    now = datetime.now()
    timestamp = str(now) + ": "
    # assemble message line
    msg = []
    for a in args:
        msg.append(str(a))
    if not fancy:
        logbuf.append(timestamp + " ".join(msg))
    else:
        logbuf.append("")
        logbuf.append("############################################################################")
        logbuf.append("### " + timestamp + " ".join(msg))
        logbuf.append("############################################################################")
        logbuf.append("")
    if logfile:
        # flush log buffer
        f = open(logfile, "a")
        for line in logbuf:
            f.write(line)
            f.write("\n")
        f.close()
        logbuf = []
    if not quiet:
        print(*msg)

# log quietly (log to file, but not to stdout)
def qlog(*args):
    log(*args, quiet=True)
