Skip to content

Commit 9990295

Browse files
authored
Fix MAPDL collection (#809)
* fix mapdl collection due to logging * use global logger on collection * completely disable logging * fix edge case
1 parent f279db6 commit 9990295

File tree

3 files changed

+70
-50
lines changed

3 files changed

+70
-50
lines changed

ansys/mapdl/core/launcher.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,19 @@ def _is_ubuntu():
6262
if os.name != "posix":
6363
return False
6464

65+
# gcc is installed by default
66+
proc = subprocess.Popen("gcc --version", shell=True, stdout=subprocess.PIPE)
67+
if 'ubuntu' in proc.stdout.read().decode().lower():
68+
return True
69+
6570
# try lsb_release as this is more reliable
6671
try:
6772
import lsb_release
6873

6974
if lsb_release.get_distro_information()["ID"].lower() == "ubuntu":
7075
return True
7176
except ImportError:
77+
# finally, check platform
7278
return "ubuntu" in platform.platform().lower()
7379

7480

ansys/mapdl/core/logging.py

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,120 @@
11
"""
2-
``log`` module
3-
*******************
2+
``logging`` module
3+
**************
44
55
## Objective
6-
This module intends to create a general framework for logging in Pymapdl.
7-
This module is built upon ``logging`` library and it does NOT intend to replace it rather provide a way to interact between ``logging`` and ``Pymapdl``.
6+
This module intends to create a general framework for logging in
7+
Pymapdl. This module is built upon ``logging`` library and it does
8+
NOT intend to replace it rather provide a way to interact between
9+
``logging`` and ``Pymapdl``.
810
9-
The loggers used in the module include the name of the instance which is intended to be unique.
10-
This name is printed in all the active outputs and it is used to track the different MAPDL instances.
11+
The loggers used in the module include the name of the instance which
12+
is intended to be unique. This name is printed in all the active
13+
outputs and it is used to track the different MAPDL instances.
1114
1215
1316
Usage
14-
----------
17+
-----
1518
1619
Global logger
1720
~~~~~~~~~~~~~~~~~
1821
19-
There is a global logger named ``pymapdl_global`` which is created at ``ansys.mapdl.core.__init__``.
20-
If you want to use this global logger, you must call at the top of your module:
22+
There is a global logger named ``pymapdl_global`` which is created at
23+
``ansys.mapdl.core.__init__``. If you want to use this global logger,
24+
you must call at the top of your module:
2125
22-
.. code::
26+
.. code:: python
2327
from ansys.mapdl.core import LOG
2428
2529
You could also rename it to avoid conflicts with other loggers (if any):
2630
27-
.. code::
31+
.. code:: python
2832
from ansys.mapdl.core import LOG as logger
2933
3034
3135
It should be noticed that the default logging level of ``LOG`` is ``ERROR``.
3236
To change this and output lower level messages you can use the next snippet:
3337
34-
.. code::
38+
.. code:: python
3539
LOG.logger.setLevel('DEBUG')
3640
LOG.file_handler.setLevel('DEBUG') # If present.
3741
LOG.stdout_handler.setLevel('DEBUG') # If present.
3842
3943
4044
Alternatively, you can do:
4145
42-
.. code::
46+
.. code:: python
4347
LOG.setLevel('DEBUG')
4448
4549
4650
This way ensures all the handlers are set to the input log level.
4751
48-
By default, this logger does not log to a file. If you wish to do so, you can add a file handler using:
52+
By default, this logger does not log to a file. If you wish to do so,
53+
you can add a file handler using:
4954
50-
.. code::
55+
.. code:: python
5156
import os
5257
file_path = os.path.join(os.getcwd(), 'pymapdl.log')
5358
LOG.log_to_file(file_path)
5459
5560
56-
This sets the logger to be redirected also to that file.
57-
If you wish to change the characteristics of this global logger from the beginning of the execution,
58-
you must edit the file ``__init__`` in the directory ``ansys.mapdl.core``.
61+
This sets the logger to be redirected also to that file. If you wish
62+
to change the characteristics of this global logger from the beginning
63+
of the execution, you must edit the file ``__init__`` in the directory
64+
``ansys.mapdl.core``.
5965
6066
To log using this logger, just call the desired method as a normal logger.
6167
62-
.. code::
68+
.. code:: python
6369
>>> import logging
6470
>>> from ansys.mapdl.core.logging import Logger
6571
>>> LOG = Logger(level=logging.DEBUG, to_file=False, to_stdout=True)
6672
>>> LOG.debug('This is LOG debug message.')
6773
68-
| Level | Instance | Module | Function | Message
69-
|----------|-----------------|------------------|----------------------|--------------------------------------------------------
70-
| DEBUG | | __init__ | <module> | This is LOG debug message.
71-
74+
DEBUG - - <ipython-input-24-80df150fe31f> - <module> - This is LOG debug message.
7275
7376
74-
Instance logger
75-
~~~~~~~~~~~~~~~~~
76-
Every time that the class ``_MapdlCore`` is instantiated, a logger is created and stored in two places:
77+
Instance Logger
78+
~~~~~~~~~~~~~~~
79+
Every time that the class ``_MapdlCore`` is instantiated, a logger is
80+
created and stored in two places:
7781
7882
* ``_MapdlCore._log``. For backward compatibility.
7983
* ``LOG._instances``. This field is a ``dict`` where the key is the name of the created logger.
8084
81-
These instance loggers inheritate the ``pymapdl_global`` output handlers and logging level unless otherwise specified.
82-
The way this logger works is very similar to the global logger.
83-
You can add a file handler if you wish using the method ``log_to_file`` or change the log level using ``setLevel`` method.
85+
These instance loggers inheritate the ``pymapdl_global`` output
86+
handlers and logging level unless otherwise specified. The way this
87+
logger works is very similar to the global logger. You can add a file
88+
handler if you wish using the method ``log_to_file`` or change the log
89+
level using ``setLevel`` method.
8490
8591
You can use this logger like this:
8692
8793
.. code:: python
8894
>>> from ansys.mapdl.core import launch_mapdl
8995
>>> mapdl = launch_mapdl()
90-
>>> mapdl._log.info('This is an useful message')
96+
>>> mapdl._log.info('This is a useful message')
9197
92-
| Level | Instance | Module | Function | Message
93-
|----------|-----------------|------------------|----------------------|--------------------------------------------------------
94-
| INFO | 127.0.0.1:50052 | test | <module> | This is an useful message
98+
INFO - GRPC_127.0.0.1:50056 - <ipython-input-19-f09bb2d8785c> - <module> - This is a useful message
9599
96100
97101
98102
Other loggers
99103
~~~~~~~~~~~~~~~~~
100-
You can create your own loggers using python ``logging`` library as you would do in any other script.
101-
There shall no be conflicts between these loggers.
104+
You can create your own loggers using python ``logging`` library as
105+
you would do in any other script. There shall no be conflicts between
106+
these loggers.
102107
103108
104-
Notes
105-
---------
106-
107109
108110
To-Do's
109-
-----------
111+
-------
110112
111113
* [] Make sure the logging level is changed when instance is created.
112114
113115
"""
114116

117+
import weakref
115118
import logging
116119
from datetime import datetime
117120
import sys
@@ -157,10 +160,12 @@
157160
class PymapdlCustomAdapter(logging.LoggerAdapter):
158161
"""This is key to keep the reference to the MAPDL instance name dynamic.
159162
160-
If we use the standard approach which is supplying ``extra`` input to the logger, we
161-
would need to keep inputting MAPDL instances every time we do a log.
163+
If we use the standard approach which is supplying ``extra`` input
164+
to the logger, we would need to keep inputting MAPDL instances
165+
every time we do a log.
162166
163-
Using adapters we just need to specify the MAPDL instance we refer to once.
167+
Using adapters we just need to specify the MAPDL instance we refer
168+
to once.
164169
"""
165170

166171
level = None # This is maintained for compatibility with ``supress_logging``, but it does nothing.
@@ -169,7 +174,10 @@ class PymapdlCustomAdapter(logging.LoggerAdapter):
169174

170175
def __init__(self, logger, extra=None):
171176
self.logger = logger
172-
self.extra = extra
177+
if extra is not None:
178+
self.extra = weakref.proxy(extra)
179+
else:
180+
self.extra = None
173181
self.file_handler = logger.file_handler
174182
self.std_out_handler = logger.std_out_handler
175183

@@ -418,9 +426,13 @@ def add_child_logger(self, sufix, level=None):
418426

419427
def _add_mapdl_instance_logger(self, name, mapdl_instance, level):
420428
if isinstance(name, str):
421-
instance_logger = PymapdlCustomAdapter(self._make_child_logger(name, level), mapdl_instance)
429+
instance_logger = PymapdlCustomAdapter(
430+
self._make_child_logger(name, level), mapdl_instance
431+
)
422432
elif isinstance(name, None):
423-
instance_logger = PymapdlCustomAdapter(self._make_child_logger("NO_NAMED_YET", level), mapdl_instance)
433+
instance_logger = PymapdlCustomAdapter(
434+
self._make_child_logger("NO_NAMED_YET", level), mapdl_instance
435+
)
424436
else:
425437
raise ValueError("You can only input 'str' classes to this method.")
426438

@@ -443,9 +455,9 @@ def add_instance_logger(self, name, mapdl_instance, level=None):
443455
Returns
444456
-------
445457
ansys.mapdl.core.logging.PymapdlCustomAdapter
446-
Logger adapter customized to add MAPDL information to the logs.
447-
You can use this class to log events in the same way you would with a logger
448-
class.
458+
Logger adapter customized to add MAPDL information to the
459+
logs. You can use this class to log events in the same
460+
way you would with the logger class.
449461
450462
Raises
451463
------
@@ -458,7 +470,9 @@ def add_instance_logger(self, name, mapdl_instance, level=None):
458470
count_ += 1
459471
new_name = name + '_' + str(count_)
460472

461-
self._instances[new_name] = self._add_mapdl_instance_logger(new_name, mapdl_instance, level)
473+
self._instances[new_name] = self._add_mapdl_instance_logger(
474+
new_name, mapdl_instance, level
475+
)
462476
return self._instances[new_name]
463477

464478
def __getitem__(self, key):

tests/test_mapdl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def warns_in_cdread_error_log(mapdl):
126126

127127
warns = []
128128
for each in error_files:
129-
with open(os.path.join(mapdl.directory, each)) as fid:
129+
with open(os.path.join(mapdl.directory, each), errors='ignore') as fid:
130130
error_log = ''.join(fid.readlines())
131131
warns.append((warn_cdread_1 in error_log) or (warn_cdread_2 in error_log) or (warn_cdread_3 in error_log))
132132
return any(warns)

0 commit comments

Comments
 (0)