123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #ifndef _VTM_LOG4CPLUS_HELPER_H_
- #define _VTM_LOG4CPLUS_HELPER_H_
- #pragma once
- #include "log4rvcother.h"
- #include <cstdint>
- #include <iostream>
- #include <set>
- #include <queue>
- #include <log4cplus/consoleappender.h>
- #include <log4cplus/fileappender.h>
- #include <log4cplus/socketappender.h>
- #include <log4cplus/asyncappender.h>
- #include <log4cplus/nullappender.h>
- #include <log4cplus/appender.h>
- #include <log4cplus/layout.h>
- #include <log4cplus/logger.h>
- #include <log4cplus/ndc.h>
- #include <log4cplus/helpers/loglog.h>
- #include <log4cplus/streams.h>
- #include <log4cplus/loggingmacros.h>
- #include <log4cplus/configurator.h>
- #include "log4upload.h"
- #include "mutex.h"
- namespace cmb {
- enum cpluslog_level
- {
- log_level_debug = 0,
- log_level_trace,
- log_level_info,
- log_level_warn,
- log_level_error,
- log_level_fatal,
- log_level_end
- };
- enum cpluslog_append_type
- {
- log_append_none = 0,
- log_append_console = 1,
- log_append_file = 2,
- log_append_console_file = 3,
- log_append_socket = 4,
- log_append_console_socket = 5,
- log_append_file_socket = 6,
- log_append_all = 7
- };
- class log4cplus_helper : public log4rvcother
- {
- public:
- ~log4cplus_helper(void);
- static log4cplus_helper* get_instance();
- bool init(const char* name);
- void uninit();
- std::ostream& stream();
- void trace(const vtm_string& text);
- void info(const vtm_string& text);
- void debug(const vtm_string& text);
- void warn(const vtm_string& text);
- void error(const vtm_string& text);
- void fatal(const vtm_string& text);
- void trace(const char* text);
- void info(const char* text);
- void debug(const char* text);
- void warn(const char* text);
- void error(const char* text);
- void fatal(const char* text);
- bool set_log_append_type(int type) {
- if((type & log_append_console) || (type & log_append_file) || (type & log_append_socket)) {
- _append_type = type;
- return true;
- }
- return false;
- }
- /*!
- * @brief 设置日志文件存储的路径文件夹
- * @param[in] 存储的文件夹路径,兼容不加 '\\' 的情况
- * @return :
- */
- bool set_log_dir(const vtm_string& log_file) {
- vtm_string tmp(log_file);
- if(tmp.empty())
- return false;
- if(tmp[tmp.size()-1] != '\\' && tmp[tmp.size()-1] != '/') {
- #if defined(_MSC_VER)
- tmp.push_back('\\');
- #else
- tmp.push_back('/');
- #endif //_MSC_VER
- }
- _log_dir = tmp;
- return true;
- }
- bool set_log_level(const int level) {
- const int ll = level * 10000;
- if(ll < 0 || ll > log4cplus::OFF_LOG_LEVEL) {
- return false;
- }
- _log_level = ll;
- return true;
- }
- void update_map_value(const vtm_string& value) {
- if(!value.empty()) {
- log4cplus::getMDC ().put("key", value);
- }
- }
- vtm_string get_map_value() {
- vtm_string value("");
- if(!log4cplus::getMDC().get(&value, "key")) {
- return vtm_string("");
- }
- return value;
- }
- int run_log_sender(const vtm_string& ip, const unsigned short port);
- vtm_string format(const char *ft, ...);
- vtm_string format(const char *ft,va_list& arg);
- void log( const char *file_name, const char *func_name, int line, const char* ft, int type = log_level_debug);
- void log( const char *file_name, const char *func_name, int line, int type ,const char *ft = "", ...);
- void log(int log_level, const vtm_string& text);
- void logw(int log_level, const std::wstring& wtext);
- log4cplus::Logger get_logger() {
- return log4cplus::Logger::getInstance(LOG4CPLUS_TEXT(_log_key_name.c_str()));
- }
- void initialize_linklog(const log_init_config& config);
- void loglink(LOG_LEVEL_E level, vtm_string text);
- private:
- static log4cplus_helper* _instance;
- log4cplus_helper(void);
- void config_remote_logger(const char* remote_name);
- log4cplus::helpers::SharedObjectPtr<log4cplus::Appender> _append_console;
- log4cplus::helpers::SharedObjectPtr<log4cplus::Appender> _append_file;
- log4cplus::helpers::SharedObjectPtr<log4cplus::Appender> _append_socket;
- log4cplus::helpers::SharedObjectPtr<log4cplus::Appender> _append_none;
- log4cplus::tstring _log_dir;
- log4cplus::tstring _log_server_ip;
- unsigned short _log_server_port;
- vtm_string _log_client_key_name;
- int _append_type;
- vtm_string _log_key_name;
- bool _socket_send_running;
- static mutex _mutex;
- bool _initialized;
- unsigned short _log_listen_port;
- mutex _mutex_4_update_socket;
- bool _initilzed_zip;
- std::queue<vtm_string> _zip_logs;
- vtm_string _cur_upload_log;
- uint64_t _cur_upload_offset;
- int _log_level;
- bool _initialize_uploaded;
- #ifndef USE_SKYEYELOG
- upload_helper _upload_helper;
- #endif
- };
- }
- #endif //_VTM_LOG4CPLUS_HELPER_H_
|