// Sample program posted by DCMTK forum user vignesh on 2024-09-18 // Forum post https://forum.dcmtk.org/viewtopic.php?t=5411 // Note: This needs to be compiled on Windows and requires Boost. #include #include #include #include #include "dcmtk/config/osconfig.h" #include "dcmtk/oflog/oflog.h" using namespace std; using namespace dcmtk::log4cplus; extern OFLogger sample_logger; #define LINFO(msg) OFLOG_INFO(sample_logger, msg) #define LDEBUG(msg) OFLOG_DEBUG(sample_logger, msg) #define LERROR(msg) OFLOG_ERROR(sample_logger, msg) #define LWARN(msg) OFLOG_WARN(sample_logger, msg) #define LTRACE(msg) OFLOG_TRACE(sample_logger, msg) #define LFATAL(msg) OFLOG_FATAL(sample_logger, msg) class LogHandler { public: std::string logLevel; void InitializeLogger(std::string fileName); void SetLogProperties(helpers::Properties& props, std::string fileName, std::string logLevel); }; void SetLogProperties(helpers::Properties& props, std::string fileName, std::string logLevel) { #define SET(propertyKey, propertyValue) props.setProperty(propertyKey, propertyValue) static const char* pattern = "[%-6i][%-6t][%D{%Y-%m-%d %H:%M:%S.%q}] %-5p: %m%n"; SET("log4cplus.logger.Service", "ALL"); SET("log4cplus.logger.Session", "ALL"); SET("log4cplus.appender.logfile", "log4cplus::RollingFileAppender"); if (logLevel == "debug") { SET("log4cplus.rootLogger", "DEBUG, console , logfile"); SET("log4cplus.appender.console", "log4cplus::ConsoleAppender"); SET("log4cplus.appender.console.ImmediateFlush", "true"); SET("log4cplus.appender.console.layout", "log4cplus::PatternLayout"); SET("log4cplus.appender.console.layout.ConversionPattern", pattern); SET("log4cplus.appender.logfile.MaxFileSize", "5MB"); } else { SET("log4cplus.rootLogger", "DEBUG, logfile"); SET("log4cplus.appender.logfile.MaxFileSize", "3MB"); } SET("log4cplus.appender.logfile.Append", "true"); SET("log4cplus.appender.logfile.ImmediateFlush", "true"); SET("log4cplus.appender.logfile.MaxBackupIndex", "10"); SET("log4cplus.appender.logfile.File", fileName.c_str()); SET("log4cplus.appender.logfile.layout", "log4cplus::PatternLayout"); SET("log4cplus.appender.logfile.layout.ConversionPattern", pattern); #undef SET } void InitializeLogger(std::string fileName) { helpers::Properties props(DCMTK_LOG4CPLUS_EXPORT("")); if (props.size() == 0) SetLogProperties(props, fileName, "debug"); unsigned int flags = 0; flags |= PropertyConfigurator::fRecursiveExpansion; flags |= PropertyConfigurator::fShadowEnvironment; PropertyConfigurator conf(props, Logger::getDefaultHierarchy(), flags); conf.configure(); Logger root = Logger::getRoot(); root.setLogLevel(OFLogger::INFO_LOG_LEVEL); OFLog::reconfigure(); } int main() { InitializeLogger("D:/log/sample.log"); // here I have read a json file and print the same return 0; }