blob: 149ad404206018953dce7935327e313c6ad1324f [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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 Ossman338e73a2016-07-07 15:35:13 +020024#include <os/Mutex.h>
25
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/util.h>
27#include <rfb/Logger_file.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028
29using namespace rfb;
30
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031Logger_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 Ossman338e73a2016-07-07 15:35:13 +020035 mutex = new os::Mutex();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036}
37
38Logger_File::~Logger_File()
39{
40 closeFile();
Pierre Ossman338e73a2016-07-07 15:35:13 +020041 delete mutex;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042}
43
44void Logger_File::write(int level, const char *logname, const char *message)
45{
Pierre Ossman338e73a2016-07-07 15:35:13 +020046 os::AutoMutex a(mutex);
47
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000048 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 Kaplinskya2adc8d2006-05-25 05:01:55 +000058 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 Kaplinskya2adc8d2006-05-25 05:01:55 +000063
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
89void Logger_File::setFilename(const char* filename)
90{
91 closeFile();
Adam Tkacd36b6262009-09-04 10:57:20 +000092 m_filename = strDup(filename);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093}
94
95void Logger_File::setFile(FILE* file)
96{
97 closeFile();
98 m_file = file;
99}
100
101void Logger_File::closeFile()
102{
103 if (m_filename) {
104 if (m_file) {
105 fclose(m_file);
106 m_file = 0;
107 }
Adam Tkacd36b6262009-09-04 10:57:20 +0000108 strFree(m_filename);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 m_filename = 0;
110 }
111}
112
113static Logger_File logger("file");
114
115bool rfb::initFileLogger(const char* filename) {
116 logger.setFilename(filename);
117 logger.registerLogger();
118 return true;
119}