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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <stdlib.h>
  23. #include <string.h>
  24. #include <os/Mutex.h>
  25. #include <rfb/util.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_filename(0), m_file(0),
  30. m_lastLogTime(0)
  31. {
  32. mutex = new os::Mutex();
  33. }
  34. Logger_File::~Logger_File()
  35. {
  36. closeFile();
  37. delete mutex;
  38. }
  39. void Logger_File::write(int /*level*/, const char *logname, const char *message)
  40. {
  41. os::AutoMutex a(mutex);
  42. if (!m_file) {
  43. if (!m_filename) return;
  44. CharArray bakFilename(strlen(m_filename) + 1 + 4);
  45. sprintf(bakFilename.buf, "%s.bak", m_filename);
  46. remove(bakFilename.buf);
  47. rename(m_filename, bakFilename.buf);
  48. m_file = fopen(m_filename, "w+");
  49. if (!m_file) return;
  50. }
  51. time_t current = time(0);
  52. if (current != m_lastLogTime) {
  53. m_lastLogTime = current;
  54. fprintf(m_file, "\n%s", ctime(&m_lastLogTime));
  55. }
  56. fprintf(m_file," %s:", logname);
  57. int column = strlen(logname) + 2;
  58. if (column < indent) {
  59. fprintf(m_file,"%*s",indent-column,"");
  60. column = indent;
  61. }
  62. while (true) {
  63. const char* s = strchr(message, ' ');
  64. int wordLen;
  65. if (s) wordLen = s-message;
  66. else wordLen = strlen(message);
  67. if (column + wordLen + 1 > width) {
  68. fprintf(m_file,"\n%*s",indent,"");
  69. column = indent;
  70. }
  71. fprintf(m_file," %.*s",wordLen,message);
  72. column += wordLen + 1;
  73. message += wordLen + 1;
  74. if (!s) break;
  75. }
  76. fprintf(m_file,"\n");
  77. fflush(m_file);
  78. }
  79. void Logger_File::setFilename(const char* filename)
  80. {
  81. closeFile();
  82. m_filename = strDup(filename);
  83. }
  84. void Logger_File::setFile(FILE* file)
  85. {
  86. closeFile();
  87. m_file = file;
  88. }
  89. void Logger_File::closeFile()
  90. {
  91. if (m_filename) {
  92. if (m_file) {
  93. fclose(m_file);
  94. m_file = 0;
  95. }
  96. strFree(m_filename);
  97. m_filename = 0;
  98. }
  99. }
  100. static Logger_File logger("file");
  101. bool rfb::initFileLogger(const char* filename) {
  102. logger.setFilename(filename);
  103. logger.registerLogger();
  104. return true;
  105. }