log.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <time.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <pthread.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <arpa/inet.h>
  28. #include "compiler.h"
  29. #include "log.h"
  30. #include "daemon/daemon.h"
  31. #include "config/config.h"
  32. #include "../../stat/stat_dtc.h"
  33. void init_log4cplus()
  34. {
  35. PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT(LOG4CPLUS_CONF_FILE));
  36. }
  37. void write_log(Logger logger, int level, const char *file_name,
  38. const char *func_name, int line, const char *fmt, ...)
  39. {
  40. //eg:[test.cpp] - [main] : 28--
  41. char str[4096];
  42. sprintf(str, "%d", line);
  43. string msg = "[" + (string)file_name + " : " + (string)str + "] - [" +
  44. (string)func_name + "]" + +" -- ";
  45. char buf[4096];
  46. int i;
  47. va_list ap;
  48. va_start(ap, fmt);
  49. i = vsnprintf(buf, 4096, fmt, ap);
  50. char *tmp = buf;
  51. string s = msg + string(tmp);
  52. va_end(ap);
  53. switch (level) {
  54. case 1:
  55. LOG4CPLUS_TRACE(logger, s);
  56. break;
  57. case 2:
  58. LOG4CPLUS_DEBUG(logger, s);
  59. break;
  60. case 3:
  61. LOG4CPLUS_INFO(logger, s);
  62. break;
  63. case 4:
  64. LOG4CPLUS_WARN(logger, s);
  65. break;
  66. case 5:
  67. LOG4CPLUS_ERROR(logger, s);
  68. break;
  69. case 6:
  70. LOG4CPLUS_FATAL(logger, s);
  71. break;
  72. default:
  73. break;
  74. }
  75. }