blob: 8a109e4a09be02cf2840cc4aca25e9a0fff47e7f [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
64#ifndef _WIN32_WCE
65 time_t current = time(0);
66 if (current != m_lastLogTime) {
67 m_lastLogTime = current;
68 fprintf(m_file, "\n%s", ctime(&m_lastLogTime));
69 }
70#endif
71
72 fprintf(m_file," %s:", logname);
73 int column = strlen(logname) + 2;
74 if (column < indent) {
75 fprintf(m_file,"%*s",indent-column,"");
76 column = indent;
77 }
78 while (true) {
79 const char* s = strchr(message, ' ');
80 int wordLen;
81 if (s) wordLen = s-message;
82 else wordLen = strlen(message);
83
84 if (column + wordLen + 1 > width) {
85 fprintf(m_file,"\n%*s",indent,"");
86 column = indent;
87 }
88 fprintf(m_file," %.*s",wordLen,message);
89 column += wordLen + 1;
90 message += wordLen + 1;
91 if (!s) break;
92 }
93 fprintf(m_file,"\n");
94 fflush(m_file);
95}
96
97void Logger_File::setFilename(const char* filename)
98{
99 closeFile();
100 m_filename = strDup(filename);
101}
102
103void Logger_File::setFile(FILE* file)
104{
105 closeFile();
106 m_file = file;
107}
108
109void Logger_File::closeFile()
110{
111 if (m_filename) {
112 if (m_file) {
113 fclose(m_file);
114 m_file = 0;
115 }
116 strFree(m_filename);
117 m_filename = 0;
118 }
119}
120
121static Logger_File logger("file");
122
123bool rfb::initFileLogger(const char* filename) {
124 logger.setFilename(filename);
125 logger.registerLogger();
126 return true;
127}