<s> from __future__ import print_function <EOL> import logging <EOL> import collections <EOL> import zmq . green as zmq <EOL> from lymph . cli . base import Command <EOL> from lymph . client import Client <EOL> from lymph . core import services <EOL> from lymph . utils . logging import get_loglevel <EOL> class RemoteTail ( collections . Iterator ) : <EOL> """<STR_LIT>""" <EOL> Entry = collections . namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , ctx = None ) : <EOL> if ctx is None : <EOL> ctx = zmq . Context . instance ( ) <EOL> self . _sock = ctx . socket ( zmq . SUB ) <EOL> self . _sock . setsockopt_string ( zmq . SUBSCRIBE , u'<STR_LIT>' ) <EOL> self . _instances = { } <EOL> @ property <EOL> def instances ( self ) : <EOL> return self . _instances <EOL> def _on_status_change ( self , instance , action ) : <EOL> """<STR_LIT>""" <EOL> if action == services . ADDED : <EOL> self . _connect ( instance ) <EOL> elif action == services . REMOVED : <EOL> self . _disconnect ( instance ) <EOL> def _connect ( self , instance ) : <EOL> self . _sock . connect ( instance . log_endpoint ) <EOL> self . _instances [ instance . log_endpoint ] = instance <EOL> def _disconnect ( self , instance ) : <EOL> self . _sock . disconnect ( instance . log_endpoint ) <EOL> del self . _instances [ instance . log_endpoint ] <EOL> def subscribe_service ( self , service ) : <EOL> """<STR_LIT>""" <EOL> service . observe ( [ services . ADDED , services . REMOVED ] , self . _on_status_change ) <EOL> connected = False <EOL> for instance in service : <EOL> if instance . log_endpoint : <EOL> self . _connect ( instance ) <EOL> connected = True <EOL> return connected <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> topic , endpoint , msg = self . _sock . recv_multipart ( ) <EOL> return self . Entry ( topic , self . _instances [ endpoint ] , msg ) <EOL> __next__ = next <EOL> class TailCommand ( Command ) : <EOL> """<STR_LIT>""" <EOL> short_description = '<STR_LIT>' <EOL> def run ( self ) : <EOL> client = Client . from_config ( self . config ) <EOL> tail = RemoteTail ( ) <EOL> for address in self . args [ '<STR_LIT>' ] : <EOL> connected = tail . subscribe_service ( client . container . lookup ( address ) ) <EOL> if not connected : <EOL> print ( "<STR_LIT>" % address ) <EOL> if not tail . instances : <EOL> return <NUM_LIT:1> <EOL> level = get_loglevel ( self . args [ '<STR_LIT>' ] ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . setLevel ( level ) <EOL> console = logging . StreamHandler ( ) <EOL> console . setLevel ( level ) <EOL> console . setFormatter ( logging . Formatter ( '<STR_LIT>' ) ) <EOL> logger . addHandler ( console ) <EOL> try : <EOL> for topic , instance , msg in tail : <EOL> level = getattr ( logging , topic ) <EOL> extra = { <EOL> '<STR_LIT>' : instance . identity [ : <NUM_LIT:10> ] , <EOL> '<STR_LIT>' : instance . endpoint , <EOL> } <EOL> logger . log ( level , msg , extra = extra ) <EOL> except KeyboardInterrupt : <EOL> pass </s>
