Better logging, also for client

This commit is contained in:
Ruben van de Ven 2019-04-10 11:13:42 +02:00
parent bb22dffd5c
commit 837e19d5e7
5 changed files with 36 additions and 16 deletions

View file

@ -509,7 +509,8 @@ class HugveyState(object):
self.streamer = AudioStreamer( self.streamer = AudioStreamer(
self.command.config['voice']['chunk'], self.command.config['voice']['chunk'],
self.ip, self.ip,
int(self.command.config['voice']['port']) + self.id) int(self.command.config['voice']['port']) + self.id,
self.id)
if self.command.config['voyeur']: if self.command.config['voyeur']:
self.logger.warn("Debug on: Connecting Audio player") self.logger.warn("Debug on: Connecting Audio player")

View file

@ -24,6 +24,10 @@ import subprocess
logger = logging.getLogger("client") logger = logging.getLogger("client")
def setLogger(hv_id):
global logger
logger = logging.getLogger("hugvey").getChild("{}".format(hv_id)).getChild("client")
class VoiceServer(object): class VoiceServer(object):
"""A UDP server, providing mic data at 16 kHz""" """A UDP server, providing mic data at 16 kHz"""
@ -283,7 +287,8 @@ class Hugvey(object):
def __init__(self, id = None): def __init__(self, id = None):
self.id = self.getId() if id is None else id self.id = self.getId() if id is None else id
logger.debug('Hugvey {}'.format(self.id)) setLogger(self.id)
logger.debug('Hugvey {}, reporting'.format(self.id))
def getId(self) -> int: def getId(self) -> int:
"""Get Hugvey ID from hostname""" """Get Hugvey ID from hostname"""

View file

@ -17,6 +17,8 @@ class Recorder:
""" """
def __init__(self, hv_id, src_rate, out_folder): def __init__(self, hv_id, src_rate, out_folder):
self.logger = mainLogger.getChild(f"{hv_id}").getChild('recorder')
if not os.path.exists(out_folder): if not os.path.exists(out_folder):
raise Exception(f"Invalid output folder for recordings: {out_folder}") raise Exception(f"Invalid output folder for recordings: {out_folder}")
@ -32,11 +34,11 @@ class Recorder:
self.data = array('h') self.data = array('h')
self.currentTranscription = "" self.currentTranscription = ""
t = time.strftime("%Y%m%d-%H%M") t = time.strftime("%Y%m%d-%H%M%s")
self.out_folder = os.path.join(self.main_folder, f"{self.hv_id}", t) self.out_folder = os.path.join(self.main_folder, f"{self.hv_id}", t)
if not os.path.exists(self.out_folder): if not os.path.exists(self.out_folder):
logger.debug(f"Create directory {self.out_folder}") self.logger.debug(f"Create directory {self.out_folder}")
self.target_folder = os.makedirs(self.out_folder, exist_ok=True) self.target_folder = os.makedirs(self.out_folder, exist_ok=True)
self.running = True self.running = True
@ -44,11 +46,16 @@ class Recorder:
def writeData(self): def writeData(self):
if len(self.data) < 1: if len(self.data) < 1:
self.logger.info("Skip empty wave creation")
return return
self.fragmentNr += 1 self.fragmentNr += 1
self.wf = wave.open(os.path.join(self.out_folder, f"{self.fragmentNr}.wav"), 'wb') fn = os.path.join(self.out_folder, f"{self.fragmentNr}.wav")
self.logger.info(f"Write wave: {fn}")
self.wf = wave.open(fn, 'wb')
self.wf.setnchannels(1) self.wf.setnchannels(1)
self.wf.setsampwidth(2) self.wf.setsampwidth(2)
self.wf.setframerate(self.src_rate) self.wf.setframerate(self.src_rate)
@ -67,15 +74,13 @@ class Recorder:
if not self.running: if not self.running:
return return
# logger.debug('receive {}'.format(len(chunk))) # self.logger.debug('receive {}'.format(len(chunk)))
if audioop.max(chunk, 2) == 0: if audioop.max(chunk, 2) == 0:
# mic is muted on client side. # mic is muted on client side.
self.subsequentMutedFrames += 1 self.subsequentMutedFrames += 1
logger.warn(f"Empty frame {self.subsequentMutedFrames}") #self.logger.debug(f"Empty frame {self.subsequentMutedFrames}")
# self.logger.debug("Muted")
if self.subsequentMutedFrames == 4: if self.subsequentMutedFrames == 4:
logger.warn("Create new wave!")
# self.createWave() # self.createWave()
self.writeData() self.writeData()
if self.subsequentMutedFrames > 4: if self.subsequentMutedFrames > 4:

View file

@ -10,7 +10,9 @@ mainLogger = logging.getLogger("hugvey")
logger = mainLogger.getChild("streamer") logger = mainLogger.getChild("streamer")
class AudioStreamer(object): class AudioStreamer(object):
def __init__(self, chunk, address: str, port: int): def __init__(self, chunk, address: str, port: int, hv_id: int):
self.logger = mainLogger.getChild(f"{hv_id}").getChild('streamer')
self.consumers = [] self.consumers = []
self.chunk = chunk self.chunk = chunk
@ -34,18 +36,18 @@ class AudioStreamer(object):
self.socket.connect(address) self.socket.connect(address)
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logger.info("Attempt connection on {}:{}".format(self.address, self.port)) self.logger.info("Attempt connection on {}:{}".format(self.address, self.port))
# s.connect((self.address, self.port)) # s.connect((self.address, self.port))
# #
try: try:
while self.isRunning: while self.isRunning:
data = await self.socket.recv() data = await self.socket.recv()
# logger.debug('chunk received') # self.logger.debug('chunk received')
self.process(data) self.process(data)
except zmq.error.Again as timeout_e: except zmq.error.Again as timeout_e:
logger.warn("Timeout of audiostream. Hugvey shutdown?") self.logger.warn("Timeout of audiostream. Hugvey shutdown?")
finally: finally:
logger.info("Close socket on {}:{}".format(self.address, self.port)) self.logger.info("Close socket on {}:{}".format(self.address, self.port))
self.socket.close() self.socket.close()
def stop(self): def stop(self):
@ -56,7 +58,7 @@ class AudioStreamer(object):
def process(self, chunk): def process(self, chunk):
# logger.debug("Received chunk") # self.logger.debug("Received chunk")
for consumer in self.consumers: for consumer in self.consumers:
consumer.receive(chunk) consumer.receive(chunk)

View file

@ -1,5 +1,7 @@
from hugvey.client import Hugvey from hugvey.client import Hugvey
import coloredlogs, logging import logging
import logging.handlers
import coloredlogs
import argparse import argparse
if __name__ == '__main__': if __name__ == '__main__':
@ -28,6 +30,11 @@ if __name__ == '__main__':
coloredlogs.install( coloredlogs.install(
level=logging.DEBUG if args.verbose else logging.INFO, level=logging.DEBUG if args.verbose else logging.INFO,
) )
logger = logging.getLogger("hugvey")
# logger.setLevel(1) # to send all records to cutelog
socket_handler = logging.handlers.SocketHandler('hugveycmd.local', 19996) # default listening address
logger.addHandler(socket_handler);
hv = Hugvey(args.id) hv = Hugvey(args.id)
hv.loadConfig(args.config) hv.loadConfig(args.config)