LSST Applications g180d380827+107df2c2fa,g2079a07aa2+86d27d4dc4,g2305ad1205+27ad5d03fa,g2bbee38e9b+c6a8a0fb72,g337abbeb29+c6a8a0fb72,g33d1c0ed96+c6a8a0fb72,g3a166c0a6a+c6a8a0fb72,g3d1719c13e+cc4c7597ef,g487adcacf7+4b0be3ae0a,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+14d443d837,g62aa8f1a4b+588cc723a1,g674612935b+7a21c67e12,g858d7b2824+cc4c7597ef,g991b906543+cc4c7597ef,g99cad8db69+b90e8bab37,g9c22b2923f+e2510deafe,g9ddcbc5298+9a081db1e4,g9fbc5161f1+1dc7c5428e,ga1e77700b3+03d07e1c1f,gb0e22166c9+60f28cb32d,gb23b769143+cc4c7597ef,gba4ed39666+c2a2e4ac27,gbb8dafda3b+52e28152d7,gbd998247f1+585e252eca,gbeadb96d05+a322446fe6,gc120e1dc64+6a70fcc921,gc28159a63d+c6a8a0fb72,gc3e9b769f7+48c1343504,gcf0d15dbbd+7a21c67e12,gdaeeff99f8+f9a426f77a,ge6526c86ff+9349653ccd,ge79ae78c31+c6a8a0fb72,gee10cc3b42+585e252eca,w.2024.18
LSST Data Management Base Package
Loading...
Searching...
No Matches
PyLogAppender.h
Go to the documentation of this file.
1/*
2 * This file is part of log.
3 *
4 * Developed for the LSST Data Management System.
5 * This product includes software developed by the LSST Project
6 * (https://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef LSST_LOG_PYLOGAPPENDER_H
25#define LSST_LOG_PYLOGAPPENDER_H
26
27// Python header has to be first to avoid compilation warnings
28#include "Python.h"
29
30#include <cstdint>
31#include <map>
32#include <mutex>
33#include <string>
34
35// Base class header
36#include "log4cxx/appenderskeleton.h"
37
38#include "log4cxx/helpers/object.h"
39#include "PyObjectPtr.h"
40
41namespace lsst::log::detail {
42
43// This needs to be here for all LOG4CXX macros to work
44using namespace log4cxx;
45
58class PyLogAppender : public AppenderSkeleton {
59public:
60
61 DECLARE_LOG4CXX_OBJECT(PyLogAppender)
62 BEGIN_LOG4CXX_CAST_MAP()
63 LOG4CXX_CAST_ENTRY(PyLogAppender)
64 LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
65 END_LOG4CXX_CAST_MAP()
66
67 // Make an instance
69
70 // we do not support copying
71 PyLogAppender(const PyLogAppender&) = delete;
73
77 void append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p) override;
78
82 void close() override;
83
89 bool requiresLayout() const override;
90
94 void setOption(const LogString &option, const LogString &value) override;
95
96private:
97
98 // cache entry type
99 struct LRUEntry {
100 PyObjectPtr logger;
101 uint32_t age = 0;
102 };
103
104 using LRUCache = std::map<std::string, LRUEntry>;
105
106 PyObjectPtr _getLogger; // logging.getLogger() method
107 PyObjectPtr _mdc_class; // lsst.log.MDCDict class
108 std::mutex _cache_mutex;
109 uint32_t _lru_age = 0;
110 LRUCache _cache; // LRU cache for loggers
111};
112
113} // namespace lsst::log::detail
114
115#endif // LSST_LOG_PYLOGAPPENDER_H
This class defines special log4cxx appender which "appends" log messages to Python logging.
void close() override
Close this appender instance, this is no-op.
void append(const spi::LoggingEventPtr &event, log4cxx::helpers::Pool &p) override
Forward the event to Python logging.
PyLogAppender(const PyLogAppender &)=delete
void setOption(const LogString &option, const LogString &value) override
Handle configuration options.
PyLogAppender & operator=(const PyLogAppender &)=delete
bool requiresLayout() const override
Returns true if appender "requires" layout to be defined for it.
Smart pointer class for PyObject instances.
Definition PyObjectPtr.h:37