Skip to content

Commit

Permalink
Use own logger (#34)
Browse files Browse the repository at this point in the history
* Fix type hint

* Use own logger

* rm test

* pep8
  • Loading branch information
joente authored Jul 17, 2024
1 parent c4f9482 commit 2d2af41
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ async def main():
print('done in ', end-start)

if __name__ == '__main__':
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

formatter = logging.Formatter(
fmt='[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d] ' +
'%(message)s',
datefmt='%y%m%d %H:%M:%S',
style='%')

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)

logger = logging.getLogger('aiowmi')
logger.addHandler(ch)
logger.setLevel(logging.DEBUG)

asyncio.run(main())


Expand Down
6 changes: 2 additions & 4 deletions aiowmi/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import asyncio
from typing import Optional, Callable
from Crypto.Cipher import ARC4
Expand Down Expand Up @@ -34,12 +33,11 @@
IID_IWbemServices,
)
from .rpc.request import RpcRequest
from .tools import pad4
from .rpc.response import RpcResponse
from .rpc.auth_verifier_co import RpcAuthVerifierCo
from .ndr.remote_create_instance_response import RemoteCreateInstanceResponse
from .ndr.ntlm_login_response import NTLMLoginResponse
from .ntlm.login import NTLMLogin
from .logger import logger


class Connection:
Expand Down Expand Up @@ -218,7 +216,7 @@ async def negotiate_ntlm(self) -> Protocol:
conn,
timeout=self._timeout)
except Exception as e:
logging.warning(
logger.warning(
f'failed to connect to {self._host} ({port}); '
f'fallback to {host} ({port})...')

Expand Down
6 changes: 3 additions & 3 deletions aiowmi/dtypes/dt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from typing import Union
from datetime import datetime, timedelta
from ..logger import logger


_FMT = '%Y%m%d%H%M%S.%f%z'
Expand Down Expand Up @@ -31,7 +31,7 @@ def dt_from_str(s: str) -> Union[datetime, timedelta]:
microseconds=int(s[15:21])
)
except Exception as e:
logging.debug(
logger.debug(
f'invalid interval `{s}` ({e}); return timedelta(0)')
td = timedelta(0)
return td
Expand All @@ -52,7 +52,7 @@ def dt_from_str(s: str) -> Union[datetime, timedelta]:
dt = datetime.strptime(f"{s[:21]}{s[-4]}{hours:02}{minutes:02}", _FMT)

except Exception as e:
logging.debug(
logger.debug(
f'invalid datetime `{s}` ({e}); '
'return datetime.fromtimestamp(0)')
dt = datetime.fromtimestamp(0)
Expand Down
3 changes: 3 additions & 0 deletions aiowmi/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import logging

logger = logging.getLogger('aiowmi')
4 changes: 2 additions & 2 deletions aiowmi/ndr/remote_create_instance_response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import struct
from ..exceptions import NoBindingException
from ..tools import is_fqdn, pad
Expand All @@ -8,6 +7,7 @@
from .orpcthat import ORPCTHAT
from .props_out_info import PropsOutInfo
from .scm_reply_info_data import ScmReplyInfoData
from ..logger import logger


class RemoteCreateInstanceResponse(NdrInterface):
Expand Down Expand Up @@ -64,7 +64,7 @@ def get_binding(self):
if self._binding is None:
raise NoBindingException('no network binding has been found')

logging.debug(f'selected binding: {self._binding}')
logger.debug(f'selected binding: {self._binding}')
return self._binding

def get_ipid(self) -> int:
Expand Down
7 changes: 3 additions & 4 deletions aiowmi/protocol.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import logging
import asyncio
import struct
from typing import Optional, Callable, Dict
from .dcom import Dcom
from .rpc.common import RpcCommon
from .rpc.response import RpcResponse
from .rpc.request import RpcRequest
from .rpc.fault import RpcFault
from .ndr.interface import NdrInterface
from .rpc.const import PFC_LAST_FRAG
from .rpc.baseresp import RpcBaseResp
from .request import Request
from .exceptions import DcomException
from .buf import Buf
from .logger import logger


class Protocol(asyncio.Protocol):
Expand Down Expand Up @@ -46,13 +45,13 @@ def connection_made(self, transport: asyncio.Transport) -> None:
'''

self._transport = transport
logging.info(f'connection made: {self.connection_info()}')
logger.info(f'connection made: {self.connection_info()}')

def connection_lost(self, exc: Exception) -> None:
'''
override asyncio.Protocol
'''
logging.info(f'connection lost {self.connection_info()}')
logger.info(f'connection lost {self.connection_info()}')
self._transport = None

def __bool__(self):
Expand Down

0 comments on commit 2d2af41

Please sign in to comment.