log messages to stderr instead of stdout

changed the log output from stdout to stderr. and refactor the error
handling logic, this is prepare for adding new features, like batch
processing.