Skip to content

Logging

https://gist.github.com/chengdi123000/42ec8ed2cbef09ee050766c2f25498cb#file-mpifilehandler-py Created on Wed Feb 14 16:17:38 2018 This handler is used to deal with logging with mpi4py in Python3.

@author: cheng

@reference: https://cvw.cac.cornell.edu/python/logging https://groups.google.com/forum/#!topic/mpi4py/SaNzc8bdj6U https://gist.github.com/JohnCEarls/8172807

MPIFileHandler

Bases: FileHandler

Source code in june/logging.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class MPIFileHandler(logging.FileHandler):
    """ """
    def __init__(
        self,
        filename,
        mode=MPI.MODE_WRONLY | MPI.MODE_CREATE | MPI.MODE_APPEND,
        encoding="utf-8",
        delay=False,
        comm=MPI.COMM_WORLD,
    ):
        self.baseFilename = abspath(filename)
        self.mode = mode
        self.encoding = encoding
        self.comm = comm
        if delay:
            # We don't open the stream, but we still need to call the
            # Handler constructor to set level, formatter, lock etc.
            logging.Handler.__init__(self)
            self.stream = None
        else:
            logging.StreamHandler.__init__(self, self._open())

    def _open(self):
        """ """
        stream = MPI.File.Open(self.comm, self.baseFilename, self.mode)
        stream.Set_atomicity(True)
        return stream

    def emit(self, record):
        """Emit a record.

        If a formatter is specified, it is used to format the record.
        The record is then written to the stream with a trailing newline.  If

        Args:
            record: 

        Raises:
            traceback.print_exception: and appended to the stream
            has: an
            output: to the stream
            Modification: 
            stream: is MPI
            than: write
            bytestring: so
            only: once in each all of this emit function to keep atomicity


        """
        try:
            msg = self.format(record)
            stream = self.stream
            stream.Write_shared((msg + self.terminator).encode(self.encoding))
            # self.flush()
        except Exception:
            self.handleError(record)

    def close(self):
        """ """
        if self.stream:
            self.stream.Sync()
            self.stream.Close()
            self.stream = None

close()

Source code in june/logging.py
78
79
80
81
82
83
def close(self):
    """ """
    if self.stream:
        self.stream.Sync()
        self.stream.Close()
        self.stream = None

emit(record)

Emit a record.

If a formatter is specified, it is used to format the record. The record is then written to the stream with a trailing newline. If

Parameters:

Name Type Description Default
record
required

Raises:

Type Description
print_exception

and appended to the stream

has

an

output

to the stream

Modification
stream

is MPI

than

write

bytestring

so

only

once in each all of this emit function to keep atomicity

Source code in june/logging.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def emit(self, record):
    """Emit a record.

    If a formatter is specified, it is used to format the record.
    The record is then written to the stream with a trailing newline.  If

    Args:
        record: 

    Raises:
        traceback.print_exception: and appended to the stream
        has: an
        output: to the stream
        Modification: 
        stream: is MPI
        than: write
        bytestring: so
        only: once in each all of this emit function to keep atomicity


    """
    try:
        msg = self.format(record)
        stream = self.stream
        stream.Write_shared((msg + self.terminator).encode(self.encoding))
        # self.flush()
    except Exception:
        self.handleError(record)