log.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #pragma once
  38. namespace gearmand {
  39. struct gearmand_log_info_st
  40. {
  41. std::string filename;
  42. int fd;
  43. bool opt_syslog;
  44. bool opt_file;
  45. bool init_success;
  46. gearmand_log_info_st(const std::string &filename_arg, const bool syslog_arg) :
  47. filename(filename_arg),
  48. fd(-1),
  49. opt_syslog(syslog_arg),
  50. opt_file(false),
  51. init_success(false)
  52. {
  53. if (opt_syslog)
  54. {
  55. openlog("gearmand", LOG_PID | LOG_NDELAY, LOG_USER);
  56. }
  57. init();
  58. }
  59. void init()
  60. {
  61. if (filename.size())
  62. {
  63. if (filename.compare("stderr") == 0)
  64. {
  65. fd= STDERR_FILENO;
  66. opt_file= true;
  67. }
  68. else if (filename.compare("none") == 0)
  69. { }
  70. else
  71. {
  72. fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
  73. if (fd == -1)
  74. {
  75. char cwd_buffer[1024];
  76. char *cwd= getcwd(cwd_buffer, sizeof(cwd_buffer));
  77. char error_mesg[1024];
  78. int error_mesg_length= snprintf(error_mesg, sizeof(error_mesg),
  79. "Could not open log file \"%.*s\", from \"%s\", switching to stderr.",
  80. int(filename.size()), filename.c_str(),
  81. cwd);
  82. if (opt_syslog)
  83. {
  84. syslog(LOG_ERR, "%.*s", error_mesg_length, error_mesg);
  85. }
  86. error::perror(error_mesg);
  87. fd= STDERR_FILENO;
  88. }
  89. opt_file= true;
  90. }
  91. }
  92. init_success= true;
  93. }
  94. bool initialized() const
  95. {
  96. return init_success;
  97. }
  98. void reset()
  99. {
  100. int new_fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
  101. if (new_fd != -1)
  102. {
  103. int old_fd= fd;
  104. fd= new_fd;
  105. close(old_fd);
  106. }
  107. }
  108. int file() const
  109. {
  110. return fd;
  111. }
  112. void write(gearmand_verbose_t verbose, const char *mesg)
  113. {
  114. if (opt_file)
  115. {
  116. char buffer[GEARMAN_MAX_ERROR_SIZE];
  117. int buffer_length= snprintf(buffer, GEARMAN_MAX_ERROR_SIZE, "%7s %s\n", gearmand_verbose_name(verbose), mesg);
  118. if (::write(file(), buffer, buffer_length) == -1)
  119. {
  120. error::perror("Could not write to log file.");
  121. if (opt_syslog)
  122. {
  123. char getcwd_buffer[1024];
  124. char *ptr_buffer= getcwd(getcwd_buffer, sizeof(getcwd_buffer));
  125. syslog(LOG_ERR, "Could not open log file \"%.*s\", from \"%s\", open failed with (%s)",
  126. int(filename.size()), filename.c_str(),
  127. ptr_buffer,
  128. strerror(errno));
  129. }
  130. }
  131. }
  132. if (opt_syslog)
  133. {
  134. syslog(int(verbose), "%7s %s", gearmand_verbose_name(verbose), mesg);
  135. }
  136. }
  137. ~gearmand_log_info_st()
  138. {
  139. if (fd != -1 and fd != STDERR_FILENO)
  140. {
  141. close(fd);
  142. }
  143. if (opt_syslog)
  144. {
  145. closelog();
  146. }
  147. }
  148. };
  149. } // namespace gearmand