Skip to content

Commit 635e625

Browse files
committed
isolate logging and timing code into submodules
1 parent 895adc8 commit 635e625

File tree

3 files changed

+149
-66
lines changed

3 files changed

+149
-66
lines changed

src/loggedfs/core.py

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,11 @@
3333
import errno
3434
from functools import wraps
3535
import grp
36-
import logging
37-
import logging.handlers
3836
import os
3937
import pwd
4038
import re
4139
import stat
4240
import sys
43-
import time
44-
45-
try:
46-
from time import time_ns
47-
except ImportError:
48-
from time import time as _time
49-
time_ns = lambda: int(_time() * 1e9)
5041

5142
from fuse import (
5243
FUSE,
@@ -61,27 +52,8 @@
6152
except ImportError:
6253
fuse_features = {}
6354

64-
65-
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66-
# LOGGING: Support nano-second timestamps
67-
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
68-
69-
class _LogRecord_ns_(logging.LogRecord):
70-
def __init__(self, *args, **kwargs):
71-
self.created_ns = time_ns() # Fetch precise timestamp
72-
super().__init__(*args, **kwargs)
73-
74-
class _Formatter_ns_(logging.Formatter):
75-
default_nsec_format = '%s,%09d'
76-
def formatTime(self, record, datefmt=None):
77-
if datefmt is not None: # Do not handle custom formats here ...
78-
return super().formatTime(record, datefmt) # ... leave to original implementation
79-
ct = self.converter(record.created_ns / 1e9)
80-
t = time.strftime(self.default_time_format, ct)
81-
s = self.default_nsec_format % (t, record.created_ns - (record.created_ns // 10**9) * 10**9)
82-
return s
83-
84-
logging.setLogRecordFactory(_LogRecord_ns_)
55+
from .log import get_logger
56+
from .timing import time
8557

8658

8759
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -350,7 +322,8 @@ def __init__(self,
350322
if log_excludes is None:
351323
log_excludes = []
352324

353-
self._init_logger(log_enabled, log_file, log_syslog, log_printprocessname)
325+
self._log_printprocessname = bool(log_printprocessname)
326+
self.logger = get_logger('LoggedFS-python', log_enabled, log_file, log_syslog)
354327

355328
if bool(fuse_foreground_bool):
356329
self.logger.info('LoggedFS-python not running as a daemon')
@@ -383,40 +356,6 @@ def __init__(self,
383356
self._compile_filter(log_includes, log_excludes)
384357

385358

386-
def _init_logger(self, log_enabled, log_file, log_syslog, log_printprocessname):
387-
388-
log_formater = _Formatter_ns_('%(asctime)s (%(name)s) %(message)s')
389-
log_formater_short = _Formatter_ns_('%(message)s')
390-
391-
self._log_printprocessname = bool(log_printprocessname)
392-
393-
self.logger = logging.getLogger('LoggedFS-python')
394-
395-
if not bool(log_enabled):
396-
self.logger.setLevel(logging.CRITICAL)
397-
return
398-
self.logger.setLevel(logging.DEBUG)
399-
400-
ch = logging.StreamHandler()
401-
ch.setLevel(logging.DEBUG)
402-
ch.setFormatter(log_formater)
403-
self.logger.addHandler(ch)
404-
405-
if bool(log_syslog):
406-
sl = logging.handlers.SysLogHandler(address = '/dev/log') # TODO Linux only
407-
sl.setLevel(logging.DEBUG)
408-
sl.setFormatter(log_formater_short)
409-
self.logger.addHandler(sl)
410-
411-
if log_file is None:
412-
return
413-
414-
fh = logging.FileHandler(os.path.join(log_file)) # TODO
415-
fh.setLevel(logging.DEBUG)
416-
fh.setFormatter(log_formater)
417-
self.logger.addHandler(fh)
418-
419-
420359
def _compile_filter(self, include_list, exclude_list):
421360

422361
def proc_filter_item(in_item):
@@ -716,7 +655,7 @@ def _fix_time_(atime, mtime):
716655
if mtime in [UTIME_OMIT, None]:
717656
mtime = st.st_mtime_ns
718657
if UTIME_NOW in (atime, mtime):
719-
now = time_ns()
658+
now = time.time_ns()
720659
if atime == UTIME_NOW:
721660
atime = now
722661
if mtime == UTIME_NOW:

src/loggedfs/log.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
5+
LoggedFS-python
6+
Filesystem monitoring with Fuse and Python
7+
https://github.com/pleiszenburg/loggedfs-python
8+
9+
src/loggedfs/log.py: Logging
10+
11+
Copyright (C) 2017-2019 Sebastian M. Ernst <[email protected]>
12+
13+
<LICENSE_BLOCK>
14+
The contents of this file are subject to the Apache License
15+
Version 2 ("License"). You may not use this file except in
16+
compliance with the License. You may obtain a copy of the License at
17+
https://www.apache.org/licenses/LICENSE-2.0
18+
https://github.com/pleiszenburg/loggedfs-python/blob/master/LICENSE
19+
20+
Software distributed under the License is distributed on an "AS IS" basis,
21+
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
22+
specific language governing rights and limitations under the License.
23+
</LICENSE_BLOCK>
24+
25+
"""
26+
27+
28+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29+
# IMPORT
30+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31+
32+
import logging
33+
import logging.handlers
34+
import os
35+
36+
from .timing import time
37+
38+
39+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
40+
# LOGGING: Support nano-second timestamps
41+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
42+
43+
class _LogRecord_ns_(logging.LogRecord):
44+
45+
def __init__(self, *args, **kwargs):
46+
47+
self.created_ns = time.time_ns() # Fetch precise timestamp
48+
super().__init__(*args, **kwargs)
49+
50+
51+
class _Formatter_ns_(logging.Formatter):
52+
53+
default_nsec_format = '%s,%09d'
54+
55+
def formatTime(self, record, datefmt=None):
56+
57+
if datefmt is not None: # Do not handle custom formats here ...
58+
return super().formatTime(record, datefmt) # ... leave to original implementation
59+
ct = self.converter(record.created_ns / 1e9)
60+
t = time.strftime(self.default_time_format, ct)
61+
s = self.default_nsec_format % (t, record.created_ns - (record.created_ns // 10**9) * 10**9)
62+
return s
63+
64+
65+
logging.setLogRecordFactory(_LogRecord_ns_)
66+
67+
68+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
69+
# ROUTINES
70+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
71+
72+
73+
def get_logger(name, log_enabled, log_file, log_syslog):
74+
75+
log_formater = _Formatter_ns_('%(asctime)s (%(name)s) %(message)s')
76+
log_formater_short = _Formatter_ns_('%(message)s')
77+
78+
logger = logging.getLogger(name)
79+
80+
if not bool(log_enabled):
81+
logger.setLevel(logging.CRITICAL)
82+
return logger
83+
logger.setLevel(logging.DEBUG)
84+
85+
ch = logging.StreamHandler()
86+
ch.setLevel(logging.DEBUG)
87+
ch.setFormatter(log_formater)
88+
logger.addHandler(ch)
89+
90+
if bool(log_syslog):
91+
sl = logging.handlers.SysLogHandler(address = '/dev/log') # TODO Linux only
92+
sl.setLevel(logging.DEBUG)
93+
sl.setFormatter(log_formater_short)
94+
logger.addHandler(sl)
95+
96+
if log_file is None:
97+
return logger
98+
99+
fh = logging.FileHandler(os.path.join(log_file)) # TODO
100+
fh.setLevel(logging.DEBUG)
101+
fh.setFormatter(log_formater)
102+
logger.addHandler(fh)
103+
104+
return logger

src/loggedfs/timing.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
5+
LoggedFS-python
6+
Filesystem monitoring with Fuse and Python
7+
https://github.com/pleiszenburg/loggedfs-python
8+
9+
src/loggedfs/timing.py: Time related
10+
11+
Copyright (C) 2017-2019 Sebastian M. Ernst <[email protected]>
12+
13+
<LICENSE_BLOCK>
14+
The contents of this file are subject to the Apache License
15+
Version 2 ("License"). You may not use this file except in
16+
compliance with the License. You may obtain a copy of the License at
17+
https://www.apache.org/licenses/LICENSE-2.0
18+
https://github.com/pleiszenburg/loggedfs-python/blob/master/LICENSE
19+
20+
Software distributed under the License is distributed on an "AS IS" basis,
21+
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
22+
specific language governing rights and limitations under the License.
23+
</LICENSE_BLOCK>
24+
25+
"""
26+
27+
28+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29+
# IMPORT
30+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31+
32+
import time
33+
34+
35+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
36+
# FIX time_ns
37+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
38+
39+
if not hasattr(time, 'time_ns'):
40+
time.time_ns = lambda: int(time.time() * 1e9)

0 commit comments

Comments
 (0)