音频错误自定义非阻塞texttospeech处理程序,用于Python的日志记录modu

2024-10-08 18:31:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在建立一个屏幕无树莓皮为基础的长期艺术装置。 Text-to-Speech on the Raspberry pi的一个常见应用是,人们配置他们的pi在引导时说出他们的IP地址,以简化SSHing。你知道吗

我渐渐喜欢上了日志模块。再也不评论数不清的文字陈述让我心潮澎湃。在我的情况下,最好有一个能说出错误消息的日志处理程序。我目前使用的StreamHandler和FileHandler对于开发和问题后诊断非常有用,但是对于就地解决问题却非常糟糕。另外,我的机器人对我大喊大叫也有一些令人愉快的科幻故事。你知道吗

I found an example of a custom handler for SMS-based error logging,并尝试用e-speak实现我自己的。它会说话,但整个程序会在第一句话的结尾处停止。你知道吗

我正在寻找关于如何实现一个流处理程序,不阻止程序执行的建议。 我的坏的自定义处理程序:

import logging
import os

#based on SMSHandler http://pantburk.info/?blog=77

def speak(stringToSay):
    '''say whatver it is told to say, squleching annoying warnings'''
    stringToSay = "'"+stringToSay+"'"
#English female voice, emphasis on capitals (-k), speaking slowly (-s) using direct text:-
#the 2>/dev/null' is there because any calls to the rPi audio card result in a dozen warnings.
#  see: http://raspberrypi.stackexchange.com/questions/3412/errors-with-espeak
    os.system('espeak  -ven+f3 -k5 -s150 '+stringToSay+' 2>/dev/null')

class TALKHandler(logging.Handler): # Inherit from logging.Handler
        def __init__(self):
                # run the regular Handler __init__
                logging.Handler.__init__(self)
        def emit(self, record):
                # record.message is the log message
                speak(record.message)

正在记录的程序片段:

logging.handlers.TALKHandler = speechHandler.TALKHandler
# create the handler object
talkingHandler = logging.handlers.TALKHandler()
# Configure the handler to only send SMS for critical errors
talkingHandler.setLevel(logging.CRITICAL)
# and finally we add the handler to the logging object
logger.addHandler(talkingHandler)

ipAddress = [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
ipAddress = " ".join(ipAddress)
ipAddress = ipAddress.replace(".","dot")
logger.critical("Current IP Address is " + ipAddress  )

Tags: theto处理程序forisonloggingdef
1条回答
网友
1楼 · 发布于 2024-10-08 18:31:14

我知道这是很久以前的问题了-我错过了当它被张贴和偶然发现它。以下是我的作品:

import logging
import subprocess
import sys

class TalkHandler(logging.Handler):
    def emit(self, record):
        msg = self.format(record)
        cmd = ['espeak', '-ven+f3', '-s150', msg]
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        p.communicate()

def configure_logging():
    h = TalkHandler()
    root = logging.getLogger()
    root.addHandler(h)
    root.setLevel(logging.DEBUG)

def main():
    logging.info('Hello')
    logging.debug('Goodbye')

if __name__ == '__main__':
    configure_logging()
    sys.exit(main())

当我运行它,我听到“你好”,然后说“再见”。你知道吗

相关问题 更多 >

    热门问题