22import dataclasses
33import json
44import logging
5- import logging .handlers
65import sys
76from enum import Enum
87from typing import Any , Dict , List , Optional , Set , Union
1615from galaxy .task_manager import TaskManager
1716
1817
18+ logger = logging .getLogger (__name__ )
19+
20+
1921class JSONEncoder (json .JSONEncoder ):
2022 def default (self , o ): # pylint: disable=method-hidden
2123 if dataclasses .is_dataclass (o ):
@@ -33,7 +35,7 @@ class Plugin:
3335 """Use and override methods of this class to create a new platform integration."""
3436
3537 def __init__ (self , platform , version , reader , writer , handshake_token ):
36- logging .info ("Creating plugin for platform %s, version %s" , platform .value , version )
38+ logger .info ("Creating plugin for platform %s, version %s" , platform .value , version )
3739 self ._platform = platform
3840 self ._version = version
3941
@@ -189,24 +191,24 @@ async def wrapper(*args, **kwargs):
189191 async def run (self ):
190192 """Plugin's main coroutine."""
191193 await self ._connection .run ()
192- logging .debug ("Plugin run loop finished" )
194+ logger .debug ("Plugin run loop finished" )
193195
194196 def close (self ) -> None :
195197 if not self ._active :
196198 return
197199
198- logging .info ("Closing plugin" )
200+ logger .info ("Closing plugin" )
199201 self ._connection .close ()
200202 self ._external_task_manager .cancel ()
201203 self ._internal_task_manager .create_task (self .shutdown (), "shutdown" )
202204 self ._active = False
203205
204206 async def wait_closed (self ) -> None :
205- logging .debug ("Waiting for plugin to close" )
207+ logger .debug ("Waiting for plugin to close" )
206208 await self ._external_task_manager .wait ()
207209 await self ._internal_task_manager .wait ()
208210 await self ._connection .wait_closed ()
209- logging .debug ("Plugin closed" )
211+ logger .debug ("Plugin closed" )
210212
211213 def create_task (self , coro , description ):
212214 """Wrapper around asyncio.create_task - takes care of canceling tasks on shutdown"""
@@ -217,11 +219,11 @@ async def _pass_control(self):
217219 try :
218220 self .tick ()
219221 except Exception :
220- logging .exception ("Unexpected exception raised in plugin tick" )
222+ logger .exception ("Unexpected exception raised in plugin tick" )
221223 await asyncio .sleep (1 )
222224
223225 async def _shutdown (self ):
224- logging .info ("Shutting down" )
226+ logger .info ("Shutting down" )
225227 self .close ()
226228 await self ._external_task_manager .wait ()
227229 await self ._internal_task_manager .wait ()
@@ -238,7 +240,7 @@ def _initialize_cache(self, data: Dict):
238240 try :
239241 self .handshake_complete ()
240242 except Exception :
241- logging .exception ("Unhandled exception during `handshake_complete` step" )
243+ logger .exception ("Unhandled exception during `handshake_complete` step" )
242244 self ._internal_task_manager .create_task (self ._pass_control (), "tick" )
243245
244246 @staticmethod
@@ -639,7 +641,7 @@ async def import_game_achievements(game_id, context_):
639641 except ApplicationError as error :
640642 self ._game_achievements_import_failure (game_id , error )
641643 except Exception :
642- logging .exception ("Unexpected exception raised in import_game_achievements" )
644+ logger .exception ("Unexpected exception raised in import_game_achievements" )
643645 self ._game_achievements_import_failure (game_id , UnknownError ())
644646
645647 async def import_games_achievements (game_ids_ , context_ ):
@@ -803,7 +805,7 @@ async def import_game_time(game_id, context_):
803805 except ApplicationError as error :
804806 self ._game_time_import_failure (game_id , error )
805807 except Exception :
806- logging .exception ("Unexpected exception raised in import_game_time" )
808+ logger .exception ("Unexpected exception raised in import_game_time" )
807809 self ._game_time_import_failure (game_id , UnknownError ())
808810
809811 async def import_game_times (game_ids_ , context_ ):
@@ -861,7 +863,7 @@ async def import_game_library_settings(game_id, context_):
861863 except ApplicationError as error :
862864 self ._game_library_settings_import_failure (game_id , error )
863865 except Exception :
864- logging .exception ("Unexpected exception raised in import_game_library_settings" )
866+ logger .exception ("Unexpected exception raised in import_game_library_settings" )
865867 self ._game_library_settings_import_failure (game_id , UnknownError ())
866868
867869 async def import_game_library_settings_set (game_ids_ , context_ ):
@@ -919,7 +921,7 @@ async def import_os_compatibility(game_id, context_):
919921 except ApplicationError as error :
920922 self ._os_compatibility_import_failure (game_id , error )
921923 except Exception :
922- logging .exception ("Unexpected exception raised in import_os_compatibility" )
924+ logger .exception ("Unexpected exception raised in import_os_compatibility" )
923925 self ._os_compatibility_import_failure (game_id , UnknownError ())
924926
925927 async def import_os_compatibility_set (game_ids_ , context_ ):
@@ -974,7 +976,7 @@ async def import_user_presence(user_id, context_) -> None:
974976 except ApplicationError as error :
975977 self ._user_presence_import_failure (user_id , error )
976978 except Exception :
977- logging .exception ("Unexpected exception raised in import_user_presence" )
979+ logger .exception ("Unexpected exception raised in import_user_presence" )
978980 self ._user_presence_import_failure (user_id , UnknownError ())
979981
980982 async def import_user_presence_set (user_ids_ , context_ ) -> None :
@@ -1037,29 +1039,29 @@ def main():
10371039 main()
10381040 """
10391041 if len (argv ) < 3 :
1040- logging .critical ("Not enough parameters, required: token, port" )
1042+ logger .critical ("Not enough parameters, required: token, port" )
10411043 sys .exit (1 )
10421044
10431045 token = argv [1 ]
10441046
10451047 try :
10461048 port = int (argv [2 ])
10471049 except ValueError :
1048- logging .critical ("Failed to parse port value: %s" , argv [2 ])
1050+ logger .critical ("Failed to parse port value: %s" , argv [2 ])
10491051 sys .exit (2 )
10501052
10511053 if not (1 <= port <= 65535 ):
1052- logging .critical ("Port value out of range (1, 65535)" )
1054+ logger .critical ("Port value out of range (1, 65535)" )
10531055 sys .exit (3 )
10541056
10551057 if not issubclass (plugin_class , Plugin ):
1056- logging .critical ("plugin_class must be subclass of Plugin" )
1058+ logger .critical ("plugin_class must be subclass of Plugin" )
10571059 sys .exit (4 )
10581060
10591061 async def coroutine ():
10601062 reader , writer = await asyncio .open_connection ("127.0.0.1" , port )
10611063 extra_info = writer .get_extra_info ("sockname" )
1062- logging .info ("Using local address: %s:%u" , * extra_info )
1064+ logger .info ("Using local address: %s:%u" , * extra_info )
10631065 async with plugin_class (reader , writer , token ) as plugin :
10641066 await plugin .run ()
10651067
@@ -1069,5 +1071,5 @@ async def coroutine():
10691071
10701072 asyncio .run (coroutine ())
10711073 except Exception :
1072- logging .exception ("Error while running plugin" )
1074+ logger .exception ("Error while running plugin" )
10731075 sys .exit (5 )
0 commit comments