ATLAS Offline Software
PyLogger.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
12 #include "RootUtils/PyLogger.h"
13 #include "Utility.h"
14 
15 
16 namespace RootUtils {
17 
18 
27 PyLogger::PyLogger (PyObject* debugfn, PyObject* errorfn)
28  : m_debugfn (debugfn),
29  m_errorfn (errorfn)
30 {
31  Py_INCREF (debugfn);
32  Py_INCREF (errorfn);
33 }
34 
35 
40 {
41  Py_DECREF (m_debugfn);
42  Py_DECREF (m_errorfn);
43 }
44 
45 
50 void PyLogger::debug (const char* msg)
51 {
52  call (m_debugfn, msg);
53 }
54 
55 
60 void PyLogger::error (const char* msg)
61 {
62  call (m_errorfn, msg);
63 }
64 
65 
73 void PyLogger::call (PyObject* fn, const char* msg)
74 {
75  if (!fn || fn == Py_None)
76  return;
77 
78  PyObject* res = PyObject_CallFunction (fn, (char*)"s", msg);
79  if (res == 0)
80  throw RootUtils::PyException();
81  Py_DECREF (res);
82 }
83 
84 
85 } // namespace RootUtils
RootUtils::PyException
CPyCppyy::PyException PyException
Definition: Utility.h:24
RootUtils
Definition: ILogger.h:20
RootUtils::PyLogger::m_debugfn
PyObject * m_debugfn
The debug message function.
Definition: PyLogger.h:80
Utility.h
Utility code originally from pyroot.
RootUtils::PyLogger::PyLogger
PyLogger(PyObject *debugfn, PyObject *errorfn)
Constructor.
Definition: PyLogger.cxx:27
RootUtils::PyLogger::debug
virtual void debug(const char *msg)
Log a debugging message.
Definition: PyLogger.cxx:50
RootUtils::PyLogger::m_errorfn
PyObject * m_errorfn
The error message function.
Definition: PyLogger.h:83
python.getCurrentFolderTag.fn
fn
Definition: getCurrentFolderTag.py:65
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
RootUtils::PyLogger::error
virtual void error(const char *msg)
Log an error message.
Definition: PyLogger.cxx:60
RootUtils::PyLogger::call
static void call(PyObject *fn, const char *msg)
Common code to call Python callback.
Definition: PyLogger.cxx:73
PyLogger.h
A concrete implementation of ILogger that calls back to Python.
PyObject
_object PyObject
Definition: IPyComponent.h:26
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
RootUtils::PyLogger::~PyLogger
~PyLogger()
Destructor.
Definition: PyLogger.cxx:39