Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-07-01 07:05:56

0001 // Created by Dmitry Romanov
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 //
0004 
0005 #pragma once
0006 
0007 #include <memory>
0008 #include <spdlog/spdlog.h>
0009 #include <JANA/JApplication.h>
0010 #include "services/log/Log_service.h"
0011 #include "SpdlogExtensions.h"
0012 
0013 namespace eicrecon {
0014     class SpdlogMixin {
0015         /** Logger mixin
0016          *
0017          * @example:
0018          *      class MyFactory : JFactory, SpdlogMixin {
0019          *
0020          *          void Init() {
0021          *              InitLogger(GetApplication(), "MyPlugin:MyFactory");
0022          *
0023          *              // Logger is ready and can be used:
0024          *              m_log->info("MyFactory logger initialized");
0025          *          }
0026          *
0027          *          void Process(...) {
0028          *              m_log->trace("Using logger!");
0029          *          }
0030          *      };
0031          */
0032     public:
0033         using level = Log_service::level;
0034 
0035         /**
0036          * Initializes logger through current LogService
0037          * @param app - JApplication pointer, as obtained from GetApplication()
0038          * @param param_prefix - name of both logger and user parameter
0039          * @param default_level - optional - default log level, overrides default logging level
0040          *                          : trace, debug, info, warn, err, critical, off
0041          *
0042          * @remark: Each logger is cloned from spdlog::default_logger(). This allows to set the default level
0043          *          and output formatting on a global system level. But sometimes it is useful to provide
0044          *          default log level independently on what is set by the system. Again we are about DEFAULT value
0045          *          if no user flag is provided
0046          *
0047          * @example:
0048          *      InitLogger(GetApplication(), "BTRK:TrackerHits")           // Default log level is set the same as in system
0049          *      InitLogger(GetApplication(), "BTRK:TrackerHits", "info")   // By default log level is info
0050          *
0051          *  will create "BTRK:TrackerHits" logger and check -PBTRK:TrackerHits:LogLevel user parameter
0052          */
0053         void InitLogger(JApplication* app, const std::string &param_prefix, const level default_level = level::info) {
0054 
0055             // Logger. Get plugin level sub-log
0056             m_log = app->GetService<Log_service>()->logger(param_prefix, default_level);
0057         }
0058 
0059     public:
0060         std::shared_ptr<spdlog::logger> &logger() { return m_log; }
0061 
0062     protected: // FIXME change to private
0063         /// current logger
0064         std::shared_ptr<spdlog::logger> m_log;
0065 
0066     };
0067 }