log.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __DTCLOG_H__
  17. #define __DTCLOG_H__
  18. #include <sys/cdefs.h>
  19. __BEGIN_DECLS
  20. #include <asm/unistd.h>
  21. #include <unistd.h>
  22. #ifndef __NR_gettid
  23. #endif
  24. static inline int _gettid_(void)
  25. {
  26. return syscall(__NR_gettid);
  27. }
  28. #include <sys/time.h>
  29. static inline unsigned int GET_MSEC(void)
  30. {
  31. struct timeval tv;
  32. gettimeofday(&tv, NULL);
  33. return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  34. }
  35. #define INIT_MSEC(v) v = GET_MSEC()
  36. #define CALC_MSEC(v) v = GET_MSEC() - (v)
  37. static inline unsigned int GET_USEC(void)
  38. {
  39. struct timeval tv;
  40. gettimeofday(&tv, NULL);
  41. return tv.tv_sec * 1000000 + tv.tv_usec;
  42. }
  43. #define INIT_USEC(v) v = GET_USEC()
  44. #define CALC_USEC(v) v = GET_USEC() - (v)
  45. __END_DECLS
  46. #include <string>
  47. #include <stdarg.h>
  48. #include <cstring>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <log4cplus/logger.h>
  52. #include <log4cplus/configurator.h>
  53. #include <log4cplus/helpers/stringhelper.h>
  54. #include <log4cplus/loggingmacros.h>
  55. #include <log4cplus/asyncappender.h>
  56. using namespace std;
  57. using namespace log4cplus;
  58. using namespace log4cplus::helpers;
  59. static Logger logger = Logger::getRoot();
  60. #define LOG4CPLUS_CONF_FILE "../conf/log4cplus.conf"
  61. /*********************************************
  62. **TRACE:调试应用的详细步骤
  63. **DEBUG:算法关键部分的相关信息
  64. **INFO: 应用的内部状态信息
  65. **WARN: 可以避免的内部状态信息
  66. **ERROR:发生了错误,且应用程序知道如何处理它
  67. **FATAL:发生了不可逆转的错误,程序无法继续运行
  68. **********************************************/
  69. #define log4cplus_trace(fmt, args...) \
  70. write_log(logger, 1, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
  71. #define log4cplus_debug(fmt, args...) \
  72. write_log(logger, 2, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
  73. #define log4cplus_info(fmt, args...) \
  74. write_log(logger, 3, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
  75. #define log4cplus_warning(fmt, args...) \
  76. write_log(logger, 4, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
  77. #define log4cplus_error(fmt, args...) \
  78. write_log(logger, 5, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
  79. #define log4cplus_fatal(fmt, args...) \
  80. write_log(logger, 6, __FILE__, __FUNCTION__, __LINE__, fmt, ##args)
  81. #define log4cplus_bare(lvl, fmt, args...) \
  82. write_log(logger, lvl, NULL, NULL, 0, fmt, ##args)
  83. extern void write_log(Logger, int, const char *, const char *, int,
  84. const char *, ...);
  85. extern void init_log4cplus();
  86. #endif