我正在从 gpsd 读取一些数据并使用 Python 的日志记录模块将其写入。我相当有信心只有一个进程写入这个文件,尽管我在这个记录器运行时使用 tail 读取它。每隔一段时间,我就会看到一些如下图所示的日志条目。我希望有人能阐明什么会导致(可能是 Python)将空控制字符插入我的日志文件。
我正在使用的代码是:
"""
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()