You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Logger_file.cxx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // -=- Logger_file.cxx - Logger instance for a file
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <limits.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <os/Mutex.h>
  26. #include <rfb/Logger_file.h>
  27. using namespace rfb;
  28. Logger_File::Logger_File(const char* loggerName)
  29. : Logger(loggerName), indent(13), width(79), m_file(0),
  30. m_lastLogTime(0)
  31. {
  32. m_filename[0] = '\0';
  33. mutex = new os::Mutex();
  34. }
  35. Logger_File::~Logger_File()
  36. {
  37. closeFile();
  38. delete mutex;
  39. }
  40. void Logger_File::write(int /*level*/, const char *logname, const char *message)
  41. {
  42. os::AutoMutex a(mutex);
  43. if (!m_file) {
  44. if (m_filename[0] == '\0')
  45. return;
  46. char bakFilename[PATH_MAX];
  47. if (snprintf(bakFilename, sizeof(bakFilename),
  48. "%s.bak", m_filename) >= (int)sizeof(bakFilename)) {
  49. remove(m_filename);
  50. } else {
  51. remove(bakFilename);
  52. rename(m_filename, bakFilename);
  53. }
  54. m_file = fopen(m_filename, "w+");
  55. if (!m_file) return;
  56. }
  57. time_t current = time(0);
  58. if (current != m_lastLogTime) {
  59. m_lastLogTime = current;
  60. fprintf(m_file, "\n%s", ctime(&m_lastLogTime));
  61. }
  62. fprintf(m_file," %s:", logname);
  63. int column = strlen(logname) + 2;
  64. if (column < indent) {
  65. fprintf(m_file,"%*s",indent-column,"");
  66. column = indent;
  67. }
  68. while (true) {
  69. const char* s = strchr(message, ' ');
  70. int wordLen;
  71. if (s) wordLen = s-message;
  72. else wordLen = strlen(message);
  73. if (column + wordLen + 1 > width) {
  74. fprintf(m_file,"\n%*s",indent,"");
  75. column = indent;
  76. }
  77. fprintf(m_file," %.*s",wordLen,message);
  78. column += wordLen + 1;
  79. message += wordLen + 1;
  80. if (!s) break;
  81. }
  82. fprintf(m_file,"\n");
  83. fflush(m_file);
  84. }
  85. void Logger_File::setFilename(const char* filename)
  86. {
  87. closeFile();
  88. m_filename[0] = '\0';
  89. if (strlen(filename) >= sizeof(filename))
  90. return;
  91. strcpy(m_filename, filename);
  92. }
  93. void Logger_File::setFile(FILE* file)
  94. {
  95. closeFile();
  96. m_file = file;
  97. }
  98. void Logger_File::closeFile()
  99. {
  100. if (m_file) {
  101. fclose(m_file);
  102. m_file = 0;
  103. }
  104. }
  105. static Logger_File logger("file");
  106. bool rfb::initFileLogger(const char* filename) {
  107. logger.setFilename(filename);
  108. logger.registerLogger();
  109. return true;
  110. }