# Path: osbrain/agent.py
# def run_agent(
#     name='',
#     nsaddr=None,
#     addr=None,
#     base=Agent,
#     serializer=None,
#     transport=None,
#     safe=None,
#     attributes=None,
# ):
#     """
#     Ease the agent creation process.
# 
#     This function will create a new agent, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str, default is ''
#         Agent name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New agent address, if it is to be fixed.
#     transport : str, AgentAddressTransport, default is None
#         Transport protocol.
#     safe : bool, default is None
#         Use safe calls by default from the Proxy.
#     attributes : dict, default is None
#         A dictionary that defines initial attributes for the agent.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new agent.
#     """
#     if not nsaddr:
#         nsaddr = os.environ.get('OSBRAIN_NAMESERVER_ADDRESS')
#     agent = AgentProcess(
#         name=name,
#         nsaddr=nsaddr,
#         addr=addr,
#         base=base,
#         serializer=serializer,
#         transport=transport,
#         attributes=attributes,
#     )
#     agent_name = agent.start()
# 
#     proxy = Proxy(agent_name, nsaddr, safe=safe)
#     proxy.run()
#     proxy.wait_for_running()
# 
#     return proxy
# 
# Path: osbrain/logging.py
# def run_logger(name, nsaddr=None, addr=None, base=Logger):
#     """
#     Ease the logger creation process.
# 
#     This function will create a new logger, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str
#         Logger name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New logger address, if it is to be fixed.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new logger.
#     """
#     return run_agent(name, nsaddr, addr, base)
# 
# Path: osbrain/helper.py
# def logger_received(
#     logger, message, log_name='log_history_info', position=None, timeout=1.0
# ):
#     """
#     Check if a logger receives a message.
# 
#     Parameters
#     ----------
#     logger : Proxy
#         Proxy to the logger.
#     log_name : str, default is `'log_history_info'`
#         The name of the attribute to look for in the logger.
#     message : anything
#         Message to look for in the log. Can be a partial match. Regular
#         expressions are allowed.
#     position : int, default is None
#         Where to look for the message in the log. If not set, the message
#         will be searched for through all the log.
#     timeout : float
#         After this number of seconds the function will return `False`.
# 
#     Returns
#     -------
#     int
#         The number of times the logger received a message that matched the
#         expression. Can be higher than 1 if `position` is not set.
#     """
#     t0 = time.time()
#     while True:
#         time.sleep(0.01)
#         log_history = logger.get_attr(log_name)
#         if position is not None:
#             log_history = [log_history[position]]
#         matches = regex_count_in_list(message, log_history)
#         if matches:
#             return matches
#         if timeout and time.time() - t0 > timeout:
#             return 0
# 
# Path: osbrain/logging.py
# def pyro_log():
#     """
#     Set environment variables to activate Pyro logging. The log level is
#     set to "DEBUG".
#     """
#     os.environ['PYRO_LOGFILE'] = 'pyro_osbrain.log'
#     os.environ['PYRO_LOGLEVEL'] = 'DEBUG'


import os
from osbrain import run_agent
from osbrain import run_logger
from osbrain.helper import logger_received
from osbrain.logging import pyro_log
"""
Logging module tests.
"""



def test_logging_level(nsproxy):
    """
    Logging with different log levels should result in different logs being
    filled.
    """
    agent = run_agent('agent')
    agent.set_attr(_DEBUG=True)
    logger = run_logger('logger')
    agent.set_logger(logger)
    i = 0
    while not len(logger.get_attr('log_history')):
        i += 1
        agent.log_info(i)

# Path: osbrain/agent.py
# def run_agent(
#     name='',
#     nsaddr=None,
#     addr=None,
#     base=Agent,
#     serializer=None,
#     transport=None,
#     safe=None,
#     attributes=None,
# ):
#     """
#     Ease the agent creation process.
# 
#     This function will create a new agent, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str, default is ''
#         Agent name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New agent address, if it is to be fixed.
#     transport : str, AgentAddressTransport, default is None
#         Transport protocol.
#     safe : bool, default is None
#         Use safe calls by default from the Proxy.
#     attributes : dict, default is None
#         A dictionary that defines initial attributes for the agent.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new agent.
#     """
#     if not nsaddr:
#         nsaddr = os.environ.get('OSBRAIN_NAMESERVER_ADDRESS')
#     agent = AgentProcess(
#         name=name,
#         nsaddr=nsaddr,
#         addr=addr,
#         base=base,
#         serializer=serializer,
#         transport=transport,
#         attributes=attributes,
#     )
#     agent_name = agent.start()
# 
#     proxy = Proxy(agent_name, nsaddr, safe=safe)
#     proxy.run()
#     proxy.wait_for_running()
# 
#     return proxy
# 
# Path: osbrain/logging.py
# def run_logger(name, nsaddr=None, addr=None, base=Logger):
#     """
#     Ease the logger creation process.
# 
#     This function will create a new logger, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str
#         Logger name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New logger address, if it is to be fixed.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new logger.
#     """
#     return run_agent(name, nsaddr, addr, base)
# 
# Path: osbrain/helper.py
# def logger_received(
#     logger, message, log_name='log_history_info', position=None, timeout=1.0
# ):
#     """
#     Check if a logger receives a message.
# 
#     Parameters
#     ----------
#     logger : Proxy
#         Proxy to the logger.
#     log_name : str, default is `'log_history_info'`
#         The name of the attribute to look for in the logger.
#     message : anything
#         Message to look for in the log. Can be a partial match. Regular
#         expressions are allowed.
#     position : int, default is None
#         Where to look for the message in the log. If not set, the message
#         will be searched for through all the log.
#     timeout : float
#         After this number of seconds the function will return `False`.
# 
#     Returns
#     -------
#     int
#         The number of times the logger received a message that matched the
#         expression. Can be higher than 1 if `position` is not set.
#     """
#     t0 = time.time()
#     while True:
#         time.sleep(0.01)
#         log_history = logger.get_attr(log_name)
#         if position is not None:
#             log_history = [log_history[position]]
#         matches = regex_count_in_list(message, log_history)
#         if matches:
#             return matches
#         if timeout and time.time() - t0 > timeout:
#             return 0
# 
# Path: osbrain/logging.py
# def pyro_log():
#     """
#     Set environment variables to activate Pyro logging. The log level is
#     set to "DEBUG".
#     """
#     os.environ['PYRO_LOGFILE'] = 'pyro_osbrain.log'
#     os.environ['PYRO_LOGLEVEL'] = 'DEBUG'


# Path: osbrain/tests/test_logging.py
import os
from osbrain import run_agent
from osbrain import run_logger
from osbrain.helper import logger_received
from osbrain.logging import pyro_log

"""
Logging module tests.
"""



def test_logging_level(nsproxy):
    """
    Logging with different log levels should result in different logs being
    filled.
    """
    agent = run_agent('agent')
    agent.set_attr(_DEBUG=True)
    logger = run_logger('logger')
    agent.set_logger(logger)
    i = 0
    while not len(logger.get_attr('log_history')):
        i += 1
        agent.log_info(i)

    assert logger_received(logger, message=r'\): %s' % i, position=-1)