blob: ebe15d529df303bc787714cda0c353fe08b49ceb [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
24#include <rfb/util.h>
25#include <rfb/Logger_file.h>
26#include <rfb/Threading.h>
27
28using namespace rfb;
29
30
31// If threading is available then protect the write() operation
32// from concurrent accesses
33#ifdef __RFB_THREADING_IMPL
34static Mutex logLock;
35#endif
36
37
38Logger_File::Logger_File(const char* loggerName)
39 : Logger(loggerName), indent(13), width(79), m_filename(0), m_file(0),
40 m_lastLogTime(0)
41{
42}
43
44Logger_File::~Logger_File()
45{
46 closeFile();
47}
48
49void Logger_File::write(int level, const char *logname, const char *message)
50{
51#ifdef __RFB_THREADING_IMPL
52 Lock l(logLock);
53#endif
54 if (!m_file) {
55 if (!m_filename) return;
56 CharArray bakFilename(strlen(m_filename) + 1 + 4);
57 sprintf(bakFilename.buf, "%s.bak", m_filename);
58 remove(bakFilename.buf);
59 rename(m_filename, bakFilename.buf);
60 m_file = fopen(m_filename, "w+");
61 if (!m_file) return;
62 }
63
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000064 time_t current = time(0);
65 if (current != m_lastLogTime) {
66 m_lastLogTime = current;
67 fprintf(m_file, "\n%s", ctime(&m_lastLogTime));
68 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069
70 fprintf(m_file," %s:", logname);
71 int column = strlen(logname) + 2;
72 if (column < indent) {
73 fprintf(m_file,"%*s",indent-column,"");
74 column = indent;
75 }
76 while (true) {
77 const char* s = strchr(message, ' ');
78 int wordLen;
79 if (s) wordLen = s-message;
80 else wordLen = strlen(message);
81
82 if (column + wordLen + 1 > width) {
83 fprintf(m_file,"\n%*s",indent,"");
84 column = indent;
85 }
86 fprintf(m_file," %.*s",wordLen,message);
87 column += wordLen + 1;
88 message += wordLen + 1;
89 if (!s) break;
90 }
91 fprintf(m_file,"\n");
92 fflush(m_file);
93}
94
95void Logger_File::setFilename(const char* filename)
96{
97 closeFile();
Adam Tkacd36b6262009-09-04 10:57:20 +000098 m_filename = strDup(filename);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099}
100
101void Logger_File::setFile(FILE* file)
102{
103 closeFile();
104 m_file = file;
105}
106
107void Logger_File::closeFile()
108{
109 if (m_filename) {
110 if (m_file) {
111 fclose(m_file);
112 m_file = 0;
113 }
Adam Tkacd36b6262009-09-04 10:57:20 +0000114 strFree(m_filename);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 m_filename = 0;
116 }
117}
118
119static Logger_File logger("file");
120
121bool rfb::initFileLogger(const char* filename) {
122 logger.setFilename(filename);
123 logger.registerLogger();
124 return true;
125}