|
1
|
|
|
# Copyright 2020 Diamond Light Source Ltd. |
|
2
|
|
|
# |
|
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
4
|
|
|
# you may not use this file except in compliance with the License. |
|
5
|
|
|
# You may obtain a copy of the License at |
|
6
|
|
|
# |
|
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
8
|
|
|
# |
|
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12
|
|
|
# See the License for the specific language governing permissions and |
|
13
|
|
|
# limitations under the License. |
|
14
|
|
|
|
|
15
|
|
|
""" |
|
16
|
|
|
.. module:: doc_test_utils |
|
17
|
|
|
:platform: Unix |
|
18
|
|
|
:synopsis: A module to set up doc test file handlers |
|
19
|
|
|
.. moduleauthor:: Jessica Verschoyle |
|
20
|
|
|
|
|
21
|
|
|
""" |
|
22
|
|
|
import os |
|
23
|
|
|
import logging |
|
24
|
|
|
|
|
25
|
|
|
import savu.plugins.utils as pu |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def setup_argparser(): |
|
29
|
|
|
""" Clean sys.argv so that command line testing will complete""" |
|
30
|
|
|
import sys |
|
31
|
|
|
|
|
32
|
|
|
sys.argv = [""] |
|
33
|
|
|
del sys |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def get_loggers(): |
|
37
|
|
|
logger = logging.getLogger("documentationLog") |
|
38
|
|
|
logger_rst = logging.getLogger("documentationRst") |
|
39
|
|
|
return logger, logger_rst |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def doc_test_path_setup(plugin_dir): |
|
43
|
|
|
"""Setup the logging file path, the logging config path and create the |
|
44
|
|
|
relevant directories |
|
45
|
|
|
""" |
|
46
|
|
|
|
|
47
|
|
|
# Determine Savu base path |
|
48
|
|
|
main_dir = os.path.dirname(os.path.realpath(__file__)).split("/Savu/")[0] |
|
49
|
|
|
savu_base_path = f"{main_dir}/Savu/" |
|
50
|
|
|
|
|
51
|
|
|
doc_test_path = "doc/doc_tests/" |
|
52
|
|
|
plugin_log_file = f"{doc_test_path}logs{plugin_dir}" |
|
53
|
|
|
out_path = savu_base_path + plugin_log_file |
|
54
|
|
|
|
|
55
|
|
|
config_path = f"{savu_base_path}{doc_test_path}logging.conf" |
|
56
|
|
|
|
|
57
|
|
|
# Create directory if it doesn't exist |
|
58
|
|
|
pu.create_dir(out_path) |
|
59
|
|
|
return out_path, config_path |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
def setup_log_files(logger, logger_rst, path): |
|
63
|
|
|
"""Use the logging config file to set up the log handlers, |
|
64
|
|
|
the format of the messages, and the 'level' eg DEBUG or CRITICAL |
|
65
|
|
|
|
|
66
|
|
|
:param logger: logger object for log file |
|
67
|
|
|
:param logger_rst: logger object for rst file |
|
68
|
|
|
:param path: path for documentation test logs |
|
69
|
|
|
:return: |
|
70
|
|
|
""" |
|
71
|
|
|
out_path, config_path = doc_test_path_setup(path) |
|
72
|
|
|
logging.config.fileConfig(config_path) |
|
73
|
|
|
|
|
74
|
|
|
# Add handlers to the logger to tell it where to send the log |
|
75
|
|
|
# information |
|
76
|
|
|
fh, ch = add_doc_log_handler(logger, out_path) |
|
77
|
|
|
fh_rst = add_doc_rst_handler(logger_rst, out_path) |
|
78
|
|
|
|
|
79
|
|
|
print("The log files are inside the directory " + out_path) |
|
80
|
|
|
return fh, ch, fh_rst |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
def add_doc_log_handler(logger, doc_log_path): |
|
84
|
|
|
"""Add a file handler to store the logger output to a file. |
|
85
|
|
|
Add a stream handler to send the logger output to the |
|
86
|
|
|
command line/sys output. |
|
87
|
|
|
|
|
88
|
|
|
:param logger: Logger which we want to add the file handler to |
|
89
|
|
|
:param doc_log_path: file path at which to store log file |
|
90
|
|
|
:returns file and stream handlers |
|
91
|
|
|
""" |
|
92
|
|
|
filename = os.path.join(doc_log_path, "doc.log") |
|
93
|
|
|
fh = logging.FileHandler(filename, mode="w") |
|
94
|
|
|
fh.setFormatter(logging.Formatter("%(message)s")) |
|
95
|
|
|
ch = logging.StreamHandler() |
|
96
|
|
|
|
|
97
|
|
|
fh.setLevel(logging.DEBUG) |
|
98
|
|
|
ch.setLevel(logging.DEBUG) |
|
99
|
|
|
logger.addHandler(fh) |
|
100
|
|
|
logger.addHandler(ch) |
|
101
|
|
|
return fh, ch |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
def add_doc_rst_handler(logger, doc_rst_path): |
|
105
|
|
|
"""Add a file handler to store the logger output to a file. |
|
106
|
|
|
|
|
107
|
|
|
:param logger: Logger which we want to add the handler to |
|
108
|
|
|
:param doc_log_path: file path at which to store rst file |
|
109
|
|
|
:return file handler |
|
110
|
|
|
""" |
|
111
|
|
|
rst_filename = os.path.join(doc_rst_path, "doc.rst") |
|
112
|
|
|
fh = logging.FileHandler(rst_filename, mode="w") |
|
113
|
|
|
fh.setFormatter(logging.Formatter("%(message)s")) |
|
114
|
|
|
fh.setLevel(logging.DEBUG) |
|
115
|
|
|
logger.addHandler(fh) |
|
116
|
|
|
return fh |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
def close_handler(logger, handler): |
|
120
|
|
|
"""Close handler""" |
|
121
|
|
|
handler.close() |
|
122
|
|
|
logger.removeHandler(handler) |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
def end_logging(logger, logger_rst, fh, ch, fh_rst): |
|
126
|
|
|
"""Close open handlers. Shutdown logging operation. |
|
127
|
|
|
|
|
128
|
|
|
:param logger: The logger for the log file and command line output |
|
129
|
|
|
:param logger_rst: The logger for the rst file |
|
130
|
|
|
:param fh: Tile handler for log file |
|
131
|
|
|
:param ch: Stream handler for command line |
|
132
|
|
|
:param fh_rst: File handler for rst file |
|
133
|
|
|
""" |
|
134
|
|
|
close_handler(logger, fh) |
|
135
|
|
|
close_handler(logger, ch) |
|
136
|
|
|
close_handler(logger_rst, fh_rst) |
|
137
|
|
|
logging.shutdown() |
|
138
|
|
|
|