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

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