acl/lib_acl_cpp/include/acl_cpp/stdlib/log.hpp

211 lines
7.2 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2014-11-19 00:25:21 +08:00
#include <stdarg.h>
#ifndef ACL_LOGGER_MACRO_OFF
2014-11-19 00:25:21 +08:00
# define logger_open acl::log::open
# define logger_close acl::log::close
2014-11-19 00:25:21 +08:00
# if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
# if _MSC_VER >= 1500
# define logger(fmt, ...) \
acl::log::msg4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# define logger_warn(fmt, ...) \
acl::log::warn4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# define logger_error(fmt, ...) \
acl::log::error4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# define logger_fatal(fmt, ...) \
acl::log::fatal4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# define logger_debug(section, level, fmt, ...) \
acl::log::msg6(section, level, __FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__)
# else
# define logger acl::log::msg1
# define logger_warn acl::log::warn1
# define logger_error acl::log::error1
# define logger_fatal acl::log::fatal1
# define logger_debug acl::log::msg3
# endif
2014-11-19 00:25:21 +08:00
# else
2014-11-19 00:25:21 +08:00
# ifdef ACL_CPP_LOG_SKIP_FILE
# define logger(fmt, args...) \
acl::log::msg4("none", __LINE__, __FUNCTION__, fmt, ##args)
# define logger_warn(fmt, args...) \
acl::log::warn4("none", __LINE__, __FUNCTION__, fmt, ##args)
# define logger_error(fmt, args...) \
acl::log::error4("none", __LINE__, __FUNCTION__, fmt, ##args)
# define logger_fatal(fmt, args...) \
acl::log::fatal4("none", __LINE__, __FUNCTION__, fmt, ##args)
# define logger_debug(section, level, fmt, args...) \
acl::log::msg6(section, level, __FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# else
# define logger(fmt, args...) \
acl::log::msg4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# define logger_warn(fmt, args...) \
acl::log::warn4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# define logger_error(fmt, args...) \
acl::log::error4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# define logger_fatal(fmt, args...) \
acl::log::fatal4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# define logger_debug(section, level, fmt, args...) \
acl::log::msg6(section, level, __FILE__, __LINE__, __FUNCTION__, fmt, ##args)
# endif
# endif
#endif // ACL_LOG_MACRO_OFF
2014-11-19 00:25:21 +08:00
namespace acl {
class string;
class ACL_CPP_API log {
2014-11-19 00:25:21 +08:00
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD>, <EFBFBD>ڳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>
* @param recipients {const char*} <EFBFBD><EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> "|" <EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD>׽ӿڣ<EFBFBD><EFBFBD><EFBFBD>:
2014-11-19 00:25:21 +08:00
* /tmp/test.log|UDP:127.0.0.1:12345|TCP:127.0.0.1:12345|UNIX:/tmp/test.sock
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> /tmp/test.log, UDP:127.0.0.1:12345,
* TCP:127.0.0.1:12345 <EFBFBD><EFBFBD> UNIX:/tmp/test.sock <EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param procname <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>: test
* @param cfg <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>ʽΪ: {section}:{level}; {section}:{level}; ...
* <EFBFBD><EFBFBD>: 100:2; 101:3; 102: 4, <EFBFBD><EFBFBD>ʾֻ<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD>ʶΪ 100/<EFBFBD><EFBFBD><EFBFBD><EFBFBD> < 2,
* <EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>ʶΪ 101/<EFBFBD><EFBFBD><EFBFBD><EFBFBD> < 3, <EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>ʶΪ 102/<EFBFBD><EFBFBD><EFBFBD><EFBFBD> < 4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
*/
static void open(const char* recipients, const char* procname = "unknown",
const char* cfg = NULL);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>־
2014-11-19 00:25:21 +08:00
*/
static void close(void);
/**
* <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><EFBFBD>ýӿ<EFBFBD>
* @param cfg {const char*} <EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>ǩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
* {section}:{level}; {section}:{level}; ...
* <EFBFBD><EFBFBD>: 1:1, 2:10, 3:8... or 1:1; 2:10; 3:8... or all:1
*/
static void debug_init(const char* cfg);
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD>δͨ<EFBFBD><EFBFBD> open <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>غ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
* @param onoff {bool}
*/
static void stdout_open(bool onoff);
/**
* <EFBFBD><EFBFBD>־<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
*/
static void ACL_CPP_PRINTF(1, 2) msg1(const char* fmt, ...);
static void ACL_CPP_PRINTF(4, 5) msg4(const char* fname,
int line, const char* func, const char* fmt, ...);
static void ACL_CPP_PRINTF(3, 4) msg3(size_t section,
size_t level, const char* fmt, ...);
static void ACL_CPP_PRINTF(6, 7) msg6(size_t section, size_t level,
const char* fname, int line, const char* func, const char* fmt, ...);
static void ACL_CPP_PRINTF(1, 2) warn1(const char* fmt, ...);
static void ACL_CPP_PRINTF(4, 5) warn4(const char* fname, int line,
const char* func, const char* fmt, ...);
static void ACL_CPP_PRINTF(1, 2) error1(const char* fmt, ...);
static void ACL_CPP_PRINTF(4, 5) error4(const char* fname, int line,
const char* func, const char* fmt, ...);
static void ACL_CPP_PRINTF(1, 2) fatal1(const char* fmt, ...);
static void ACL_CPP_PRINTF(4, 5) fatal4(const char* fname, int line,
const char* func, const char* fmt, ...);
static void vmsg2(const char* fmt, va_list ap);
static void vmsg5(const char* fname, int line, const char* func,
const char* fmt, va_list ap);
static void vmsg4(size_t section, size_t level, const char* fmt, va_list ap);
static void vmsg7(size_t section, size_t level, const char* fname,
int line, const char* func, const char* fmt, va_list ap);
static void vwarn2(const char* fmt, va_list ap);
static void vwarn5(const char* fname, int line, const char* func,
const char* fmt, va_list ap);
static void verror2(const char* fmt, va_list ap);
static void verror5(const char* fname, int line, const char* func,
const char* fmt, va_list ap);
static void vfatal2(const char* fmt, va_list ap);
static void vfatal5(const char* fname, int line, const char* func,
const char* fmt, va_list ap);
/************************************************************************/
/* ʾ<><CABE> */
2014-11-19 00:25:21 +08:00
/************************************************************************/
#ifndef ACL_LOGGER_MACRO_OFF
static void logger_test1(void) {
# define DEBUG_TEST_BASE 100
# define DEBUG_TEST1 (DEBUG_TEST_BASE + 1)
# define DEBUG_TEST2 (DEBUG_TEST_BASE + 2)
# define DEBUG_TEST3 (DEBUG_TEST_BASE + 3)
2014-11-19 00:25:21 +08:00
const char* logfile = "test.log", *procname = "test";
const char* cfg = "101:2; 102:3; 103:2";
// <20>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
2014-11-19 00:25:21 +08:00
logger_open(logfile, procname, cfg);
# if defined(VC2003) || defined(VC2002) || defined(VC6)
2014-11-19 00:25:21 +08:00
// <20><>д<EFBFBD><D0B4>־
2014-11-19 00:25:21 +08:00
logger("%s(%d), %s: %s", __FILE__, __LINE__, __FUNCTION__, "zsx");
logger_debug(DEBUG_TEST1, 1, "%s(%d), %s: hello world11(%s)!",
__FILE__, __LINE__, __FUNCTION__, "zsx");
logger_debug(DEBUG_TEST2, 3, "%s(%d), %s: hello world12(%s)!",
__FILE__, __LINE__, __FUNCTION__, "zsx");
logger_debug(DEBUG_TEST3, 2, "%s(%d), %s: hello world13(%s)!",
__FILE__, __LINE__, __FUNCTION__, "zsx");
// <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>־
2014-11-19 00:25:21 +08:00
logger_debug(DEBUG_TEST1, 3, "%s(%d), %s: hello world21(%s)!",
__FILE__, __LINE__, __FUNCTION__, "zsx");
# else // VC2005, VC2008, VC2010
2014-11-19 00:25:21 +08:00
// <20><>д<EFBFBD><D0B4>־
2014-11-19 00:25:21 +08:00
logger("error(%s)!", "zsx");
logger_debug(DEBUG_TEST1, 1, "hello world11(%s)!", "zsx");
logger_debug(DEBUG_TEST2, 3, "hello world12(%s)!", "zsx");
logger_debug(DEBUG_TEST3, 2, "hello world13(%s)!", "zsx");
// <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4>־
2014-11-19 00:25:21 +08:00
logger_debug(DEBUG_TEST1, 3, "hello world21(%s)!", "zsx");
# endif
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ر<EFBFBD><D8B1><EFBFBD>־
2014-11-19 00:25:21 +08:00
logger_close();
}
static void logger_test2(void) {
2014-11-19 00:25:21 +08:00
logger("logger ok!");
logger_warn("logger_warn ok!");
logger_error("logger_error ok!");
logger_fatal("logger_fatal ok!");
}
#endif // ACL_LOGGER_MACRO_OFF
2014-11-19 00:25:21 +08:00
};
} // namespace acl