Fix thread safe logging.
This commit is contained in:
parent
76b7895e11
commit
1bef7a67d3
@ -11,21 +11,32 @@ import subprocess
|
|||||||
import argparse
|
import argparse
|
||||||
import threading
|
import threading
|
||||||
import socketserver
|
import socketserver
|
||||||
|
import sys
|
||||||
from http.server import BaseHTTPRequestHandler
|
from http.server import BaseHTTPRequestHandler
|
||||||
from threading import Event
|
from threading import Event
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from threading import Lock
|
||||||
|
|
||||||
|
xprint_lock = Lock()
|
||||||
|
|
||||||
|
def xprint(*args, **kwargs):
|
||||||
|
"""Thread safe print function"""
|
||||||
|
with xprint_lock:
|
||||||
|
print(*args, **kwargs)
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Sensors polling and metrics recording.')
|
parser = argparse.ArgumentParser(description='Sensors polling and metrics recording.')
|
||||||
parser.add_argument("-v", "--verbosity", help="Increase output verbosity",
|
parser.add_argument("-v", "--verbosity", help="Increase output verbosity",
|
||||||
type=str, choices=['DEBUG', 'INFO', 'WARNING'], default='INFO')
|
type=str, choices=['DEBUG', 'INFO', 'WARNING'], default='INFO')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.verbosity == 'DEBUG':
|
verbosity = args.verbosity
|
||||||
|
if verbosity == 'DEBUG':
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
elif args.verbosity == 'INFO':
|
elif verbosity == 'INFO':
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
elif args.verbosity == 'WARNING':
|
elif verbosity == 'WARNING':
|
||||||
logging.basicConfig(level=logging.WARNING)
|
logging.basicConfig(level=logging.WARNING)
|
||||||
|
|
||||||
logging.info("====== Starting ======")
|
logging.info("====== Starting ======")
|
||||||
@ -146,6 +157,13 @@ class MyHandler(BaseHTTPRequestHandler):
|
|||||||
self.wfile.write(bytes(json.dumps(last_data[self.path[1:]])+'\n', 'utf-8'))
|
self.wfile.write(bytes(json.dumps(last_data[self.path[1:]])+'\n', 'utf-8'))
|
||||||
else:
|
else:
|
||||||
self.send_response(404)
|
self.send_response(404)
|
||||||
|
# This rewrites the BaseHTTP logging function
|
||||||
|
def log_message(self, format, *args):
|
||||||
|
if verbosity == 'INFO':
|
||||||
|
xprint("%s - - [%s] %s" %
|
||||||
|
(self.address_string(),
|
||||||
|
self.log_date_time_string(),
|
||||||
|
format%args))
|
||||||
|
|
||||||
class WebThread(threading.Thread):
|
class WebThread(threading.Thread):
|
||||||
def run(self):
|
def run(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user