Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | |
| 19 | // -=- Logger_file.cxx - Logger instance for a file |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 24 | #include <os/Mutex.h> |
| 25 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <rfb/util.h> |
| 27 | #include <rfb/Logger_file.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace rfb; |
| 30 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 31 | Logger_File::Logger_File(const char* loggerName) |
| 32 | : Logger(loggerName), indent(13), width(79), m_filename(0), m_file(0), |
| 33 | m_lastLogTime(0) |
| 34 | { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 35 | mutex = new os::Mutex(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | Logger_File::~Logger_File() |
| 39 | { |
| 40 | closeFile(); |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 41 | delete mutex; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void Logger_File::write(int level, const char *logname, const char *message) |
| 45 | { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 46 | os::AutoMutex a(mutex); |
| 47 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 48 | if (!m_file) { |
| 49 | if (!m_filename) return; |
| 50 | CharArray bakFilename(strlen(m_filename) + 1 + 4); |
| 51 | sprintf(bakFilename.buf, "%s.bak", m_filename); |
| 52 | remove(bakFilename.buf); |
| 53 | rename(m_filename, bakFilename.buf); |
| 54 | m_file = fopen(m_filename, "w+"); |
| 55 | if (!m_file) return; |
| 56 | } |
| 57 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 58 | time_t current = time(0); |
| 59 | if (current != m_lastLogTime) { |
| 60 | m_lastLogTime = current; |
| 61 | fprintf(m_file, "\n%s", ctime(&m_lastLogTime)); |
| 62 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | |
| 64 | fprintf(m_file," %s:", logname); |
| 65 | int column = strlen(logname) + 2; |
| 66 | if (column < indent) { |
| 67 | fprintf(m_file,"%*s",indent-column,""); |
| 68 | column = indent; |
| 69 | } |
| 70 | while (true) { |
| 71 | const char* s = strchr(message, ' '); |
| 72 | int wordLen; |
| 73 | if (s) wordLen = s-message; |
| 74 | else wordLen = strlen(message); |
| 75 | |
| 76 | if (column + wordLen + 1 > width) { |
| 77 | fprintf(m_file,"\n%*s",indent,""); |
| 78 | column = indent; |
| 79 | } |
| 80 | fprintf(m_file," %.*s",wordLen,message); |
| 81 | column += wordLen + 1; |
| 82 | message += wordLen + 1; |
| 83 | if (!s) break; |
| 84 | } |
| 85 | fprintf(m_file,"\n"); |
| 86 | fflush(m_file); |
| 87 | } |
| 88 | |
| 89 | void Logger_File::setFilename(const char* filename) |
| 90 | { |
| 91 | closeFile(); |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 92 | m_filename = strDup(filename); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void Logger_File::setFile(FILE* file) |
| 96 | { |
| 97 | closeFile(); |
| 98 | m_file = file; |
| 99 | } |
| 100 | |
| 101 | void Logger_File::closeFile() |
| 102 | { |
| 103 | if (m_filename) { |
| 104 | if (m_file) { |
| 105 | fclose(m_file); |
| 106 | m_file = 0; |
| 107 | } |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 108 | strFree(m_filename); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 109 | m_filename = 0; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static Logger_File logger("file"); |
| 114 | |
| 115 | bool rfb::initFileLogger(const char* filename) { |
| 116 | logger.setFilename(filename); |
| 117 | logger.registerLogger(); |
| 118 | return true; |
| 119 | } |