1

I am reading some data from gpsd and writing it using using Python's logging module. I am fairly confident that only one process writes to this file, although I read from it using tail while this logger is running. Every once in a while I am seeing some log entries that look like the image below. I am hoping that somebody can shed some light on what would cause (presumably Python) to insert null control characters into my log file.

enter image description here

The code I am using is:

"""
Read the GPS continuously
"""
import logging
from logging.handlers import RotatingFileHandler
import sys
import gps

LOG = '/sensor_logs/COUNT.csv'

def main():
    log_formatter = logging.Formatter('%(asctime)s,%(message)s', "%Y-%m-%d %H:%M:%S")

    my_handler = RotatingFileHandler(LOG, mode='a', maxBytes=1024*1024,
                                     backupCount=1, encoding=None, delay=0)
    my_handler.setFormatter(log_formatter)
    my_handler.setLevel(logging.INFO)

    app_log = logging.getLogger('root')
    app_log.setLevel(logging.INFO)
    app_log.addHandler(my_handler)

    session = gps.gps("localhost", "2947")
    session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

    while True:

        try:
            report = session.next()
            if hasattr(report, 'gdop'):
                satcount = 0
                for s in report['satellites']:
                    if s['used'] == True:
                        satcount+=1
                data = "{}".format(str(satcount))
                app_log.info(data)

        except KeyError:
            pass
        except KeyboardInterrupt:
            quit()
        except StopIteration:
            session = None

if __name__ == "__main__":
    main()
a2f0
  • 1,615
  • 2
  • 19
  • 28
  • I only see the one command that write to the log. So I'm not sure where those bytes are coming from. Is it possible that you have another job writting to the same logger? What happens if you change the name of the log to something other than 'root' and to a different filename just in case? – RobertB Sep 08 '17 at 18:13
  • I appreciate you getting back to me, @RobertB. I can try what you suggest. This bug is difficult to reproduce. It will be a while before I am able to dig into this again. I will report back if and when I figure out what is causing the issue I described. – a2f0 Sep 11 '17 at 16:04

1 Answers1

0

This ended up being a result of failing hardware. Thank you for your help, @RobertB.

a2f0
  • 1,615
  • 2
  • 19
  • 28