GCC Code Coverage Report


Directory: ./
File: include/na64util/log4cpp-extras.hh
Date: 2025-09-01 06:19:01
Exec Total Coverage
Lines: 0 5 0.0%
Functions: 0 4 0.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #pragma once
2
3 #include <log4cpp/LayoutAppender.hh>
4 #include <log4cpp/AppendersFactory.hh>
5 #include <log4cpp/LayoutsFactory.hh>
6
7 #include <unordered_map>
8 #include <mutex>
9
10 namespace na64dp {
11 namespace util {
12
13 #pragma GCC diagnostic push
14 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
15 typedef std::auto_ptr<log4cpp::Appender> Log4cppAppendersFactoryResult_t;
16 typedef std::auto_ptr<log4cpp::Layout> Log4cppLayoutFactoryResult_t;
17 #pragma GCC diagnostic pop
18
19 /**\brief Adds support for some auxiliary logging facilities
20 *
21 * Will add following entities to `log4cpp` object factories:
22 * - Appenders:
23 * -- (TODO) "ostream-stdout" -- present in native `log4cpp` properties
24 * configurator, but weirdly absent in factory
25 * -- (TODO) "Geant4 UI" -- does nothing, if there is no running Geant4 session
26 *
27 * \todo JSON layout
28 * \todo ZMQ/remote/EPI appender
29 * */
30 void inject_extras_to_log4cpp();
31
32 /**\brief Custom stream layout appender
33 *
34 * Extends basic behaviour to print status line if FD is TTY. In principle,
35 * should have exclusive access to standard output stream for a working
36 * process.
37 * */
38 class StdoutStreamAppender : public log4cpp::LayoutAppender {
39 protected:
40 StdoutStreamAppender( const std::string & name
41 , int fd=1 );
42 virtual ~StdoutStreamAppender()
43 { close(); }
44 /// log4cpp API -- does nothing for this class
45 bool reopen() override { return true; }
46 /// log4cpp API -- does nothing for this class
47 void close() override {}
48 private:
49 /// destination file descriptor
50 int _fd;
51 /// ptr to self instance, if instantiated
52 static StdoutStreamAppender * _self;
53 /// guards stdout
54 std::mutex _lockStatusline;
55 /// keeps status line to print
56 char _statusline[128];
57 protected:
58 /// log4cpp API -- actually prints an event
59 void _append(const log4cpp::LoggingEvent & event) override;
60 public:
61 /// return ptr to (singleton) instance or `null`
62 static StdoutStreamAppender * self();
63 /// sets the status text to be printed in console
64 void update_status_text(const char *);
65
66 friend Log4cppAppendersFactoryResult_t
67 create_ostream_appender(const log4cpp::AppendersFactory::params_t & p);
68 }; // class StdoutStreamAppender
69
70 /**\brief Custom pattern layout for log4cpp
71 *
72 * This is an extended variation based on standard `log4cpp::PatternLayout`
73 * class.
74 *
75 * Besides of same formatting characters it also has minor enhancements to
76 * colorize the output, shorten level, etc. Coloring somehow became important
77 * since our logs is the primary feedback to user about app being ran.
78 * */
79 class PatternLayout : public log4cpp::Layout {
80 public:
81 /// Layout component base class
82 struct iComponent {
83 virtual void append_msg(const log4cpp::LoggingEvent &, std::ostringstream &) = 0;
84 virtual ~iComponent() {}
85 };
86 /// Component constructor
87 typedef iComponent * (*ComponentConstructor)(const std::unordered_map<std::string, std::string> &);
88 /// Additional tags for coloring/text decorations
89 struct DecorationSuffixes {
90 std::string titleOpen, titleClose, messageOpen, messageClose;
91 };
92 protected:
93 /// Dictionary of component constructors available for this type
94 /// of pattern layout
95 std::unordered_map<std::string, ComponentConstructor> _constructors;
96 /// Active components
97 std::vector<iComponent *> _components;
98 /// Suffixes, by priority/100
99 std::vector<DecorationSuffixes> _suffixes;
100 public:
101 /// ctr (empty)
102 PatternLayout();
103 ~PatternLayout();
104 /// Sets pattern to use
105 void set_pattern(const std::string &);
106 // Sets suffixes assets, TODO: implement it?
107 //void set_coloring(const std::vector<DecorationSuffixes> &);
108 /// String formatting implementation
109 std::string format(const log4cpp::LoggingEvent &) override;
110
111 /// Standard log4cpp creator function callback to cope with its native
112 /// factory
113 static Log4cppLayoutFactoryResult_t create(const log4cpp::LayoutsFactory::params_t &);
114 }; // class ColoredPatternLayout
115
116 } // namespace ::na64dp::util
117 } // namespace na64dp
118
119