vendredi 8 septembre 2023

Suggestion needed to remove duplicate class, without changing #define depends on that class static object

There is a duplicate class implementation in two projects, which are using C++. When i tried to merge the minor differences of both into a single class, i faced a blocker as the #define's in the class are having direct dependency on static object which is created out of this class on both projects as shown below.

From Project1

enum severity_type
{
    debug = 1,
    error,
    warning
};


template< typename T >
class logger
{
public:
    logger(const std::string& name);

};

#define LOGGING_LEVEL_1
static logger< file_log_policy > log_inst(GetAppDataPath()+ "\\Logs\\backup.log");
#ifdef LOGGING_LEVEL_1
#define LOG log_inst.print< severity_type::debug >
#define LOG_ERR log_inst.print< severity_type::error >
#define LOG_WARN log_inst.print< severity_type::warning >
#else
#define LOG(...) 
#define LOG_ERR(...)
#define LOG_WARN(...)
#endif

#ifdef LOGGING_LEVEL_2
#define ELOG log_inst.print< severity_type::debug >
#define ELOG_ERR log_inst.print< severity_type::error >
#define ELOG_WARN log_inst.print< severity_type::warning >
#else
#define ELOG(...) 
#define ELOG_ERR(...)
#define ELOG_WARN(...)
#endif

From Project 2

enum severity_type
{
    debug = 1,
    error,
    warning
};


template< typename T >
class logger
{
public:
    logger(const std::string& name);

};

#define LOGGING_LEVEL_1
static logger< file_log_policy > log_inst(GetAppDataPath()+ "\\Logs\\restore.log");
#ifdef LOGGING_LEVEL_1
#define LOG log_inst.print< severity_type::debug >
#define LOG_ERR log_inst.print< severity_type::error >
#define LOG_WARN log_inst.print< severity_type::warning >
#else
#define LOG(...) 
#define LOG_ERR(...)
#define LOG_WARN(...)
#endif

#ifdef LOGGING_LEVEL_2
#define ELOG log_inst.print< severity_type::debug >
#define ELOG_ERR log_inst.print< severity_type::error >
#define ELOG_WARN log_inst.print< severity_type::warning >
#else
#define ELOG(...) 
#define ELOG_ERR(...)
#define ELOG_WARN(...)
#endif

Here, you can observe as we have two static objects from two projects.

I want to have these static objects while the program is running. But the major bottle neck while i try to merge these files are the #define's from Project 1

#define LOGGING_LEVEL_1
static logger< file_log_policy > log_inst(GetAppDataPath()+ "\\Logs\\backup.log");
#ifdef LOGGING_LEVEL_1
#define LOG log_inst.print< severity_type::debug >
#define LOG_ERR log_inst.print< severity_type::error >
#define LOG_WARN log_inst.print< severity_type::warning >
#else
#define LOG(...) 
#define LOG_ERR(...)
#define LOG_WARN(...)
#endif

and project2

#define LOGGING_LEVEL_1
static logger< file_log_policy > log_inst(GetAppDataPath()+ "\\Logs\\restore.log");
#ifdef LOGGING_LEVEL_1
#define LOG log_inst.print< severity_type::debug >
#define LOG_ERR log_inst.print< severity_type::error >
#define LOG_WARN log_inst.print< severity_type::warning >
#else
#define LOG(...) 
#define LOG_ERR(...)
#define LOG_WARN(...)
#endif

these #define's like LOG, LOG_ERR are used in thousands of places in code. can you suggest a better approach here, without modifying the #define's where i can merge these classes and use a single class for both projects.

i want the #define's to be unchanged and the duplicate classes to be merged into one, and the static objects should also exist.

Can someone suggest a better approach where i can refactor this code without touching #define's

Aucun commentaire:

Enregistrer un commentaire